RHCE 9.0 Practice Exam: Generating a Comprehensive Hardware Report Using Ansible

Expertise in Cloud, Networking & DevOps
Photo by Barez Omer / Unsplash

πŸ“Œ Introduction

In this RHCE 9.0 Ansible exam practice guide, we will automate system information collection using Ansible. The playbook will gather hardware details such as memory size, BIOS version, disk sizes, and hostnames, and store them in a structured hardware report (/root/hwreport.txt) on all managed nodes.

This guide includes:

  • Detailed explanations of the playbook structure
  • Step-by-step command breakdown
  • Error handling mechanisms
  • Common troubleshooting solutions
  • Real-world use cases of system automation

πŸ’‘ What You Will Learn

βœ… How to collect hardware information using Ansible facts (setup module)
βœ… How to structure reports dynamically with lineinfile
βœ… How to download and initialize a report using get_url
βœ… How to verify and troubleshoot the generated report
βœ… How to handle missing system values in Ansible playbooks


πŸ“œ 1. Task Requirements

πŸ“ 1.1. Problem Statement

  1. Create an Ansible playbook named /home/greg/ansible/hwreport.yml.
  2. Run the playbook on all managed nodes.
  3. The report (/root/hwreport.txt) should include:
    • Inventory Hostname (HOST=)
    • Total Memory in MB (MEMORY=)
    • BIOS Version (BIOS=)
    • CPU Model (CPU_MODEL=)
    • CPU Core Count (CPU_CORES=)
    • Disk Size of vda (DISK_SIZE_VDA=)
    • Disk Size of vdb (DISK_SIZE_VDB=, defaults to NONE if missing)
    • Default IPv4 Address (IPV4=)
    • Kernel Version (KERNEL=)
    • OS Release Version (OS_RELEASE=)
  4. If a hardware item does not exist, set its value to NONE.
  5. Download a template (hwreport.empty) from http://classroom/materials/hwreport.empty and save it as /root/hwreport.txt.

πŸ“œ 2. Writing the Ansible Playbook

πŸ“ 2.1. Create the Playbook File

$ vim /home/greg/ansible/hwreport.yml

πŸ“„ Playbook Breakdown (Step-by-Step)

---
- name: Generate Hardware Report
  hosts: all
  become: yes
  tasks:

πŸ“Œ Explanation:

  • hosts: all β†’ Run the playbook on all managed nodes.
  • become: yes β†’ Runs the tasks as root (sudo).
  • tasks: β†’ Defines the list of operations to be performed.

πŸ“ 2.2. Download an Empty Report Template

    - name: Download empty report template
      ansible.builtin.get_url:
        url: http://classroom/materials/hwreport.empty
        dest: /root/hwreport.txt
        mode: '0644'

πŸ“Œ Explanation:

  • get_url β†’ Fetches the pre-existing empty report template.
  • dest β†’ Saves the file as /root/hwreport.txt.
  • mode: '0644' β†’ Ensures the file has appropriate permissions.

πŸ“ 2.3. Insert Hostname into Report

    - name: Report - Inventory Hostname
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^HOST='
        line: "HOST={{ inventory_hostname }}"

πŸ“Œ Explanation:

  • lineinfile β†’ Modifies the report dynamically.
  • inventory_hostname β†’ Retrieves the system’s hostname.

πŸ“ 2.4. Add Total Memory to Report

    - name: Report - Total Memory (MB)
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^MEMORY='
        line: "MEMORY={{ ansible_memtotal_mb | default('NONE', true) }}"

πŸ“Œ Explanation:

  • ansible_memtotal_mb β†’ Fetches total system memory in MB.
  • default('NONE', true) β†’ Ensures NONE is recorded if data is missing.

πŸ“ 2.5. Add BIOS Version

    - name: Report - BIOS Version
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^BIOS='
        line: "BIOS={{ ansible_bios_version | default('NONE', true) }}"

πŸ“Œ Explanation:

  • ansible_bios_version β†’ Retrieves the system’s BIOS version.

πŸ“ 2.6. Add CPU and Disk Size Information

    - name: Report - CPU Model
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^CPU_MODEL='
        line: "CPU_MODEL={{ ansible_processor[1] | default('NONE', true) }}"

    - name: Report - CPU Core Count
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^CPU_CORES='
        line: "CPU_CORES={{ ansible_processor_count | default('NONE', true) }}"

    - name: Report - Disk Size VDA
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^DISK_SIZE_VDA='
        line: "DISK_SIZE_VDA={{ ansible_devices.vda.size | default('NONE', true) }}"

    - name: Report - Disk Size VDB
      ansible.builtin.lineinfile:
        path: /root/hwreport.txt
        regexp: '^DISK_SIZE_VDB='
        line: "DISK_SIZE_VDB={{ ansible_devices.vdb.size | default('NONE', true) }}"

πŸ“Œ Explanation:

  • Adds CPU and disk size data dynamically.
  • Defaults to NONE if values are not available.

πŸ“œ 3. Common Issues & Troubleshooting

πŸ”΄ Issue 1: Missing System Information

βœ… Solution:

Refresh cache and retry:

$ ansible all -m setup --refresh-cache

Verify facts availability:

$ ansible all -m setup --tree /tmp/facts

πŸ”΄ Issue 2: Report File Not Created

βœ… Solution:

  • If missing, rerun the playbook.

Ensure the report file exists:

$ ansible all -m shell -a 'ls -l /root/hwreport.txt'

πŸ”΄ Issue 3: Incorrect or Missing Disk Size

βœ… Solution:

  • Ensure vda and vdb exist; if missing, they default to NONE.

Run:

$ ansible all -m setup -a 'filter=ansible_devices'

πŸš€ Congratulations! You have successfully automated system reporting using Ansible for RHCE 9.0! πŸš€

πŸ“’ If you found this guide helpful, share it with your RHCE 9.0 study group! πŸ“’

πŸ”₯ Good luck on your RHCE 9.0 exam! πŸ”₯

Read more