前言
关于ansible的setup模块和gather_facts功能的介绍。
正文
1. setup模块
这个模块是ansible的自带模块,可以收集机器的一些信息。
1.1 用法
ansible host -m setup -a "filter=xxx", 此filter可以筛选关键词。
[root@vm1 ~]# ansible localhost -m setup
localhost | SUCCESS => {
"ansible_facts": {
"ansible_all_ipv4_addresses": [
"192.168.1.106"
],
"ansible_all_ipv6_addresses": [
"fe80::7fc4:6b7a:c1f:851f"
],
"ansible_apparmor": {
"status": "disabled"
},
"ansible_architecture": "x86_64",
"ansible_bios_date": "05/18/2018",
"ansible_bios_version": "090007",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-3.10.0-693.el7.x86_64",
"LANG": "en_US.UTF-8",
"crashkernel": "auto",
"quiet": true,
"rd.lvm.lv": "centos/swap",
"rhgb": true,
"ro": true,
"root": "/dev/mapper/centos-root"
},
"ansible_date_time": {
"date": "2021-08-03",
"day": "03",
"epoch": "1627949682",
"hour": "08",
"iso8601": "2021-08-03T00:14:42Z",
"iso8601_basic": "20210803T081442627944",
"iso8601_basic_short": "20210803T081442",
"iso8601_micro": "2021-08-03T00:14:42.627944Z",
"minute": "14",
"month": "08",
"second": "42",
"time": "08:14:42",
"tz": "CST",
"tz_offset": "+0800",
"weekday": "星期二",
"weekday_number": "2",
"weeknumber": "31",
"year": "2021"
},
"ansible_default_ipv4": {
"address": "192.168.1.106",
"alias": "eth0",
"broadcast": "192.168.1.255",
"gateway": "192.168.1.3",
"interface": "eth0",
"macaddress": "00:15:5d:4a:01:04",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.1.0",
"type": "ether"
},
...
"ansible_distribution": "CentOS",
"ansible_distribution_file_parsed": true,
"ansible_distribution_file_path": "/etc/redhat-release",
"ansible_distribution_file_variety": "RedHat",
"ansible_distribution_major_version": "7",
"ansible_distribution_release": "Core",
"ansible_distribution_version": "7.4",
"ansible_dns": {
"nameservers": [
"192.168.1.3"
]
},
...
"module_setup": true
},
"changed": false
}
[root@vm1 ~]#
1.2 常用的关键词
ansible_all_ipv4_addresses #所有机器的ipv4地址
ansible_all_ipv6_addresses #所有机器的ipv6地址
ansible_date_time #系统时间
ansible_kernel #内核版本
ansible_default_ipv4 #默认机器的ipv4地址
ansible_default_ipv6 #默认机器的ipv6地址
ansible_distribution #linux系统发行版本,如centos,ubuntu,debian等
ansible_nodename #主机名
ansible_pkg_mgr #包管理器
ansible_python_version #python版本
2. gather_facts
在ansible-playbook中,又一个gather_facts功能,如果我们使用,即可让ansible去收集各机的信息。
开启gather_facts有利有弊,虽然可以看到更多机器的信息,但是也需要更多执行时间。
因此我们需要按照需求决定是否使用。
- hosts: whatever
gather_facts: no or yes #决定是否开启收集功能