RHCE 9.0 Practice Exam: Generating the /etc/myhosts File Using Ansible Templates
π Article Overview
This tutorial is designed for RHCE 9.0 certification candidates and explains how to use Ansible templates (Jinja2) to dynamically generate the /etc/myhosts file.
π‘ What You Will Learn:
β
How to use Ansible to generate /etc/myhosts dynamically from a template
β
How to work with Jinja2 loops in Ansible templates
β
How to use ansible_facts to retrieve host information dynamically
β
How to verify the generated file on managed nodes
π 1. Task Requirements
π 1.1. Problem Statement
- Download the required template and playbook:
hosts.j2fromhttp://classroom/materials/hosts.j2hosts.ymlfromhttp://classroom/materials/hosts.yml
- Modify
hosts.j2to generate a/etc/myhostsfile containing:- A default localhost entry
- Run
hosts.ymlto generate the/etc/myhostsfile on all nodes in thedevhost group.
After running the playbook, the /etc/myhosts file should contain:
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.250.9 node1.lab.example.com node1
172.25.250.10 node2.lab.example.com node2
172.25.250.11 node3.lab.example.com node3
172.25.250.12 node4.lab.example.com node4
172.25.250.13 node5.lab.example.com node5
An entry for each managed node in the format:
172.25.250.X nodeX.lab.example.com nodeX
π 2. Downloading the Required Files
π 2.1. Download hosts.j2 Template
$ wget -O /home/greg/ansible/hosts.j2 http://classroom/materials/hosts.j2
π This file serves as a Jinja2 template for generating the /etc/myhosts file dynamically.
π 2.2. Download hosts.yml Playbook
$ wget -O /home/greg/ansible/hosts.yml http://classroom/materials/hosts.yml
π This playbook is responsible for applying the template and generating the /etc/myhosts file.
π 3. Editing the hosts.j2 Template
π 3.1. Open the Template File
$ vim /home/greg/ansible/hosts.j2
π Modify the File to Include the Following Content
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
{% for i in groups['all'] %}
{{ hostvars[i].ansible_facts.default_ipv4.address }} {{ hostvars[i].ansible_facts.fqdn }} {{ hostvars[i].inventory_hostname }}
{% endfor %}
π Explanation:
- Loops through all inventory hosts (
groups['all']). - Extracts the IPv4 address (
ansible_facts.default_ipv4.address). - Retrieves the fully qualified domain name (
ansible_facts.fqdn). - Uses
inventory_hostnameas a shorthand host alias.
πΎ Save and exit: (ESC β :wq)
π 4. Editing the Ansible Playbook (hosts.yml)
π 4.1. Open the Playbook File
$ vim /home/greg/ansible/hosts.yml
π Add the Following Content
---
- name: Generate Host File
hosts: all
become: yes
tasks:
- name: Template a file to /etc/myhosts
ansible.builtin.template:
src: /home/greg/ansible/hosts.j2
dest: /etc/myhosts
mode: '0644'
when: inventory_hostname in groups['dev']
π Explanation:
- Applies the
hosts.j2template to generate/etc/myhosts. - Runs only on nodes in the
devgroup (when: inventory_hostname in groups['dev']). - Ensures the correct file permissions (
0644).
πΎ Save and exit: (ESC β :wq)
π 5. Running the Playbook
π 5.1. Execute the Playbook
$ ansible-navigator run /home/greg/ansible/hosts.yml -m stdout
π Expected Output
PLAY [Generate Host File] *****************************************************
TASK [Template a file to /etc/myhosts] ****************************************
changed: [node1]
changed: [node2]
changed: [node3]
changed: [node4]
changed: [node5]
PLAY RECAP ********************************************************************
node1 : ok=1 changed=1 unreachable=0 failed=0
node2 : ok=1 changed=1 unreachable=0 failed=0
node3 : ok=1 changed=1 unreachable=0 failed=0
node4 : ok=1 changed=1 unreachable=0 failed=0
node5 : ok=1 changed=1 unreachable=0 failed=0
β
All dev group nodes successfully generated the /etc/myhosts file!
π 6. Verifying the /etc/myhosts File
π 6.1. Check File Content
$ ansible dev -m shell -a 'cat /etc/myhosts'
π Expected Output
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4
::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
172.25.250.9 node1.lab.example.com node1
172.25.250.10 node2.lab.example.com node2
172.25.250.11 node3.lab.example.com node3
172.25.250.12 node4.lab.example.com node4
172.25.250.13 node5.lab.example.com node5
β
The /etc/myhosts file was correctly generated on all managed nodes!
π 7. Common Issues & Troubleshooting
π΄ Issue 1: Incorrect Host Entries in /etc/myhosts
β Solution:
- Check the inventory file to ensure all nodes are listed in
groups['all']:
$ ansible dev -m debug -a 'var=groups'
- Check that host variables contain correct IP addresses:
$ ansible dev -m debug -a 'var=hostvars[inventory_hostname]'
π΄ Issue 2: Playbook Skipped Some Hosts
β Solution:
- Ensure that the affected hosts are part of the
devgroup:
$ ansible dev --list-hosts
π΄ Issue 3: File Not Generated on Some Nodes
β Solution:
- Verify Ansible has write permissions:
$ ansible dev -m shell -a 'ls -l /etc/myhosts'
If permission errors occur, rerun with become: yes in the playbook.
π Congratulations! You have successfully used Ansible to generate a /etc/myhosts file dynamically using templates! π
π’ If you found this guide helpful, share it with your RHCE 9.0 study group! π’
π₯ Good luck on your RHCE 9.0 exam! π₯