12. 利用playbook生成所有清单主机指定硬件信息报告

创建一个名为 /home/student/ansible/hwreport.yml 的 playbook,
它将在所有受管节点上生成含有以下信息的输出文件 /root/hwreport.txt:


任务要求
输出文件 /root/hwreport.txt
hostname: inventoryhostname
mem: memory_in_MB
bios: BIOS_version
vda: disk_vda_size
vdb: disk_vdb_size
输出文件中的每一行含有一个 key=value 对。
playbook 应当从 http://content.example.com/fifiles/hwreport.empty 下载文件,并将它保存为/root/hwreport.txt
使用正确的值修改 /root/hwreport.txt
如果硬件项不存在,相关的值应设为NONE
 

[student@workstation ansible]$ vim hwreport.yml
---
- name: Get hwreport info 
  hosts: all
  tasks:
    - name: Create report file 
      get_url:
        url: http://content.example.com/files/hwreport.empty 
        dest: /root/hwreport.txt
    - name: Get inventory_hostname 
      replace:
        path: /root/hwreport.txt 
        regexp: 'inventoryhostname'
        replace: '{{ inventory_hostname }}'
    - name: Get memory total size 
      replace:
        path: /root/hwreport.txt 
        regexp: 'memory_in_MB'
        replace: "{{ ansible_memtotal_mb | string}}" ## 内存大小是个数值,替换成字符串进行输出
    - name: Get bios version 
      replace:
        path: /root/hwreport.txt 
        regexp: 'BIOS_version'
        replace: "{{ ansible_bios_version }}"
    - name: Get disk vda size 
      replace:
        path: /root/hwreport.txt 
        regexp: 'disk_vda_size'
        replace: "{{ ansible_devices.vda.size if ansible_devices.vda is defined else 'NONE' }}" ## 对变量是否存在进行条件判断
    - name: Get disk vdb size 
      replace:
      path: /root/hwreport.txt 
      regexp: 'disk_vdb_size'
      replace: "{{ ansible_devices.vdb.size if ansible_devices.vdb is defined else 'NONE' }}"
[student@workstation ansible]$ ansible-playbook hwreport.yml

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

晚睡晚起秦叁叁

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值