RHCE 9.0 Practice Exam: Creating and Using an Ansible Role for Apache Deployment

RHCE 9.0 Practice Exam: Creating and Using an Ansible Role for Apache Deployment
Photo by Lukas / Unsplash

πŸ“Œ Article Overview

This tutorial is designed for RHCE 9.0 certification candidates and explains how to create and use an Ansible role to deploy Apache (httpd) on managed nodes.

πŸ’‘ What You Will Learn:

βœ… How to create an Ansible role using ansible-galaxy role init
βœ… How to configure Apache (httpd) and firewalld using Ansible
βœ… How to create and deploy an HTML template for a web page
βœ… How to run an Ansible Playbook that applies the role to managed nodes
βœ… How to verify that Apache is running and accessible


πŸ“œ 1. Task Requirements

πŸ“ 1.1. Problem Statement

  1. Create an Ansible role named apache in /home/greg/ansible/roles
  2. Ensure that:
    • 🟒 httpd is installed, enabled at system startup, and running.
    • 🟒 firewalld is enabled and running, with HTTP traffic allowed.
      • HOSTNAME β†’ Fully Qualified Domain Name (FQDN) of the managed node
      • IPADDRESS β†’ Managed node’s IP address
  3. Create a Playbook named /home/greg/ansible/apache.yml that:
    • Runs on all hosts in the webservers inventory group.
    • Uses the apache role to configure the nodes.

🟒 A template file (index.html.j2) generates a web page showing:

Welcome to HOSTNAME on IPADDRESS

where:


πŸ“œ 2. Creating the Apache Role

πŸ“ 2.1. Initialize the Role

On the control node (control), run:

$ ansible-galaxy role init --init-path /home/greg/ansible/roles apache

πŸ“Œ Expected Output

- Role apache was created successfully
- /home/greg/ansible/roles/apache/
  - defaults/
  - files/
  - handlers/
  - meta/
  - tasks/
  - templates/
  - tests/
  - vars/

βœ… The apache role structure is now created.


πŸ“œ 3. Defining Apache Tasks

πŸ“ 3.1. Edit tasks/main.yml

$ vim /home/greg/ansible/roles/apache/tasks/main.yml

πŸ“„ Add the following content:

---
- name: Install Apache
  ansible.builtin.yum:
    name: httpd
    state: latest

- name: Start Apache (httpd) service
  ansible.builtin.systemd:
    name: httpd
    state: started
    enabled: yes

- name: Ensure firewalld is installed and running
  ansible.builtin.systemd:
    name: firewalld
    state: started
    enabled: yes

- name: Allow HTTP service in firewalld
  ansible.posix.firewalld:
    service: http
    permanent: yes
    state: enabled
    immediate: yes

- name: Deploy index.html using a Jinja2 template
  ansible.builtin.template:
    src: index.html.j2
    dest: /var/www/html/index.html
    mode: '0644'

πŸ“Œ Key Points:

  • Installs Apache (httpd) using yum.
  • Starts and enables Apache on boot.
  • Ensures firewalld is enabled and running.
  • Allows HTTP traffic through the firewall.
  • Deploys the custom HTML page using a Jinja2 template.

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


πŸ“œ 4. Creating the HTML Template

πŸ“ 4.1. Create templates/index.html.j2

$ vim /home/greg/ansible/roles/apache/templates/index.html.j2

πŸ“„ Add the following content:

Welcome to {{ ansible_nodename }} on {{ ansible_default_ipv4.address }}

πŸ“Œ Explanation:

  • {{ ansible_nodename }} β†’ Captures the FQDN (Fully Qualified Domain Name).
  • {{ ansible_default_ipv4.address }} β†’ Captures the IP address of the managed node.

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


πŸ“œ 5. Creating the Ansible Playbook

πŸ“ 5.1. Create apache.yml

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

πŸ“„ Add the following content:

---
- name: Deploy Apache Web Server
  hosts: webservers
  roles:
    - apache

πŸ“Œ Explanation:

  • The playbook targets webservers (a group defined in the inventory).
  • The apache role is applied to all nodes in the webservers group.

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


πŸ“œ 6. Running the Playbook

πŸ“ 6.1. Execute the Playbook

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

πŸ“Œ Expected Output

PLAY [Deploy Apache Web Server] **********************************************

TASK [apache : Install Apache] ***********************************************
changed: [node3]
changed: [node4]

TASK [apache : Start Apache (httpd) service] *********************************
ok: [node3]
ok: [node4]

TASK [apache : Ensure firewalld is installed and running] ********************
ok: [node3]
ok: [node4]

TASK [apache : Allow HTTP service in firewalld] ******************************
changed: [node3]
changed: [node4]

TASK [apache : Deploy index.html using a Jinja2 template] ********************
changed: [node3]
changed: [node4]

PLAY RECAP ********************************************************************
node3       : ok=5    changed=3    unreachable=0    failed=0
node4       : ok=5    changed=3    unreachable=0    failed=0

βœ… Apache is now installed, started, and configured on node3 and node4.


πŸ“œ 7. Verifying the Apache Web Server

πŸ“ 7.1. Confirm Hosts in the webservers Group

$ ansible webservers --list-hosts

πŸ“Œ Expected Output

hosts (2):
  node3
  node4

πŸ“ 7.2. Test Webpage Accessibility

$ curl http://node3

πŸ“Œ Expected Output

Welcome to node3.lab.example.com on 172.25.250.11
$ curl http://node4

πŸ“Œ Expected Output

Welcome to node4.lab.example.com on 172.25.250.12

βœ… Apache is successfully serving the custom web page on both nodes!


πŸ“œ 8. Scoring Criteria

Step Description Score
1. Create the apache role Role exists in /home/greg/ansible/roles 5 points
2. Configure tasks/main.yml Installs Apache, configures firewalld 10 points
3. Create index.html.j2 template Generates a custom web page 10 points
4. Define apache.yml Playbook Deploys apache role to webservers 10 points
5. Verify Apache deployment Web page returns correct hostname & IP 10 points

βœ… Total Score: 45 points


πŸš€ Congratulations! You have successfully created, deployed, and verified an Ansible role for Apache in 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! πŸ”₯

Read more