之前写了一篇关于收集服务器硬件信息的博文,详见服务器硬件常用信息查询脚本 ,除使用脚本可以收集外,也可以使用ansible来实现资源收集。
准备ansible剧本
#cat collectinfo.yaml
- hosts: all
tasks:
- name: collect info
template: src=./info.j2 dest=/info.txt
- name: collect file
fetch: src=/info.txt dest=./collections/
- name: delete remote file
command: rm -f /info.txt
在当前目录下新建collections目录,用于存放收集回的信息
模板文件
hostname: {{ ansible_hostname }}
product name: {{ ansible_product_name }}
product serial: {{ ansible_product_serial }}
system: {{ ansible_distribution }} {{ ansible_distribution_version }}
kernel: {{ ansible_kernel }}
CPU count: {{ ansible_processor_count }} {{ ansible_processor_cores }} cores
total memory:{{ ansible_memtotal_mb }} MB
IP Address: {{ ansible_all_ipv4_addresses }}
执行脚本
# ansible-playbook collectinfo.yaml
执行后的文件目录
[root@node01 simple_book]# tree
.
├── collectinfo.yaml
├── collections
│ ├── node01
│ │ └── info.txt
│ ├── node02
│ │ └── info.txt
│ └── node03
│ └── info.txt
└── info.j2
验证:
# cat collections/node02/info.txt
hostname: node02
product name: VMware Virtual Platform
product serial: VMware-56 4d 43 50 c5 12 0d ae-28 bd 3b 60 89 6c fd f0
system: CentOS 7.4
kernel: 3.10.0-693.el7.x86_64
CPU count: 1 2 cores
total memory:3774 MB
IP Address: [u'172.16.0.12', u'192.168.23.12']