如何是用Ansible中的"setup"模块
时间: 2024-04-28 19:27:10 浏览: 10
Ansible中的"setup"模块可以用来收集被控机器的系统信息,例如操作系统类型、内存使用情况、CPU信息等等。下面是使用"setup"模块的步骤:
1. 编写Inventory文件
Inventory文件是Ansible用来管理主机的清单文件,需要指定主机名和主机IP地址等信息。例如:
```
[webserver]
192.168.1.100
[database]
192.168.1.101
```
2. 编写Playbook文件
Playbook文件是Ansible用来执行任务的文件,需要指定要执行的任务,例如使用"setup"模块收集被控机器的系统信息。例如:
```
- hosts: webserver
tasks:
- name: Gather facts
setup:
```
3. 执行Playbook文件
执行Playbook文件需要使用ansible-playbook命令,例如:
```
ansible-playbook -i inventory_file playbook_file.yml
```
执行完毕后,可以在输出结果中看到被控机器的系统信息,例如:
```
"ansible_facts": {
"ansible_architecture": "x86_64",
"ansible_bios_date": "04/19/2019",
"ansible_bios_version": "1.0.0",
"ansible_cmdline": {
"BOOT_IMAGE": "/vmlinuz-4.4.0-47-generic",
"quiet": true,
"ro": true
},
"ansible_date_time": {
"date": "2021-09-29",
"day": "29",
"epoch": "1632914145",
"hour": "13",
"iso8601": "2021-09-29T13:02:25Z",
"iso8601_basic": "20210929T130225683138",
"iso8601_basic_short": "20210929T130225",
"iso8601_micro": "2021-09-29T13:02:25.683138Z",
"minute": "02",
"month": "09",
"second": "25",
"time": "13:02:25",
"tz": "UTC",
"tz_offset": "+0000",
"weekday": "Wednesday",
"weekday_number": "3",
"weeknumber": "39",
"year": "2021"
},
"ansible_default_ipv4": {
"address": "192.168.1.100",
"alias": "eth0",
"broadcast": "192.168.1.255",
"gateway": "192.168.1.1",
"interface": "eth0",
"macaddress": "00:0c:29:02:db:8c",
"mtu": 1500,
"netmask": "255.255.255.0",
"network": "192.168.1.0",
"type": "ether"
},
"ansible_devices": {
"sda": {
"holders": [],
"host": "",
"links": {
"ids": [],
"labels": [],
"masters": [],
"uuids": []
},
"model": "Virtual disk",
"partitions": {},
"removable": "0",
"rotational": "1",
"sas_address": null,
"sas_device_handle": null,
"scheduler_mode": "mq-deadline",
"sectors": "20971520",
"sectorsize": 512,
"size": "10.00 GB",
"support_discard": "0",
"vendor": "VMware",
"virtual": 1
}
},
"ansible_distribution": "Ubuntu",
"ansible_distribution_release": "16.04",
"ansible_distribution_version": "16.04",
"ansible_domain": "",
"ansible_env": {
"HOME": "/root",
"LANG": "en_US.UTF-8",
"LANGUAGE": "en_US",
"LC_ALL": "en_US.UTF-8",
"LESSCLOSE": "/usr/bin/lesspipe %s %s",
"LESSOPEN": "| /usr/bin/lesspipe %s",
"LOGNAME": "root",
"MAIL": "/var/mail/root",
"PATH": "/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games",
"PWD": "/root",
"SHELL": "/bin/bash",
"SHLVL": "1",
"TERM": "xterm-256color",
"USER": "root",
"USERNAME": "root"
},
"ansible_eth0": {
"active": true,
"device": "eth0",
"ipv4": {
"address": "192.168.1.100",
"broadcast": "192.168.1.255",
"netmask": "255.255.255.0",
"network": "192.168.1.0"
},
"ipv6": [
{
"address": "fe80::20c:29ff:fe02:db8c",
"prefix": "64",
"scope": "link"
}
],
"macaddress": "00:0c:29:02:db:8c",
"module": "vmxnet3",
"mtu": 1500,
"promisc": false,
"type": "ether"
}
}
```
以上就是使用Ansible中的"setup"模块的步骤。