RHCE 9.0 Practice Exam: Creating and Using an Ansible Role for Apache Deployment
π 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
- Create an Ansible role named
apache
in/home/greg/ansible/roles
- 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 nodeIPADDRESS
β Managed nodeβs IP address
- π’
- 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.
- Runs on all hosts in the
π’ 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
) usingyum
. - 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 thewebservers
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! π₯