RHCE 9.0 Practice Exam: Generating the /etc/myhosts File Using Ansible Templates

RHCE 9.0 Practice Exam: Generating the /etc/myhosts File Using Ansible Templates
Photo by Florian Krumm / Unsplash

πŸ“Œ 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

  1. Download the required template and playbook:
    • hosts.j2 from http://classroom/materials/hosts.j2
    • hosts.yml from http://classroom/materials/hosts.yml
  2. Modify hosts.j2 to generate a /etc/myhosts file containing:
    • A default localhost entry
  3. Run hosts.yml to generate the /etc/myhosts file on all nodes in the dev host 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_hostname as 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.j2 template to generate /etc/myhosts.
  • Runs only on nodes in the dev group (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 dev group:
$ 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! πŸ”₯

Read more