Ansible的主机信息模块---setup
为什么要讲这个模块?
做过自动化的小伙伴会觉得这个模块非常实用
在公司中总会有一些需求
比如:
1.根据不同主机不同IP创建对应IP的目录
2.根据不同主机不同主机名创建对应主机名的目录
3.自动化运维平台需要自动获取到主机的IP地址,内存信息,磁盘信息,主机名...等
4.如果安装数据库,分配内存为物理内存的80%,此时有3台不同物理内存的机器2G、4G、16G
写一个playbook的情况下,我需要获取到对应主机的内存并作出计算,写判断。
ansible_all_ipv4_addresses:仅显示ipv4的信息。
ansible_devices:仅显示磁盘设备信息。
ansible_distribution:显示是什么系统,例:centos,suse等。
ansible_distribution_major_version:显示是系统主版本。
ansible_distribution_version:仅显示系统版本。
ansible_machine:显示系统类型,例:32位,还是64位。
ansible_eth0:仅显示eth0的信息。
ansible_hostname:仅显示主机名。
ansible_kernel:仅显示内核版本。
ansible_lvm:显示lvm相关信息。
ansible_memtotal_mb:显示系统总内存。
ansible_memfree_mb:显示可用系统内存。
ansible_memory_mb:详细显示内存情况。
ansible_swaptotal_mb:显示总的swap内存。
ansible_swapfree_mb:显示swap内存的可用内存。
ansible_mounts:显示系统磁盘挂载情况。
ansible_processor:显示cpu个数(具体显示每个cpu的型号)。
ansible_processor_vcpus:显示cpu个数(只显示总的个数)。
实操
#### 获取IP信息
ansible aaa01 -m setup -a 'filter=ansible_default_ipv4'
aaa01 | SUCCESS => {
"ansible_facts": {
"ansible_default_ipv4": {
"address": "10.0.0.101",
"alias": "ens33",
"broadcast": "10.0.0.255",
"gateway": "10.0.0.2",
"interface": "ens33",
"macaddress": "00:0c:29:e3:a0:c5",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "10.0.0.0",
"prefix": "24",
"type": "ether"
},
"discovered_interpreter_python": "/usr/bin/python3.12"
},
"changed": false
}
#### 获取主机名字
ansible all -m setup -a 'filter=ansible_fqdn'
[root@li ~]#ansible all -m setup -a 'filter=ansible_fqdn'
bbb01 | SUCCESS => {
"ansible_facts": {
"ansible_fqdn": "103",
"discovered_interpreter_python": "/usr/bin/python3.10"
},
"changed": false
}
#### 获取内存信息
ansible aaa01 -m setup -a 'filter=ansible_memory_mb'
ansible aaa01 -m setup -a 'filter=ansible_memory_mb'
aaa01 | SUCCESS => {
"ansible_facts": {
"ansible_memory_mb": {
"nocache": {
"free": 2809,
"used": 1059
},
"real": {
"free": 810,
"total": 3868,
"used": 3058
},
"swap": {
"cached": 0,
"free": 3867,
"total": 3867,
"used": 0
}
},
"discovered_interpreter_python": "/usr/bin/python3.12"
},
"changed": false
}
#### 获取磁盘信息
ansible aaa01 -m setup -a 'filter=ansible_devices'
[root@li ~]#ansible aaa01 -m setup -a 'filter=ansible_devices'
[WARNING]: Platform linux on host aaa01 is using the discovered Python interpreter at /usr/bin/python3.12,
but future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.17/reference_appendices/interpreter_discovery.html for more
information.
aaa01 | SUCCESS => {
"ansible_facts": {
"ansible_devices": {
"loop0": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "8",
"sectorsize": "512",
"size": "4.00 KB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop1": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "152112",
"sectorsize": "512",
"size": "74.27 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop10": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "1136",
"sectorsize": "512",
"size": "568.00 KB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop11": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "0",
"sectorsize": "512",
"size": "0.00 Bytes",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop2": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "152056",
"sectorsize": "512",
"size": "74.25 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop3": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "21952",
"sectorsize": "512",
"size": "10.72 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop4": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "1034424",
"sectorsize": "512",
"size": "505.09 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop5": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "552488",
"sectorsize": "512",
"size": "269.77 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop6": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "187776",
"sectorsize": "512",
"size": "91.69 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop7": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "21584",
"sectorsize": "512",
"size": "10.54 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop8": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "79520",
"sectorsize": "512",
"size": "38.83 MB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"loop9": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": null,
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "none",
"sectors": "1000",
"sectorsize": "512",
"size": "500.00 KB",
"support_discard": "4096",
"vendor": null,
"virtual": 1
},
"sda": {
"holders": [],
"host": "SCSI storage controller: Broadcom / LSI 53c1030 PCI-X Fusion-MPT Dual Ultra320 SCSI (rev 01)",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "VMware Virtual S",
"partitions": {
"sda1": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"sectors": "2048",
"sectorsize": 512,
"size": "1.00 MB",
"start": "2048",
"uuid": null
},
"sda2": {
"holders": [],
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": [
"e4f11015-3735-4b8a-a1c7-44008cf398f4"
]
},
"sectors": "209709056",
"sectorsize": 512,
"size": "100.00 GB",
"start": "4096",
"uuid": "e4f11015-3735-4b8a-a1c7-44008cf398f4"
}
},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-deadline",
"sectors": "209715200",
"sectorsize": "512",
"size": "100.00 GB",
"support_discard": "512",
"vendor": "VMware,",
"virtual": 1
},
"sr0": {
"holders": [],
"host": "SATA controller: VMware SATA AHCI controller",
"links": {
"ids": [
"ata-VMware_Virtual_SATA_CDRW_Drive_01000000000000000001"
],
"labels": [
"Ubuntu\\x2024.04.1\\x20LTS\\x20amd64"
],
"masters": [],
"uuids": [
"2024-08-27-16-23-26-00"
]
},
"model": "VMware SATA CD01",
"partitions": {},
"removable": "1",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-deadline",
"sectors": "12115928",
"sectorsize": "2048",
"size": "5.78 GB",
"support_discard": "2048",
"vendor": "NECVMWar",
"virtual": 1
}
},
"discovered_interpreter_python": "/usr/bin/python3.12"
},
"changed": false
}