RHCE 9.0 Practice Exam: Modifying the /etc/issue File Based on Host Groups Using Ansible

Expertise in Cloud, Networking & DevOps
Photo by Jonathan / Unsplash

πŸ“Œ Article Overview

This tutorial is designed for RHCE 9.0 certification candidates and explains how to use Ansible to modify the /etc/issue file dynamically based on host group membership.

πŸ’‘ What You Will Learn:

βœ… How to modify files using Ansible’s copy module
βœ… How to apply conditional logic based on inventory groups
βœ… How to verify the correct content replacement in /etc/issue


πŸ“œ 1. Task Requirements

πŸ“ 1.1. Problem Statement

  1. Create a playbook named /home/greg/ansible/issue.yml.
  2. Run the playbook on all managed hosts.
  3. Modify /etc/issue with the following content based on group membership:

Hosts in the prod group should contain:

Production

Hosts in the test group should contain:

Test

Hosts in the dev group should contain:

Development

πŸ“œ 2. Gathering Information About Hosts

Before writing the playbook, verify the inventory group structure.

πŸ“ 2.1. Check Group Membership

$ ansible dev -m debug -a 'var=inventory_hostname'
$ ansible dev -m debug -a 'var=groups'
$ ansible dev -m debug -a 'var=groups.dev'

πŸ“Œ This ensures the hosts are correctly assigned to the dev, test, and prod groups.

πŸ“ 2.2. Check the File Module Documentation

$ ansible-doc copy
$ ansible-doc stat

πŸ“Œ This provides details on how to use Ansible's copy module to modify files.


πŸ“œ 3. Writing the Ansible Playbook

πŸ“ 3.1. Create the Playbook File

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

πŸ“„ Add the Following Content

---
- name: Modify /etc/issue based on host group
  hosts: all
  become: yes
  tasks:

    - name: Set /etc/issue for Development hosts
      ansible.builtin.copy:
        content: 'Development\n'
        dest: /etc/issue
        mode: '0644'
      when: inventory_hostname in groups['dev']

    - name: Set /etc/issue for Test hosts
      ansible.builtin.copy:
        content: 'Test\n'
        dest: /etc/issue
        mode: '0644'
      when: inventory_hostname in groups['test']

    - name: Set /etc/issue for Production hosts
      ansible.builtin.copy:
        content: 'Production\n'
        dest: /etc/issue
        mode: '0644'
      when: inventory_hostname in groups['prod']

πŸ“Œ Explanation:

  • Uses Ansible’s copy module to modify /etc/issue with the correct text.
  • Applies conditions (when:) to target specific groups (dev, test, prod).
  • Ensures proper file permissions (0644).

πŸ’Ύ Save and exit: (ESC β†’ :wq)


πŸ“œ 4. Running the Playbook

πŸ“ 4.1. Execute the Playbook

$ ansible-navigator run /home/greg/ansible/issue.yml -m stdout

πŸ“Œ Expected Output

PLAY [Modify /etc/issue based on host group] **********************************

TASK [Set /etc/issue for Development hosts] ***********************************
changed: [node1]
skipping: [node3]
skipping: [node5]

TASK [Set /etc/issue for Test hosts] *****************************************
skipping: [node1]
changed: [node3]
skipping: [node5]

TASK [Set /etc/issue for Production hosts] ************************************
skipping: [node1]
skipping: [node3]
changed: [node5]

PLAY RECAP ********************************************************************
node1 : ok=1 changed=1 unreachable=0 failed=0
node3 : ok=1 changed=1 unreachable=0 failed=0
node5 : ok=1 changed=1 unreachable=0 failed=0

βœ… Each host received the correct /etc/issue file based on its group!


πŸ“œ 5. Verifying /etc/issue Content

πŸ“ 5.1. Check File Content on All Hosts

$ ansible dev -a 'cat /etc/issue'
$ ansible test -a 'cat /etc/issue'
$ ansible prod -a 'cat /etc/issue'

πŸ“Œ Expected Output:

# For dev group:
Development

# For test group:
Test

# For prod group:
Production

βœ… Each host has the correct content in /etc/issue!


πŸ“œ 6. Common Issues & Troubleshooting

πŸ”΄ Issue 1: Some Hosts Did Not Receive the Correct /etc/issue

βœ… Solution:

Check if Ansible is correctly identifying the host groups:

$ ansible all -m debug -a 'var=groups'

Check if the affected hosts are assigned to the correct inventory group:

$ ansible-inventory --graph

πŸ”΄ Issue 2: /etc/issue Is Not Changing

βœ… Solution:

Check file permissions:

$ ls -l /etc/issue

If permissions are incorrect, rerun the playbook with become: yes.

Ensure the copy module is correctly writing to the file:

$ ansible all -m stat -a 'path=/etc/issue'

πŸ”΄ Issue 3: Playbook Fails with Permission Errors

βœ… Solution:

  1. Ensure become: yes is enabled in the playbook.

Manually check if Ansible has root privileges:

$ ansible all -m shell -a 'whoami'

If it returns root, permissions should be fine. If it returns another user, try running the playbook with:

$ ansible-playbook /home/greg/ansible/issue.yml --become

πŸš€ Congratulations! You have successfully used Ansible to modify the /etc/issue file dynamically based on host groups! πŸš€
πŸ“’ 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