RHCE 9.0 Practice Exam: Generating a Comprehensive Hardware Report Using Ansible
π 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
- Create an Ansible playbook named
/home/greg/ansible/hwreport.yml. - Run the playbook on all managed nodes.
- 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 toNONEif missing) - Default IPv4 Address (
IPV4=) - Kernel Version (
KERNEL=) - OS Release Version (
OS_RELEASE=)
- Inventory Hostname (
- If a hardware item does not exist, set its value to
NONE. - Download a template (
hwreport.empty) fromhttp://classroom/materials/hwreport.emptyand 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)β EnsuresNONEis 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
NONEif 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
vdaandvdbexist; if missing, they default toNONE.
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! π₯