生成硬件报告
创建一个名为 /home/greg/ansible/hwreport.yml 的 playbook ,它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt :
清单主机名称
以 MB 表示的总内存大小
BIOS 版本
磁盘设备 vda 的大小
磁盘设备 vdb 的大小
输出文件中的每一行含有一个 key=value 对。
您的 playbook 应当:
从 http://materials/hwreport.empty 下载文件,并将它保存为 /root/hwreport.txt
使用正确的值改为 /root/hwreport.txt
如果硬件项不存在,相关的值应设为 NONE
#编写文件
vim /home/greg/ansible/webcontent.yml#编写playbook
---
- name: hwhosts: allvars:hw_all:- hw_name: HOST=hw_cont: "{{ inventory_hostname | default('NONE',true) }}"- hw_name: MEMORY=hw_cont: "{{ ansible_memtotal_mb | default('NONE',true) }}"- hw_name: BIOS=hw_cont: "{{ ansible_bios_version | default('NONE',true) }}"- hw_name: DISK_SIZE_VDA=hw_cont: "{{ ansible_devices.vda.size | default('NONE',true) }}"- hw_name: DISK_SIZE_VDB=hw_cont: "{{ ansible_devices.vdb.size | default('NONE',true) }}"tasks:- name: xiazaiget_url:url: http://materials/hwreport.emptydest: /root/hwreport.txt- name: xiugailineinfile:path: /root/hwreport.txtregexp: "^{{ item.hw_name }}"line: "{{ item.hw_name }}{{ item.hw_cont }}"loop: "{{ hw_all }}"#运行
ansible-playbook /home/greg/ansible/hwreport.yml#查看
ansible all -m shell -a 'cat /root/hwreport.txt'