1.5.20:setup 模块
官方文档:https://docs.ansible.com/ansible/latest/collections/ansible/builtin/setup_module.html#ansible-collections-ansible-builtin-setup-module
setup模块用于收集目标主机的系统信息(facts),获取到的facts信息都是变量的键值对,可以直接引用。
获取的信息较多,如果目标主机过多,会影响执行速度(在playbook中可以使用gather_facts: no来禁止Ansible收集facts信息)。
1.5.20.1:常用参数
setup模块本身的用法不复杂,主要是要熟悉facts信息。
| 参数 | 说明 |
|---|---|
| filter | 用于过滤收集到的facts信息; 使用shell风格的通配符进行匹配。 |
1.5.20.2:示例
查看目标主机的python版本:
[root@ansible ~]# ansible centos -m setup -a "filter=ansible_python_version"
192.168.1.203 | SUCCESS => {
"ansible_facts": {
"ansible_python_version": "2.7.5",
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
获取目标主机所有的ipv4地址:
[root@ansible ~]# ansible centos -m setup -a "filter=ansible_all_ipv4_addresses"
192.168.1.203 | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.203"
],
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
查看目标主机默认的ipv4地址信息:
[root@ansible ~]# ansible centos -m setup -a "filter=ansible_default_ipv4"
192.168.1.203 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "192.168.1.203",
"alias": "eth0",
"broadcast": "192.168.1.255",
"gateway": "192.168.1.1",
"interface": "eth0",
"macaddress": "00:0c:29:4a:8b:5e",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.1.0",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python"
},
"changed": false
}
本文介绍了Ansible的setup模块,如何用于收集目标主机的系统事实(facts),并展示了如何通过filter参数高效获取特定信息,如Python版本、IPv4地址等。
534

被折叠的 条评论
为什么被折叠?



