RHCE8 练习题:生成硬件报告

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

  • 清单主机名称
  • 以 MB 表示的总内存大小
  • BIOS 版本
  • 磁盘设备 vda 的大小
  • 磁盘设备 vdb 的大小
  • 输出文件中的每一行格式为 key=value

您的playbook应当:


Answer:

vim /home/student/ansible/hwreport.yml
---
- name: create hwreport
  hosts: all
  vars:
    hw_all:
      - hw_name: HOSTNAME
        hW_cont: "{{ ansible_hostname | default('NONE',true) }}"
      - hw_name: BIOS_VERSION
        hW_cont: "{{ ansible_bios_version | default('NONE',true) }}"
      - hw_name: MEMORY
        hW_cont: "{{ ansible_memtotal_mb | default('NONE',true) }}"
      - hw_name: VDASIZE
        hW_cont: "{{ ansible_devices.vda.size | default('NONE',true) }}"
      - hw_name: VDBSIZE
        hW_cont: "{{ ansible_devices.vdb.size | default('NONE',true) }}"
  tasks:
    - name: download file
      get_url:
        url: http://materials.example.com/cd/exam_rhce8/hwreport.empty
        dest: /root/hwreport.txt
    - name: modify hwreport file
      lineinfile:
        path: /root/hwreport.txt
        regexp: "^{{item.hw_name}}"
        line: "{{ item.hw_name }}={{ item.hw_cont }}"
      loop: "{{ hw_all }}"
ansible-playbook hwreport.yml

验证:

ansible all -a 'cat /root/hwreport.txt'

file