Module 5: Ansible Assignment - 4

Tasks To Be Performed:

  1. Use the previous deployment of Ansible cluster
  2. 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