1、Ansible:Ansible核心程序。
HostInventory:记录由Ansible管理的主机信息,包括端口、密码、ip等。
2、Playbooks:“剧本”YAML格式文件,多个任务定义在一个文件中,定义主机需要调用哪些模块来完成的功能。
3、CoreModules:核心模块,主要操作是通过调用核心模块来完成管理任务。
4、CustomModules:自定义模块,完成核心模块无法完成的功能,支持多种语言。
5、ConnectionPlugins:连接插件,Ansible和Host通信使用
1、加载自己的配置文件,默认/etc/ansible/ansible.cfg;
2、查找对应的主机配置文件,找到要执行的主机或者组;
3、加载自己对应的模块文件,如 command;
4、通过ansible将模块或命令生成对应的临时py文件(python脚本), 并将该文件传输至远程服务器;
5、对应执行用户的家目录的 ~/.ansible/tmp/XXX/XXX.PY文件;
6、给文件 +x 执行权限;
6、执行并返回结果;
8、删除临时py文件,sleep 0退出;
安装目录如下(yum安装):
配置文件目录:/etc/ansible/
执行文件目录:/usr/bin/
Lib库依赖目录:/usr/lib/pythonX.X/site-packages/ansible/
Help文档目录:/usr/share/doc/ansible-X.X.X/
Man文档目录:/usr/share/man/man1/
1、检查环境变量ANSIBLE_CONFIG指向的路径文件(export ANSIBLE_CONFIG=/etc/ansible.cfg);
2、~/.ansible.cfg,检查当前目录下的ansible.cfg配置文件;
3、/etc/ansible.cfg检查etc目录的配置文件。
inventory = /etc/ansible/hosts #这个参数表示资源清单inventory文件的位置
library = /usr/share/ansible #指向存放Ansible模块的目录,支持多个目录方式,只要用冒号(:)隔开就可以
forks = 5 #并发连接数,默认为5
sudo_user = root #设置默认执行命令的用户
remote_port = 22 #指定连接被管节点的管理端口,默认为22端口,建议修改,能够更加安全
host_key_checking = False #设置是否检查SSH主机的密钥,值为True/False。关闭后第一次连接不会提示配置实例
timeout = 60 #设置SSH连接的超时时间,单位为秒
log_path = /var/log/ansible.log #指定一个存储ansible日志的文件(默认不记录日志)
1、 直接指明主机地址或主机名:
192.168.1.2
192.168.1.3
2、 定义一个主机组[组名]把地址或主机名加进去
[testnode]
node2
/usr/bin/ansible Ansibe AD-Hoc 临时命令执行工具,常用于临时命令的执行
/usr/bin/ansible-doc Ansible 模块功能查看工具
/usr/bin/ansible-playbook Ansible 定制自动化的任务集编排工具
ansible 目标节点/节点组名称 -m 运行的模块名称 -a "模块中的指定参数"
测试节点连通性, 使用 ping 模块:
node2 | SUCCESS => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false,
"ping": "pong"
}
2、command 模块:直接在远程主机上执行简单命令,并将结果返回本主机。不会通过shell进行处理,比如$HOME和操作如"<“,”>“,”|“,”;“,”&" 工作(需要使用(shell)模块实现这些功能),该命令不支持| 管道命令,
该模块下常用的几个命令:
chdir='/tmp/test 需要执行的命令' #切换到/tmp/test 目录下执行接下来的命令
creates='/tmp/test.sh 需要执行的命令' #判断一个文件是否存在,如果存在就不执行后面的命令
removes='/tmp/test.sh 需要执行的命令' #判断一个文件是否不存在,如果不存在就不执行后面的命令
warn='yes/no' #用于启用或禁用任务警告
执行示例:
ansible testnode -m command -a "chdir='/tmp/an-test/' ls"
[root@test test] ansible testnode -m command -a "chdir='/tmp/an-test/' ls"
node2 | CHANGED | rc=0 >>
aaa.sh
abc.txt
cdd.jpg
[root@test test] ansible testnode -m command -a "creates='/tmp/an-test/aaa.sh' ls" ####如果/tmp/an-test/aaa.sh存在,则不执行“ls”命令
node2 | SUCCESS | rc=0 >>
skipped, since /tmp/an-test/aaa.sh exists
[root@test test] ansible testnode -m command -a "chdir='/tmp/an-test/' echo 'hello' > aaa.sh"
node2 | CHANGED | rc=0 >>
hello > aaa.sh
[root@test test] ansible testnode -m shell -a " echo 'hello' > /tmp/an-test/aaa.sh" ##使用shell写入'hello' 到文件中
node2 | CHANGED | rc=0 >>
[root@test test] ansible testnode -m command -a " chdir='/tmp/an-test/' removes='/tmp/an-test/aaa.sh' cat aaa.sh" ##判断文件不存在就不执行命令,现在文件存在所以执行cat aaa.sh 命令
node2 | CHANGED | rc=0 >>
hello
3、 shell模块:在远程主机上调用shell解释器运行命令,支持shell的各种功能,例如管道等
[root@test test] ansible testnode -m shell -a "ps aux|grep nfs"
node2 | CHANGED | rc=0 >>
root 6222 0.0 0.0 0 0 ? S< Jun10 0:00 [nfsiod]
root 6478 0.0 0.0 0 0 ? S Jun10 0:00 [nfsv4.1-svc]
root 65432 0.0 0.0 113288 1212 pts/0 S+ 19:36 0:00 /bin/sh -c ps aux|grep nfs
root 65434 0.0 0.0 112820 960 pts/0 S+ 19:36 0:00 grep nfs
4、copy模块: 这个模块用于将文件复制到远程主机,同时支持给定内容生成文件和修改权限等。
常用参数:
src #被复制到远程主机的本地文件。可以是绝对路径,也可以是相对路径。如果路径是一个目录,则会递归复制,用法类似于"rsync"
content #用于替换"src",可以直接指定文件的值
dest #必选项,将源文件复制到的远程主机的绝对路径
backup #当文件内容发生改变后,在覆盖之前把源文件备份,备份文件包含时间信息
directory_mode #递归设定目录的权限,默认为系统默认权限
force #当目标主机包含该文件,但内容不同时,设为"yes",表示强制覆盖;设为"no",表示目标主机的目标位置不存在该文件才复制。默认为"yes"
others #所有的 file 模块中的选项可以在这里使用
[root@test ~] ansible testnode -m copy -a "src='/root/sleep.sh' dest='/tmp/an-test' "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "3e08400e4a3c47f34fda071a9482644885817555",
"dest": "/tmp/an-test/sleep.sh",
"gid": 0,
"group": "root",
"md5sum": "c2cc2cd03ea9f48514c49ac24a962f5a",
"mode": "0644",
"owner": "root",
"size": 22,
"src": "/root/.ansible/tmp/ansible-tmp-1763293395.99-10087-8627623535488/source",
"state": "file",
"uid": 0
}
####给定内容生成文件,并制定权限
[root@test ~] ansible testnode -m copy -a "content='This is ansible test' dest='/tmp/an-test/test.sh' mode=600 "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"checksum": "4a6dd7c9cf0f4119f12a8e788881919a4e381fdb",
"dest": "/tmp/an-test/test.sh",
"gid": 0,
"group": "root",
"md5sum": "f76c0c431732e248e6ccdb40c605bca4",
"mode": "0600",
"owner": "root",
"size": 20,
"src": "/root/.ansible/tmp/ansible-tmp-1763293564.61-10776-147919066213180/source",
"state": "file",
"uid": 0
}
[root@test ~] ansible testnode -m command -a "ls -l /tmp/an-test/ "
node2 | CHANGED | rc=0 >>
total 12
-rw-r--r-- 1 root root 6 Nov 16 19:30 aaa.sh
-rw-r--r-- 1 root root 0 Nov 16 19:24 abc.txt
-rw-r--r-- 1 root root 0 Nov 16 19:24 cdd.jpg
-rw-r--r-- 1 root root 22 Nov 16 19:43 sleep.sh
-rw------- 1 root root 20 Nov 16 19:46 test.sh
####把文件的内容修改一下,然后选择覆盖备份:
[root@test ~] ansible testnode -m copy -a "content='This is ansible test 2' dest='/tmp/an-test/test.sh' backup='yes' mode=770 "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"backup_file": "/tmp/an-test/test.sh.68669.2025-11-16@19:49:31~",
"changed": true,
"checksum": "fb7e0fcadbff5ae75c4422be8beda23b8d2f0c70",
"dest": "/tmp/an-test/test.sh",
"gid": 0,
"group": "root",
"md5sum": "3e507ddf19c9ebb6b376e5cd5d5eb88f",
"mode": "0770",
"owner": "root",
"size": 22,
"src": "/root/.ansible/tmp/ansible-tmp-1763293770.46-11819-139498383329418/source",
"state": "file",
"uid": 0
}
[root@test ~] ansible testnode -m command -a "ls -l /tmp/an-test/ "
node2 | CHANGED | rc=0 >>
total 16
-rw-r--r-- 1 root root 6 Nov 16 19:30 aaa.sh
-rw-r--r-- 1 root root 0 Nov 16 19:24 abc.txt
-rw-r--r-- 1 root root 0 Nov 16 19:24 cdd.jpg
-rw-r--r-- 1 root root 22 Nov 16 19:43 sleep.sh
-rwxrwx--- 1 root root 22 Nov 16 19:49 test.sh
-rw------- 1 root root 21 Nov 16 19:49 test.sh.68669.2025-11-16@19:49:31~
5、**file模块:**主要用于设置文件的属性,比如创建文件、创建链接文件、删除文件等
常用参数:
path #指定要创建的目录或者文件
force #需要在两种情况下强制创建软链接,一种是源文件不存在,但之后会建立的情况下;另一种是目标软链接已存在,需要先取消之前的软链,然后创建新的软链,有两个选项:yes|no
group #定义文件/目录的属组。后面可以加上mode:定义文件/目录的权限
owner #定义文件/目录的属主。后面必须跟上path:定义文件/目录的路径
recurse #递归设置文件的属性,只对目录有效,后面跟上src:被链接的源文件路径,只应用于state=link的情况
dest #被链接到的路径,只应用于state=link的情况
state #状态,有以下选项:
directory:如果目录不存在,就创建目录
file:即使文件不存在,也不会被创建
link:创建软链接
hard:创建硬链接
touch:如果文件不存在,则会创建一个新的文件,如果文件或目录已存在,则更新其最后修改时间
absent:删除目录、文件或者取消链接文件
[root@test ~] ansible testnode -m file -a "path='/tmp/an-test/bak1/bak2' state='directory' " ###创建目录
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 0,
"group": "root",
"mode": "0755",
"owner": "root",
"path": "/tmp/an-test/bak1/bak2",
"size": 4096,
"state": "directory",
"uid": 0
}
[root@test ~]# ansible testnode -m file -a "path='/tmp/an-test/abc.sh' src='/tmp/an-test/aaa.sh' state='link' " ###创建软连接
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"dest": "/tmp/an-test/abc.sh",
"gid": 0,
"group": "root",
"mode": "0777",
"owner": "root",
"size": 19,
"src": "/tmp/an-test/aaa.sh",
"state": "link",
"uid": 0
}
[root@test ~]# ansible testnode -m command -a "ls -l /tmp/an-test/ "
node2 | CHANGED | rc=0 >>
total 20
-rw-r--r-- 1 root root 6 Nov 16 19:30 aaa.sh
lrwxrwxrwx 1 root root 19 Nov 16 19:56 abc.sh -> /tmp/an-test/aaa.sh
-rw-r--r-- 1 root root 0 Nov 16 19:24 abc.txt
drwxr-xr-x 3 root root 4096 Nov 16 19:55 bak1
-rw-r--r-- 1 root root 0 Nov 16 19:24 cdd.jpg
-rw-r--r-- 1 root root 22 Nov 16 19:43 sleep.sh
-rwxrwx--- 1 root root 22 Nov 16 19:49 test.sh
-rw------- 1 root root 21 Nov 16 19:49 test.sh.68669.2025-11-16@19:49:31~
[root@test ~] ansible testnode -m file -a "path='/tmp/an-test/abc.sh' state='absent' " ###删除文件
nodeA100-2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"path": "/tmp/an-test/abc.sh",
"state": "absent"
}
6、yum模块:该模块主要用于软件的安装。
主要参数:
name= #所安装的包的名称
state= #present--->安装, latest--->安装最新的, absent---> 卸载软件。
update_cache #强制更新yum的缓存
conf_file #指定远程yum安装时所依赖的配置文件(安装本地已有的包)。
disable_pgp_check #是否禁止GPG checking,只用于presentor latest。
disablerepo #临时禁止使用yum库。 只用于安装或更新时。
enablerepo #临时使用的yum库。只用于安装或更新时。
示例
[root@test ~] ansible testnode -m yum -a "name='nginx' state='absent' " ##因为之前装过所以先卸载nginx
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"removed": [
"nginx"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
nginx x86_64 1:1.20.1-10.el7 @epel 1.7 M
Transaction Summary
================================================================================
Remove 1 Package
Installed size: 1.7 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : 1:nginx-1.20.1-10.el7.x86_64 1/1
Verifying : 1:nginx-1.20.1-10.el7.x86_64 1/1
Removed:
nginx.x86_64 1:1.20.1-10.el7
Complete!
"
]
}
[root@test ~] ansible testnode -m yum -a "name='nginx' state='present' " ##安装nginx
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"changes": {
"installed": [
"nginx"
]
},
"msg": "",
"rc": 0,
"results": [
"Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-10.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
nginx x86_64 1:1.20.1-10.el7 epel 588 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 588 k
Installed size: 1.7 M
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : 1:nginx-1.20.1-10.el7.x86_64 1/1
Verifying : 1:nginx-1.20.1-10.el7.x86_64 1/1
Installed:
nginx.x86_64 1:1.20.1-10.el7
Complete!
"
]
}
7、service 模块: 用于服务程序的管理
主要参数:
arguments #命令行提供额外的参数
enabled #设置开机启动。
name= #服务名称
runlevel #开机启动的级别,一般不用指定。
sleep #在重启服务的过程中,是否等待。如在服务关闭以后等待2秒再启动。(定义在剧本中。)
state #有四种状态,分别为:started--->启动服务, stopped--->停止服务, restarted--->重启服务, reloaded--->重载配置
示例:
[root@test ~]# ansible testnode -m service -a "name='nginx' state='started' "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "nginx",
"state": "started",
"status": {
.......
[root@test ~]# ansible testnode -m shell -a "ss -tnl|grep 80 "
node2 | CHANGED | rc=0 >>
LISTEN 0 128 *:800 *:*
LISTEN 0 128 *:80 *:*
LISTEN 0 128 [::]:80 [::]:*
[root@test ~]# ansible testnode -m service -a "name='nginx' state='stopped' "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "nginx",
"state": "stopped",
"status": {
.......
[root@test ~] ansible testnode -m shell -a "ss -tnl|grep 80 "
node2 | CHANGED | rc=0 >>
LISTEN 0 128 *:800 *:*
8、user模块: 主要是用来管理用户账号。
comment # 用户的描述信息
createhome # 是否创建家目录
force # 在使用state=absent时, 行为与userdel –force一致.
group # 指定基本组
groups # 指定附加组,如果指定为(groups=)表示删除所有组
home # 指定用户家目录
move_home # 如果设置为home=时, 试图将用户主目录移动到指定的目录
name # 指定用户名
non_unique # 该选项允许改变非唯一的用户ID值
password # 指定用户密码
remove # 在使用state=absent时, 行为是与userdel –remove一致
shell # 指定默认shell
state # 设置帐号状态,不指定为创建,指定值为absent表示删除
system # 当创建一个用户,设置这个用户是系统用户。这个设置不能更改现有用户
uid # 指定用户的uid
示例:
[root@test ~] ansible testnode -m user -a "name='an-test' createhome='no' shell='/sbin/nologin' uid=9988 groups='GB' comment='This ansible test user' " ####创建用户,指定不创建home目录,用户id、属组、用户说明、shell
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"comment": "This ansible test user",
"create_home": false,
"group": 9988,
"groups": "GB",
"home": "/home/an-test",
"name": "an-test",
"shell": "/sbin/nologin",
"state": "present",
"system": false,
"uid": 9988
}
[root@test ~] ansible testnode -a "id an-test"
node2 | CHANGED | rc=0 >>
uid=9988(an-test) gid=9988(an-test) groups=9988(an-test),9311(GB)
[root@lrtm ~] ansible testnode -a "grep an-test /etc/passwd"
node2 | CHANGED | rc=0 >>
an-test:x:9988:9988:This ansible test user:/home/an-test:/sbin/nologin
[root@test ~] ansible testnode -m user -a "name='an-test' state='absent' force='yes' remove='yes' " #删除用户
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"force": true,
"name": "an-test",
"remove": true,
"state": "absent",
"stderr": "userdel: an-test home directory (/home/an-test) not found
",
"stderr_lines": [
"userdel: an-test home directory (/home/an-test) not found"
]
}
[root@test ~]# ansible testnode -a "id an-test"
node2 | FAILED | rc=1 >>
id: an-test: no such usernon-zero return code
[root@test ~]# ansible testnode -a "grep an-test /etc/passwd"
node2 | FAILED | rc=1 >>
non-zero return code
9、group模块 主要用于添加或删除组。
常用参数:
gid= #设置组的GID号
name= #指定组的名称
state= #指定组的状态,默认为创建,设置值为absent为删除
system= #设置值为yes,表示创建为系统组
示例:
[root@test ~]# ansible testnode -m group -a "name='an-test' gid='9988' "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"gid": 9988,
"name": "an-test",
"state": "present",
"system": false
}
[root@test ~]# ansible testnode -m command -a "grep an-test /etc/group "
node2 | CHANGED | rc=0 >>
an-test:x:9988:
[root@test ~]# ansible testnode -m group -a "name='an-test' state='absent' "
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"name": "an-test",
"state": "absent"
}
[root@test ~]# ansible testnode -m command -a "grep an-test /etc/group "
node2 | FAILED | rc=1 >>
non-zero return code
script模块 用于将本机的脚本在被管理端的机器上运行
###写一个测试脚本
[root@test ~]# vim /tmp/test.sh
[root@test ~]# cat /tmp/test.sh
#!/bin/bash
free -h >> /tmp/test.txt
[root@test ~]# ansible testnode -m script -a "/tmp/test.sh"
node2 | CHANGED => {
"changed": true,
"rc": 0,
"stderr": "Shared connection to nodea100-2 closed.
",
"stderr_lines": [
"Shared connection to nodea100-2 closed."
],
"stdout": "",
"stdout_lines": []
}
[root@test ~]# ansible testnode -m command -a "cat /tmp/test.txt"
node2 | CHANGED | rc=0 >>
total used free shared buff/cache available
Mem: 10.0G 9.1G 1G 4.8M 9G 9G
Swap: 0B 0B 0B
11、fetch 模块: 用于从远程某主机获取(复制)文件到本地。文件保存的路径是我们设置的接收目录下的被管制主机ip目录下:
常用参数:
dest:用来存放文件的目录
src:在远程拉取的文件,并且必须是一个file,不能是目录
[root@test ~]# ansible testnode -m fetch -a "src=/tmp/test.txt dest='/tmp/' "
node2 | CHANGED => {
"changed": true,
"checksum": "d7ee18f40d13bc117db2a1b6f1d63713cfdfb287",
"dest": "/tmp/node2/tmp/test.txt",
"md5sum": "9db5f1bc6e2df1db0885f1ffc723c9ad",
"remote_checksum": "d7ee18f40d13bc117db2a1b6f1d63713cfdfb287",
"remote_md5sum": null
}
12、cron模块: 用于管理cron计划任务的。
参数说明:
day= #日应该运行的工作( 1-31, *, */2, )
hour= # 小时 ( 0-23, *, */2, )
minute= #分钟( 0-59, *, */2, )
month= # 月( 1-12, *, /2, )
weekday= # 周 ( 0-6 for Sunday-Saturday,, )
job= #指明运行的命令是什么
name= #定时任务描述
reboot # 任务在重启时运行,不建议使用,建议使用special_time
special_time #特殊的时间范围,参数:reboot(重启时),annually(每年),monthly(每月),weekly(每周),daily(每天),hourly(每小时)
state #指定状态,present表示添加定时任务,也是默认设置,absent表示删除定时任务
user # 以哪个用户的身份执行
[root@test ~] ansible testnode -m cron -a 'name="get date every 5 min" minute=*/5 job="/bin/date &> /dev/null"' ##创建定时任务
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"get date every 5 min"
]
}
[root@test ~] ansible testnode -m cron -a 'name="get df -h everyday" hour=12 job="df -h &> /dev/null"' ##创建定时任务
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"get date every 5 min",
"get df -h everyday"
]
}
[root@test ~] ansible testnode -m command -a "crontab -l" #查看定时任务
node2 | CHANGED | rc=0 >>
#Ansible: get date every 5 min
*/5 * * * * /bin/date &> /dev/null
#Ansible: get df -h everyday
* 12 * * * df -h &> /dev/null
[root@test ~] ansible testnode -m cron -a 'name="get date every 5 min" state='absent'' ##删除定时任务
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": [
"get date every 5 min"
]
}
[root@test ~] ansible testnode -m cron -a 'name="get df -h everyday" state='absent'' #删除定时任务
node2 | CHANGED => {
"ansible_facts": {
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": true,
"envs": [],
"jobs": []
}
13、setup模块: 主要用于收集信息,是通过调用facts组件来实现的。
facts组件是Ansible用于采集被管机器设备信息的一个功能,我们可以使用setup模块查机器的所有facts信息,可以使用filter来查看指定信息。整个facts信息被包装在一个JSON格式的数据结构中,ansible_facts是最上层的值。
facts就是变量,内建变量 。每个主机的各种信息,cpu颗数、内存大小等。会存在facts中的某个变量中。调用后返回很多对应主机的信息,在后面的操作中可以根据不同的信息来做不同的操作。
通过filter参数限制收集范围,例如仅获取网络信息(filter=ansible_network*)或内存信息(filter=ansible_memtotal_mb) 。
支持gather_subset参数指定子集(如min最小信息、network,hardware网络和硬件信息)
示例:
[root@test ~]# ansible testnode -m setup -a 'filter='*cpu*''
node2 | SUCCESS => {
"ansible_facts": {
"ansible_processor_vcpus": 128,
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@test ~] ansible testnode -m setup -a "filter='*nodename'" #获取节点名称
node2 | SUCCESS => {
"ansible_facts": {
"ansible_nodename": "node2",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@test ~] ansible testnode -m setup -a "filter='*pkg_mgr'" ##获取包管理器
node2 | SUCCESS => {
"ansible_facts": {
"ansible_pkg_mgr": "yum",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
[root@test ~] ansible testnode -m setup -a "filter='ansible_kernel'" ##获取内核版本
node2 | SUCCESS => {
"ansible_facts": {
"ansible_kernel": "3.10.0-1062.18.1.el7.x86_64",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}