Ansible常用模块使用

1:Command 模块

功能:在远程主机执行命令,此为默认模块,可忽略-m选项
注意:此命令不支持 $VARNAME < > | ; & 等,用shell模块实现

# 示例
[root@ansible-manager ~]# ansible all -m command -a "echo 'Hello World'"
10.0.0.12 | CHANGED | rc=0 >>
Hello World
10.0.0.11 | CHANGED | rc=0 >>
Hello World
[root@ansible-manager ~]# ansible all -m command -a "chdir=/etc cat redhat-release"
10.0.0.12 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
10.0.0.11 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
[root@ansible-manager ~]# ansible all -m command -a "chdir=/etc/ creates=/opt/os.txt cat redhat-release"
10.0.0.11 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
10.0.0.12 | CHANGED | rc=0 >>
CentOS Linux release 7.9.2009 (Core)
# 解释:这里面有几个陌生的操作带大家熟悉一下
1:chdir:它是定位我们当前的目录
2:creates:这个是用于创建文件
[root@ansible-manager ~]# ansible all -m command -a 'chdir=/etc removes=/opt/os cat redhat-release'
10.0.0.12 | SUCCESS | rc=0 >>
skipped, since /opt/os does not exist
10.0.0.11 | SUCCESS | rc=0 >>
skipped, since /opt/os does not exist
# 自然了有创建当然也肯定有删除的

[root@ansible-manager ~]# ansible 10.0.0.11 -m command -a "systemctl start nginx"
10.0.0.11 | CHANGED | rc=0 >>
[root@ansible-manager ~]# ansible 10.0.0.11 -m command -a "systemctl status nginx"
10.0.0.11 | CHANGED | rc=0 >>
?𹠠nginx.service - The nginx HTTP and reverse proxy server
   Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
   Active: active (running) since Tue 2021-12-28 03:05:47 EST; 8s ago
  Process: 18506 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
  Process: 18504 ExecStartPre=/usr/sbin/nginx -t (code=exited, status=0/SUCCESS)
  Process: 18503 ExecStartPre=/usr/bin/rm -f /run/nginx.pid (code=exited, status=0/SUCCESS)
 Main PID: 18508 (nginx)
   CGroup: /system.slice/nginx.service
           ?𼰿𺲀18508 nginx: master process /usr/sbin/ngin
           ?𺰿𺲀18510 nginx: worker proces

Dec 28 03:05:47 virtual_host systemd[1]: Starting The nginx HTTP and reverse proxy server...
Dec 28 03:05:47 virtual_host nginx[18504]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Dec 28 03:05:47 virtual_host nginx[18504]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Dec 28 03:05:47 virtual_host systemd[1]: Started The nginx HTTP and reverse proxy server.

2:Shell模块

功能:和command相似,用shell执行命令

# 示例
[root@ansible-manager ~]# ansible all -m shell -a "echo $HOSTNAME"
10.0.0.11 | CHANGED | rc=0 >>
ansible-manager
10.0.0.12 | CHANGED | rc=0 >>
ansible-manager
# 注意看,上下两者的区别""和''的区别
[root@ansible-manager ~]# ansible all -m shell -a 'echo $HOSTNAME'
10.0.0.12 | CHANGED | rc=0 >>
virtual_host
10.0.0.11 | CHANGED | rc=0 >>
virtual_host

注意:调用bash执行命令 类似 cat /var/log/nginx/accaess.log | awk '{print $1,$2}' &> /opt/log 这些复杂命令,即使使用shell也可能会失败,解决办法:写到脚本时,copy到远程,执行,再把需要的结果拉回执行命令的机器

范例:将shell模块代替command,设为模块
vim /etc/ansible/ansible.cfg 114
将command替换为shell
[root@ansible-manager ~]# grep "module_name" /etc/ansible/ansible.cfg 
module_name = shell

3:Script模块

功能:在远程主机上运行ansible服务器上的脚本

# 示例
[root@ansible-manager ~]# cat script.sh 
#!/bin/bash
echo "Hello World" >> /opt/Hello
[root@ansible-manager ~]# ansible all -m script -a ./script.sh 
10.0.0.12 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.0.0.12 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.0.0.12 closed."
    ], 
    "stdout": "Hello World\r\n", 
    "stdout_lines": [
        "Hello World"
    ]
}
10.0.0.11 | CHANGED => {
    "changed": true, 
    "rc": 0, 
    "stderr": "Shared connection to 10.0.0.11 closed.\r\n", 
    "stderr_lines": [
        "Shared connection to 10.0.0.11 closed."
    ], 
    "stdout": "Hello World\r\n", 
    "stdout_lines": [
        "Hello World"
    ]
}
[root@ansible-manager ~]# ansible all -m shell -a 'ls /opt'
10.0.0.11 | CHANGED | rc=0 >>
Hello
10.0.0.12 | CHANGED | rc=0 >>
Hello

4:Copy模块

功能:从ansible服务器主控端复制文件到远程主机

1:如目标存在,默认覆盖,此处指定先备份
[root@ansible-manager ~]# ansible all -m copy -a "src=/root/script.sh dest=/tmp/script.sh owner=zhangsan mode=755 backup=yes"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "14d60cdee95302d9978ce5b8461c94bb05d00446", 
    "dest": "/tmp/script.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "89056180a58785e6f7981dd2873fa2ba", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "size": 45, 
    "src": "/root/.ansible/tmp/ansible-tmp-1657139060.93-16262-79896353984334/source", 
    "state": "file", 
    "uid": 1000
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "14d60cdee95302d9978ce5b8461c94bb05d00446", 
    "dest": "/tmp/script.sh", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "89056180a58785e6f7981dd2873fa2ba", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "size": 45, 
    "src": "/root/.ansible/tmp/ansible-tmp-1657139060.92-16261-210995197466816/source", 
    "state": "file", 
    "uid": 1000
}

# 解释
1:src:指定ansible的文件地址
2:dest:指定目标地址的路径及文件名
3:owne:指定属主
4:mode:指定传过去的文件权限
5:backup:如果文件存在则备份再传


2:指定内容,直接生成目标文件
[root@ansible-manager ~]# ansible all -m copy -a "content='Hello' dest=/opt/hello"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0", 
    "dest": "/opt/hello", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "8b1a9953c4611296a827abf8c47804d7", 
    "mode": "0644", 
    "owner": "root", 
    "size": 5, 
    "src": "/root/.ansible/tmp/ansible-tmp-1657139377.96-16411-10893149968568/source", 
    "state": "file", 
    "uid": 0
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "checksum": "f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0", 
    "dest": "/opt/hello", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "8b1a9953c4611296a827abf8c47804d7", 
    "mode": "0644", 
    "owner": "root", 
    "size": 5, 
    "src": "/root/.ansible/tmp/ansible-tmp-1657139377.96-16410-139803205625736/source", 
    "state": "file", 
    "uid": 0
}
[root@ansible-manager ~]# ansible all -m shell -a "ls /opt/ && cat /opt/hello"
10.0.0.12 | CHANGED | rc=0 >>
hello
Hello
10.0.0.11 | CHANGED | rc=0 >>
hello
Hello


3:复制/opt/下文件,不包括/opt目录自身
[root@ansible-manager ~]# ansible all -m copy -a "src=/opt/ dest=/tmp"
10.0.0.11 | SUCCESS => {
    "changed": false, 
    "dest": "/tmp/", 
    "src": "/opt/"
}
10.0.0.12 | SUCCESS => {
    "changed": false, 
    "dest": "/tmp/", 
    "src": "/opt/"
}

# 解释,这里传输也是传输的ansible管理端的

5:Fetch模块

功能:从远程主机提取文件至ansible的主控端,copy相反,目前不支持目录

# 示例
[root@ansible-manager ~]# ansible all -m fetch -a "src=/etc/redhat-release dest=/data/"
10.0.0.11 | CHANGED => {
    "changed": true, 
    "checksum": "0d3186157c40752f89db0e618a5866935b523e7b", 
    "dest": "/data/10.0.0.11/etc/redhat-release", 
    "md5sum": "902962816d0ec4fbb532949f70a41ae7", 
    "remote_checksum": "0d3186157c40752f89db0e618a5866935b523e7b", 
    "remote_md5sum": null
}
10.0.0.12 | CHANGED => {
    "changed": true, 
    "checksum": "0d3186157c40752f89db0e618a5866935b523e7b", 
    "dest": "/data/10.0.0.12/etc/redhat-release", 
    "md5sum": "902962816d0ec4fbb532949f70a41ae7", 
    "remote_checksum": "0d3186157c40752f89db0e618a5866935b523e7b", 
    "remote_md5sum": null
}
# 这里和copy正好相反
1:src:是目标机器的文件
2:dest:ansible管理端的路径(可以是空目录会自动创建哦)

[root@ansible-manager ~]# tree /data
/data
	10.0.0.11
		etc
			redhat-release
	10.0.0.12
		etc
			redhat-release

6:File模块

功能:设置文件属性

# 示例
# 创建空文件
[root@ansible-manager ~]# ansible all -m file -a "path=/opt/linux.txt state=touch"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/linux.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/linux.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0644", 
    "owner": "root", 
    "size": 0, 
    "state": "file", 
    "uid": 0
}
[root@ansible-manager ~]# ansible all -m shell -a "ls /opt/"
10.0.0.12 | CHANGED | rc=0 >>
linux.txt
10.0.0.11 | CHANGED | rc=0 >>
linux.txt

# 删除
[root@ansible-manager ~]# ansible all -m file -a "path=/opt/linux.txt state=absent"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/linux.txt", 
    "state": "absent"
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "path": "/opt/linux.txt", 
    "state": "absent"
}

# 创建并设置权限属主等
[root@ansible-manager ~]# ansible all -m file -a "path=/opt/linux.txt state=touch owner=zhangsan mode=755"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/linux.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/opt/linux.txt", 
    "gid": 0, 
    "group": "root", 
    "mode": "0755", 
    "owner": "zhangsan", 
    "size": 0, 
    "state": "file", 
    "uid": 1000
}
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /opt"
10.0.0.11 | CHANGED | rc=0 >>
-rwxr-xr-x 1 zhangsan root 0 12?𷰠28 04:08 linux.txt
10.0.0.12 | CHANGED | rc=0 >>
-rwxr-xr-x 1 zhangsan root 0 12?𷰠28 04:08 linux.txt


# 创建目录
[root@ansible-manager ~]# ansible all -m file -a "path=/var/www/html state=directory owner=nginx group=nginx"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 995, 
    "group": "nginx", 
    "mode": "0755", 
    "owner": "nginx", 
    "path": "/var/www/html", 
    "size": 6, 
    "state": "directory", 
    "uid": 997
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 995, 
    "group": "nginx", 
    "mode": "0755", 
    "owner": "nginx", 
    "path": "/var/www/html", 
    "size": 6, 
    "state": "directory", 
    "uid": 997
}
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /var/www"
10.0.0.11 | CHANGED | rc=0 >>
drwxr-xr-x 2 nginx nginx 6 12?𷰠28 04:15 html
10.0.0.12 | CHANGED | rc=0 >>
drwxr-xr-x 2 nginx nginx 6 12?𷰠28 04:15 html


# 创建软链接
[root@ansible-manager ~]# ansible all -m file -a 'src=/var/www/html dest=/etc/nginx/html state=link'
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/etc/nginx/html", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 13, 
    "src": "/var/www/html", 
    "state": "link", 
    "uid": 0
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "dest": "/etc/nginx/html", 
    "gid": 0, 
    "group": "root", 
    "mode": "0777", 
    "owner": "root", 
    "size": 13, 
    "src": "/var/www/html", 
    "state": "link", 
    "uid": 0
}
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /etc/nginx/ | grep html"
10.0.0.11 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root   13 12?𷰠28 04:19 html -> /var/www/html
10.0.0.12 | CHANGED | rc=0 >>
lrwxrwxrwx 1 root root   13 12?𷰠28 04:19 html -> /var/www/html

7:unarchive模块

功能:解包解压缩

实现有两种用法:
1:将ansible主机上的压缩包传到远程主机后解压缩至特定目录,设置copy=yes
2:将远程主机上的某个压缩包解压缩到指定路径下,设置copy=no

常见参数:
1:copy:默认为yes,当copy=yes,拷贝的文件是从ansible主机复制到远程主机上,如果设置为copy=no,会在远程主机上寻找src源文件
2:remote_src:和copy功能一样且互斥,yes表示在远程主机,不在ansible主机,no表示文件在ansible主机上
3:src:源路径,可以是ansible主机上的路径,也可以是远程主机上的路径,如果是远程主机上的路径,则需要设置copy=no
4:dest:远程主机上的目标路径
5:mode:设置解压缩后的文件权限

# 示例
[root@ansible-manager ~]# ansible all -m unarchive -a 'src=/root/go1.18.linux-amd64.tar.gz dest=/tmp/'
[root@ansible-manager ~]# ansible all -m shell -a "ls /tmp| grep go"
10.0.0.12 | CHANGED | rc=0 >>
go
10.0.0.11 | CHANGED | rc=0 >>
go
# 第一步利用copy模块分发ansible压缩包到各个主机,然后再解压
[root@ansible-manager ~]# ansible all -m copy -a "src=/root/go1.18.linux-amd64.tar.gz dest=/opt/go1.18.linux-amd64.tar.gz"
[root@ansible-manager ~]# ansible all -m shell -a "ls /opt/"
10.0.0.11 | CHANGED | rc=0 >>
go1.18.linux-amd64.tar.gz
10.0.0.12 | CHANGED | rc=0 >>
go1.18.linux-amd64.tar.gz
# 第二步删除ansible上的压缩包然后使用各个主机上的压缩包解压在指定的路径
[root@ansible-manager ~]# ansible all -m unarchive -a "src=/opt/go1.18.linux-amd64.tar.gz dest=/tmp/ copy=no mode=0777"
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /tmp/ | grep go"
10.0.0.12 | CHANGED | rc=0 >>
drwxrwxrwx  10 root     root   257 3?𷰠 15 2022 go
10.0.0.11 | CHANGED | rc=0 >>
drwxrwxrwx  10 root     root   257 3?𷰠 15 2022 go
# 这里我们需要看的是权限哦 和上面的mode是对应的

# 通过网络下载压缩包(以Nginx为例子)
https://nginx.org/download/nginx-1.23.0.tar.gz

[root@ansible-manager ~]# ansible all -m unarchive -a 'src=http://nginx.org/download/nginx-1.23.0.tar.gz dest=/tmp/ copy=no'
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /tmp | grep nginx"
10.0.0.11 | CHANGED | rc=0 >>
drwxr-xr-x 8 lisi lisi  158 6?𷰠 21 2022 nginx-1.23.0
10.0.0.12 | CHANGED | rc=0 >>
drwxr-xr-x 8 lisi lisi 158 6?𷰠 21 2022 nginx-1.23.0
# 需要注意的是。这里面也是帮你已经解压好了哦

8:Archive模块

功能:打包压缩

# 示例

[root@ansible-manager ~]# ansible all -m archive -a 'path=/var/log/ dest=/tmp/log.tar.gz format=gz owner=zhangsan mode=0600'
[root@ansible-manager ~]# ansible all -m shell -a "ls -l /tmp|grep log"
10.0.0.12 | CHANGED | rc=0 >>
-rw------- 1 zhangsan root 562578 12?𷰠28 07:00 log.tar.gz
10.0.0.11 | CHANGED | rc=0 >>
-rw------- 1 zhangsan root 575067 12?𷰠28 07:00 log.tar.gz
# 这里需要注意的就是权限和数组,然后我们可以配合Fetch模块取打包好的日志到ansible主控端。这样就做好了一个简单的日志收集

9:Hostname模块

功能:管理主机名

# 示例
[root@ansible-manager ~]# ansible 10.0.0.11 -m hostname -a "name=ansible_agent_1"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "", 
        "ansible_fqdn": "ansible_agent_1", 
        "ansible_hostname": "ansible_agent_1", 
        "ansible_nodename": "ansible_agent_1", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "ansible_agent_1"
}
# 这里暂时还是只能针对一个主机更改哦,但是我们可以配合shell脚本实现自动化更改

# 我这里写一个示例


[root@ansible-manager ~]# cat hostname.sh 
#!/bin/bash
names="ansible_agent_"
nets="10.0.0"

for i in {11..12};do
    ansible 10.0.0.$i -m hostname -a "name=$names$i"
done

# 执行
[root@ansible-manager ~]# bash hostname.sh 
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "", 
        "ansible_fqdn": "ansible_agent_11", 
        "ansible_hostname": "ansible_agent_11", 
        "ansible_nodename": "ansible_agent_11", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false, 
    "name": "ansible_agent_11"
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "ansible_domain": "", 
        "ansible_fqdn": "ansible_agent_12", 
        "ansible_hostname": "ansible_agent_12", 
        "ansible_nodename": "ansible_agent_12", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "ansible_agent_12"
}

10:cron模块

功能:计划任务
支持时间:minute,hour,day,month,weekday

# 示例
[root@ansible-manager ~]# cat job.sh 
#!/bin/bash
echo "$PATH" >>/opt/path

# 创建任务
[root@ansible-manager ~]# ansible 10.0.0.11 -m cron -a 'hour=*/2 minute=*/1  name="job" job=/root/job.sh'
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job"
    ]
}

[root@ansible-manager ~]# ansible 10.0.0.11 -m shell -a "crontab -l"
10.0.0.11 | CHANGED | rc=0 >>
#Ansible: job
*/1 */2 * * * /root/job.sh
# 解释:这里的脚本必须要传到所有的主机才可以哦

[root@ansible-manager ~]# ansible all -m cron -a "minute=*/1 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job", 
        "Synctime"
    ]
}
[root@ansible-manager ~]# ansible all -m shell -a "crontab -l"
10.0.0.12 | CHANGED | rc=0 >>
#Ansible: Synctime
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
10.0.0.11 | CHANGED | rc=0 >>
#Ansible: Synctime
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null

# 禁用计划任务
[root@ansible-manager ~]# ansible all -m cron -a "minute=*/1 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime disabled=yes"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job", 
        "Synctime"
    ]
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}
[root@ansible-manager ~]# ansible all -m shell -a "crontab -l"
10.0.0.11 | CHANGED | rc=0 >>
#Ansible: job
*/1 * * * * /root/job.sh
#Ansible: Synctime
#*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
10.0.0.12 | CHANGED | rc=0 >>
#Ansible: Synctime
#*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null

# 很搞笑居然是注释掉了

# 启用计划任务
[root@ansible-manager ~]# ansible all -m cron -a "minute=*/1 job='/usr/sbin/ntpdate ntp.aliyun.com &>/dev/null' name=Synctime disabled=no"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "job", 
        "Synctime"
    ]
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}
[root@ansible-manager ~]# ansible all -m shell -a "crontab -l"
10.0.0.12 | CHANGED | rc=0 >>
#Ansible: Synctime
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
10.0.0.11 | CHANGED | rc=0 >>
#Ansible: job
*/1 */2 * * * /root/job.sh
#Ansible: Synctime
*/1 * * * * /usr/sbin/ntpdate ntp.aliyun.com &>/dev/null
# 关键点在:disabled这个参数上  yes/no

# 删除任务
[root@ansible-manager ~]# ansible all -m cron -a "name=job state=absent"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": [
        "Synctime"
    ]
}

[root@ansible-manager ~]# ansible all -m cron -a "state=absent name=Synctime"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "envs": [], 
    "jobs": []
}

[root@ansible-manager ~]# ansible all -m shell -a "crontab -l"
10.0.0.12 | CHANGED | rc=0 >>

10.0.0.11 | CHANGED | rc=0 >>

# 关注点在于:这里是匹配name执行操作的 absent就是不存在的意思,意思就是删除

11:Yum模块

功能:管理软件包,只支持RHEL,CentOS,fedora,不支持Ubuntu其它版本

# 示例
# 安装 httpd
[root@ansible-manager ~]# ansible all -m yum -a "name=httpd state=present"

# 检查是否安装成功
[root@ansible-manager ~]# ansible all -m shell -a "systemctl status httpd"
10.0.0.12 | FAILED | rc=3 >>
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)non-zero return code
10.0.0.11 | FAILED | rc=3 >>
httpd.service - The Apache HTTP Server
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: inactive (dead)
     Docs: man:httpd(8)
           man:apachectl(8)non-zero return code


# 卸载
[root@ansible-manager ~]# ansible all -m yum -a "name=mariadb state=absent"

# 命令一样但state的参数不同
[root@ansible-manager ~]# ansible all -m shell -a "systemctl status httpd"
10.0.0.11 | FAILED | rc=4 >>
Unit httpd.service could not be found.non-zero return code
10.0.0.12 | FAILED | rc=4 >>
Unit httpd.service could not be found.non-zero return code

12:Service模块

功能:管理服务

# 示例
# 第一步:安装服务:nginx
[root@ansible-manager ~]# ansible all -m yum -a "name=nginx state=present"

# 第二步:启动服务并设置开机自启
[root@ansible-manager ~]# ansible all -m service -a "name=nginx state=started enabled=yes"
# 第三步:测试
[root@ansible-manager ~]# ansible all -m shell -a "curl -I 127.0.0.1"
10.0.0.11 | CHANGED | rc=0 >>
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 07 Jul 2022 17:27:40 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"

10.0.0.12 | CHANGED | rc=0 >>
HTTP/1.1 200 OK
Server: nginx/1.20.1
Date: Thu, 07 Jul 2022 17:27:40 GMT
Content-Type: text/html
Content-Length: 4833
Last-Modified: Fri, 16 May 2014 15:12:48 GMT
Connection: keep-alive
ETag: "53762af0-12e1"

# 停止服务
[root@ansible-manager ~]# ansible all -m service -a "name=nginx state=stopped"
# 测试
[root@ansible-manager ~]# ansible all -m shell -a "curl -I 127.0.0.1"
10.0.0.12 | FAILED | rc=7 >>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed connect to 127.0.0.1:80; Connection refusednon-zero return code
10.0.0.11 | FAILED | rc=7 >>
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0curl: (7) Failed connect to 127.0.0.1:80; Connection refusednon-zero return code


# 重载服务
# 前提是yum安装的服务必须支持reload哦
[root@ansible-manager ~]# ansible all -m service -a "name=nginx state=started"

# 重启服务
[root@ansible-manager ~]# ansible all -m shell -a "sed -i 's/^Listen 80/Listen 8080/' /etc/httpd/conf/httpd.conf"
[WARNING]: Consider using the replace, lineinfile or template module rather than running 'sed'.  If you need to use command because
replace, lineinfile or template is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in
ansible.cfg to get rid of this message.
10.0.0.12 | CHANGED | rc=0 >>

10.0.0.11 | CHANGED | rc=0 >>

[root@ansible-manager ~]# ansible all -m service -a 'name=httpd state=restarted'

[root@ansible-manager ~]# ansible all -m shell -a "curl -I 127.0.0.1:8080"
[WARNING]: Consider using the get_url or uri module rather than running 'curl'.  If you need to use command because get_url or uri is
insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message.
10.0.0.12 | CHANGED | rc=0 >>
HTTP/1.1 403 Forbidden
Date: Thu, 07 Jul 2022 17:51:03 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8  % Total    % Received % Xferd  Average Speed   Time    Time     Time 

10.0.0.11 | CHANGED | rc=0 >>
HTTP/1.1 403 Forbidden
Date: Thu, 07 Jul 2022 17:51:03 GMT
Server: Apache/2.4.6 (CentOS)
Last-Modified: Thu, 16 Oct 2014 13:20:58 GMT
ETag: "1321-5058a1e728280"
Accept-Ranges: bytes
Content-Length: 4897
Content-Type: text/html; charset=UTF-8  % Total    % Received % Xferd  Average Speed   Time    Time     Time 

13:User模块

功能:管理用户

# 示例
[root@ansible-manager ~]# ansible all -m user -a 'name=list comment="test user" uid=2022 home=/home/list group=root'
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "test user", 
    "create_home": true, 
    "group": 0, 
    "home": "/home/list", 
    "name": "list", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 2022
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "test user", 
    "create_home": true, 
    "group": 0, 
    "home": "/home/list", 
    "name": "list", 
    "shell": "/bin/bash", 
    "state": "present", 
    "system": false, 
    "uid": 2022
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/passwd | grep list"
10.0.0.12 | CHANGED | rc=0 >>
list:x:2022:0:test user:/home/list:/bin/bash
10.0.0.11 | CHANGED | rc=0 >>
list:x:2022:0:test user:/home/list:/bin/bash

[root@ansible-manager ~]# ansible all -m user -a 'name=mysql comment=mysql uid=2022 group=root groups="root,daemon" shell=/sbin/nologin system=yes create_home=no home=/home/mysql non_unique=yes'
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "mysql", 
    "create_home": false, 
    "group": 0, 
    "groups": "root,daemon", 
    "home": "/home/mysql", 
    "name": "mysql", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": true, 
    "uid": 2022
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "comment": "mysql", 
    "create_home": false, 
    "group": 0, 
    "groups": "root,daemon", 
    "home": "/home/mysql", 
    "name": "mysql", 
    "shell": "/sbin/nologin", 
    "state": "present", 
    "system": true, 
    "uid": 2022
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/passwd | grep mysql"
10.0.0.12 | CHANGED | rc=0 >>
mysql:x:2022:0:mysql:/home/mysql:/sbin/nologin
10.0.0.11 | CHANGED | rc=0 >>
mysql:x:2022:0:mysql:/home/mysql:/sbin/nologin


# 删除用户及家目录等数据
[root@ansible-manager ~]# ansible all -m user -a "name=list state=absent remove=yes"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "list", 
    "remove": true, 
    "state": "absent"
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "force": false, 
    "name": "list", 
    "remove": true, 
    "state": "absent"
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/passwd | grep list"
10.0.0.11 | FAILED | rc=1 >>
non-zero return code
10.0.0.12 | FAILED | rc=1 >>
non-zero return code

14:Group模块

功能:管理组

# 示例
# 创建组
[root@ansible-manager ~]# ansible all -m group -a 'name=mysql gid=1000 system=yes'
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1000, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "gid": 1000, 
    "name": "mysql", 
    "state": "present", 
    "system": true
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/group | grep mysql"
10.0.0.11 | CHANGED | rc=0 >>
mysql:x:1000:
10.0.0.12 | CHANGED | rc=0 >>
mysql:x:1000:



# 删除组
[root@ansible-manager ~]# ansible all -m group -a 'name=mysql state=absent'
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "mysql", 
    "state": "absent"
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "name": "mysql", 
    "state": "absent"
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/group | grep mysql"
10.0.0.12 | FAILED | rc=1 >>
non-zero return code
10.0.0.11 | FAILED | rc=1 >>
non-zero return code

15:Lineinfile模块

ansible在使用sed进行替换时,经常会遇到需要转义的问题,而且ansible在遇到特殊符号进行替换时,存在问题,无法正常进行替换 。其实在ansible自身提供了两个模块:lineinfile模块和replace模块,可以方便的进行替换

功能:相当于sed,可以修改文件内容

# 示例
# 远程主机的Selinux文件
[root@ansible_agent_11 ~]# cat /etc/selinux/config 

# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of three values:
#     targeted - Targeted processes are protected,
#     minimum - Modification of targeted policy. Only selected processes are protected. 
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted 

# 我们把disabled改成enforcing

[root@ansible-manager ~]# ansible all -m lineinfile -a "path=/etc/selinux/config regexp='^SELINUX=' line='SELINUX=enforcing'"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "msg": "line replaced"
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/selinux/config | grep -v '#' | grep enf"
10.0.0.12 | CHANGED | rc=0 >>
SELINUX=enforcing
10.0.0.11 | CHANGED | rc=0 >>
SELINUX=enforcing
# 解释:path指定文件,regexp正则匹配内容,line替换的内容


# 删除内容
# fstab文件
[root@ansible_agent_11 ~]# cat /etc/fstab 

#
# /etc/fstab
# Created by anaconda on Mon Dec 27 21:14:15 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0
#/dev/mapper/centos-swap swap                    swap    defaults        0 0

[root@ansible-manager ~]# ansible all -m lineinfile -a 'dest=/etc/fstab state=absent regexp="^#"'
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 8, 
    "msg": "8 line(s) removed"
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "backup": "", 
    "changed": true, 
    "found": 8, 
    "msg": "8 line(s) removed"
}

[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/fstab"
10.0.0.12 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0
10.0.0.11 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0

16:Replace模块

该模块有点类似于sed命令,主要也是基于正则进行匹配和替换

# 示例
[root@ansible-manager ~]# ansible all -m replace -a "path=/etc/fstab regexp='^(UUID.*)' replace='#\1'"
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/fstab"
10.0.0.12 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
#UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0
10.0.0.11 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
#UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0

# 再次改回来
[root@ansible-manager ~]# ansible all -m replace -a "path=/etc/fstab regexp='^#(.*)' replace='\1'"
10.0.0.12 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
10.0.0.11 | CHANGED => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": true, 
    "msg": "1 replacements made"
}
[root@ansible-manager ~]# ansible all -m shell -a "cat /etc/fstab"
10.0.0.11 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0
10.0.0.12 | CHANGED | rc=0 >>

/dev/mapper/centos-root /                       xfs     defaults        0 0
UUID=b2974ea5-dea4-458a-a2ca-744a525404a9 /boot                   xfs     defaults        0 0

17:Setup模块

功能: setup 模块来收集主机的系统信息,这些 facts 信息可以直接以变量的形式使用,但是如果主机较多,会影响执行速度,可以使用
gather_facts: no  来禁止 Ansible 收集 facts 信息

# 示例
# 获取节点nodename
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_nodename"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_nodename": "ansible_agent_12", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_nodename": "ansible_agent_11", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 获取节点hostname
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_hostname"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "ansible_agent_12", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_hostname": "ansible_agent_11", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 获取domain
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_domain"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_domain": "", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 以mb为单位获取总内存大小
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_memtotal_mb"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_memtotal_mb": 972, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_memtotal_mb": 972, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出更多的内存信息
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_memory_mb"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 757, 
                "used": 215
            }, 
            "real": {
                "free": 366, 
                "total": 972, 
                "used": 606
            }, 
            "swap": {
                "cached": 0, 
                "free": 0, 
                "total": 0, 
                "used": 0
            }
        }, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_memory_mb": {
            "nocache": {
                "free": 769, 
                "used": 203
            }, 
            "real": {
                "free": 379, 
                "total": 972, 
                "used": 593
            }, 
            "swap": {
                "cached": 0, 
                "free": 0, 
                "total": 0, 
                "used": 0
            }
        }, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出内存的free(已用信息)
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_memfree_mb"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 379, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_memfree_mb": 367, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出主机信息
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_os_family"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_os_family": "RedHat", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_os_family": "RedHat", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出系统大版本
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_distribution_major_version"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "7", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_major_version": "7", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出系统小版本
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_distribution_version"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_version": "7.9", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_distribution_version": "7.9", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出CPU个数
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_processor_vcpus"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor_vcpus": 1, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor_vcpus": 1, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 列出IP地址
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_all_ipv4_addresses"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.0.11"
        ], 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_all_ipv4_addresses": [
            "10.0.0.12"
        ], 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 查看系统架构
[root@ansible-manager ~]# ansible all -m setup -a "filter=ansible_architecture"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_architecture": "x86_64", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 查看CPU架构
[root@ansible-manager ~]# ansible all -m  setup  -a "filter=ansible_processor*"
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor": [
            "0", 
            "GenuineIntel", 
            "Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz"
        ], 
        "ansible_processor_cores": 1, 
        "ansible_processor_count": 1, 
        "ansible_processor_threads_per_core": 1, 
        "ansible_processor_vcpus": 1, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_processor": [
            "0", 
            "GenuineIntel", 
            "Intel(R) Core(TM) i5-8300H CPU @ 2.30GHz"
        ], 
        "ansible_processor_cores": 1, 
        "ansible_processor_count": 1, 
        "ansible_processor_threads_per_core": 1, 
        "ansible_processor_vcpus": 1, 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

# 查看远程主机python版本
[root@ansible-manager ~]# \ansible all -m setup -a "filter=ansible_python_version"
10.0.0.11 | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "2.7.5", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}
10.0.0.12 | SUCCESS => {
    "ansible_facts": {
        "ansible_python_version": "2.7.5", 
        "discovered_interpreter_python": "/usr/bin/python"
    }, 
    "changed": false
}

18:总结

# 模块非常的多,常用的都给大家列出来了,大家可以慢慢的消化一下,这里主要还是根据Ad-Hoc操作的
posted @ 2022-07-08 03:29  Layzer  阅读(147)  评论(0编辑  收藏  举报