Module 5: Ansible Assignment - 4
Tasks To Be Performed:
- Use the previous deployment of Ansible cluster
- Configure the files folder in the role with index.html which should be replaced with the original index.html
All of the above should only happen on the slave which has NGINX installed using the role.
Using the role structure from Assignment 3 β Ansible
Step 1: Configure files/index.html
I place the index.html
file that is to be deployed into the files
directory within the role.
index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Ansible Deployment</title>
</head>
<body>
<div class="header">
<h1>Deployed from Ansible</h1>
</div>
</body>
</html>
ubuntu@ip-10-0-1-223:~/nginx_role$ tree
.
βββ README.md
βββ defaults
β βββ main.yml
βββ files
β βββ index.html #newly created
βββ handlers
β βββ main.yml
βββ meta
β βββ main.yml
βββ tasks
β βββ main.yml
βββ tests
β βββ inventory
β βββ test.yml
βββ vars
βββ main.yml
7 directories, 9 files
Step 2: Update the Roleβs tasks/main.yml
I edit the tasks/main.yml
file to incorporate the task that will substitute the existing index.html
file.
---
# tasks file for nginx_setup
- name: Gather the NGINX service facts
service_facts: #Condition fails if not present
- name: Replace index.html with the custom one
ansible.builtin.copy:
src: index.html
dest: /var/www/html/index.nginx-debian.html
owner: root
group: root
mode: '0644'
when: ansible_facts.services["nginx.service"] is defined and ansible_facts.services["nginx.service"].state == "running"
notify: reload nginx
And the corresponding handler remains the same:
---
# handlers file for nginx_setup
- name: reload nginx
ansible.builtin.service:
name: nginx
state: reloaded
Iβll run the same playbook from Assignment 3 β Ansible
---
- hosts: slave1
become: yes
roles:
- apache_role
- hosts: slave2
become: yes
roles:
- nginx_role
%%Dry run ansible-playbook -i inventory.ini install_web_servers.yml --check
%%
ansible-playbook -i inventory.ini install_web_servers.yml
From the terminal on Slave2 EC2, I utilize the text-based web browser Lynx to confirm the successful deployment.
lynx localhost
Success