RHCE 9.0 Practice Exam: Modifying the /etc/issue File Based on Host Groups Using Ansible
π 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
- Create a playbook named
/home/greg/ansible/issue.yml
. - Run the playbook on all managed hosts.
- 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:
- 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! π₯