RHCE之路--12生成硬件报告

352 阅读1分钟

本文已参与「新人创作礼」活动,一起开启掘金创作之路

RHCE之路--12生成硬件报告

1. 考题

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

  • 清单主机名称
  • 以 MB 表示的总内存大小
  • BIOS 版本
  • 磁盘设备 vda 的大小
  • 磁盘设备 vdb 的大小

输出文件中的每一行含有一个 key=value 对 您的 playbook 应当: 从 materials.example.com/cd/exam_rhc… 下载文件,并将它保存为 /root/hwreport.txt 使用正确的值改为 /root/hwreport.txt,如果硬件项不存在,相关的值应设为 NONE

2. 解题

vi  /home/student/ansible/hwreport.yml

2.1 方法1:

---
- name: 11
  hosts: all

  tasks:
    - name: get url
      get_url:
        url: http://materials.example.com/cd/exam_rhce8/hwreport.empty
        dest: /root/hwreport.txt
    - name: HOSTNAME
      lineinfile:
        path: /root/hwreport.txt
        regexp: '^HOSTNAME'
        line: HOSTNAME="{{ ansible_fqdn }}"
    - name: MEMORY
      lineinfile:
        path: /root/hwreport.txt
        regexp: '^MEMORY'
        line: MEMORY="{{ ansible_memtotal_mb }}"
    - name: BIOS_VERSION
      lineinfile:
        path: /root/hwreport.txt
        regexp: '^BIOS_VERSION'
        line: BIOS_VERSION="{{ ansible_bios_version }}"
    - name: VDASIZE
      lineinfile:
        path: /root/hwreport.txt
        regexp: '^VDASIZE'
        line: VDASIZE="{{ ansible_devices.vda.size }}"
    - name: VDBSIZE
      lineinfile:
        path: /root/hwreport.txt
        regexp: '^VDBSIZE'
        line: VDBSIZE="{{ ansible_devices.vdb.size | default('NONE',true) }}"

2.2 方法2:

---
- name: 11
  hosts: all
  vars:
    hw:
      - hw_key: HOSTNAME
        hw_val: "{{ ansible_fqdn }}"
      - hw_key: MEMORY
        hw_val: "{{ ansible_memtotal_mb }}"
      - hw_key: BIOS_VERSION
        hw_val: "{{ ansible_bios_version }}"
      - hw_key: VDASIZE
        hw_val: "{{ ansible_devices.vda.size }}"
      - hw_key: VDBSIZE
        hw_val: "{{ ansible_devices.vdb.size | default('NONE',true) }}"

  tasks:
    - name: get url
      get_url:
        url: http://materials.example.com/cd/exam_rhce8/hwreport.empty
        dest: /root/hwreport.txt
    - name: sed
      lineinfile:
        path: /root/hwreport.txt
        regexp: "^{{ item.hw_key }}"
        line: "{{ item.hw_key }}={{ item.hw_val }}"
      loop: "{{ hw }}"

3. 确认本题是否成功

ansible-playbook /home/student/ansible/hwreport.yml

image.png