红帽 RHCE8 认证考试:生成硬件报告

题目描述

创建一个名为 /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.

详细解析

本题需要下载文件,因此需要用到 get_url 模块,由于还需要替换 hwreport.txt 文件中的内容,因此还需要用到 replace 模块。此外,由于题目要求不存在的硬件项对应的值需要设置为 NONE, 因此,我们还需要用到 when 关键字。

需要特别注意的是,本题的 playbook 中涉及许多 Ansible 主机变量,我们需要准确记住这些变量的写法。

我们可以在做题之前,先找一个与题目无关的目录,用 wget 命令将题目中给出来的 hwreport.empty 文件下载下来,之后,使用 cat 命令查看一下其中的内容,这样我们就可以对之后需要替换哪些部分有一个了解。

在编写本文时,我所使用的考试模拟环境中,hwreport.empty 文件中的内容如下:

# Hardware report
HOST=inventoryhostname
MEMORY=memory_in_MB
BIOS=BIOS_version
DISK_SIZE_VDA=disk_vda_size
DISK_SIZE_VDB=disk_vdb_size

接着,根据题目要求创建并编辑如下 playbook:

vim /home/greg/ansible/hwreport.yml

本题中的 playbook 全文如下:

---
- name:
  hosts: all
  tasks:
    - name:
      get_url:
        url: http://materials/hwreport.empty
        dest: /root/hwreport.txt
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "inventoryhostname"
        replace: "{{ ansible_hostname }}"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "memory_in_MB"
        replace: "{{ ansible_memory_mb }}"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "BIOS_version"
        replace: "{{ ansible_bios_version }}"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vda_size"
        replace: "{{ ansible_devices.vda.size }}"
      when: "'vda' in ansible_devices"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vda_size"
        replace: "NONE"
      when: "'vda' not in ansible_devices"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vdb_size"
        replace: "ansible_devices.vdb.size"
      when: "'vdb' in ansible_devices"
    - name:
      replace:
        path: /root/hwreport.txt
        regexp: "disk_vdb_size"
        replace: "NODE"
      when: "'vdb' not in ansible_devices"

运行上面的 playbook:

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

测试 playbook 的执行结果:

ansible all -m command -a "cat /root/hwreport.txt"

荒原之梦网全部内容均为原创,提供了涵盖考研数学基础知识、考研数学真题、考研数学练习题和计算机科学等方面,大量精心研发的学习资源。

意见反馈 | 内容纠错 | 微信 | QQ | 公众号 | 知乎 | 微博 | 博客园 |CSDN | B 站 | 电子邮件
豫 ICP 备 17023611 号-1 | 公网安备 - 荒原之梦 豫公网安备 41142502000132 号 | SiteMap
Copyright © 2017-2024 ZhaoKaifeng.com 版权所有 All Rights Reserved.

Copyright © 2024   zhaokaifeng.com   All Rights Reserved.
豫ICP备17023611号-1
 豫公网安备41142502000132号

荒原之梦 自豪地采用WordPress