Module 5: Case Study - Ansible

Problem Statement: You are a DevOps Engineer and the organization you are working on needs to set up two configuration management server groups. One for Apache, another for NGINX. Being a DevOps Engineer it is your task to deal with this configuration management issue.

Let us see the tasks that you need to perform using Ansible:

  1. Create two server groups. One for Apache and another for NGINX
  2. Push two html files with their server information

Make sure that you don’t forget to start the services once the installation is done. Also send post installation messages for both the server groups.

Using Ansible roles accomplish the above tasks. Also, once the Apache server configuration is done you need to install Java on that server group using Ansible role in a Playbook.

I will reuse the Ansible roles from Assignment 4 – Ansible and the EC2 instances that were created in Assignment 1 – Ansible. %%SSH agent%%

EC2 instances

Ansible Control Machine 10.0.1.223 Slave1 10.0.1.65 Slave2 10.0.1.233

Create HTML Files:

  • I prepare two index.html files, one for Apache and the other for Nginx, with the respective server information.
ubuntu@ip-10-0-1-223:~$ tree nginx_role/
nginx_role/
β”œβ”€β”€ README.md
β”œβ”€β”€ defaults
β”‚   └── main.yml
β”œβ”€β”€ files
β”‚   └── index.html
β”œβ”€β”€ handlers
β”‚   └── main.yml
β”œβ”€β”€ meta
β”‚   └── main.yml
β”œβ”€β”€ tasks
β”‚   └── main.yml
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ inventory
β”‚   └── test.yml
└── vars
    └── main.yml
 
7 directories, 9 files
ubuntu@ip-10-0-1-223:~$ cat nginx_role/files/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 to Nginx</h1>
    </div>
</body>
</html>
ubuntu@ip-10-0-1-223:~$ tree apache_role/
apache_role/
β”œβ”€β”€ README.md
β”œβ”€β”€ defaults
β”‚   └── main.yml
β”œβ”€β”€ files
β”‚   └── index.html
β”œβ”€β”€ handlers
β”‚   └── main.yml
β”œβ”€β”€ meta
β”‚   └── main.yml
β”œβ”€β”€ tasks
β”‚   └── main.yml
β”œβ”€β”€ tests
β”‚   β”œβ”€β”€ inventory
β”‚   └── test.yml
└── vars
    └── main.yml
 
7 directories, 9 files
ubuntu@ip-10-0-1-223:~$ cat apache_role/files/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 to Apache</h1>
    </div>
</body>
</html>

Create Ansible Inventory Groups:

  • I create an inventory file (hosts.ini) to define two groups, [apache] and [nginx].
[all:vars]
ansible_user=ubuntu
 
[apache]
slave1 ansible_host=10.0.1.65 
slave2 ansible_host=10.0.1.233
 
[nginx]
slave3 ansible_host=10.0.1.210
slave4 ansible_host=10.0.1.85

apache_role/tasks/main.yml

---
- name: Install Apache
  apt:
    name: apache2
    state: present
  notify: start apache
  
- name: Gather the Apache service facts
  service_facts:
  
- name: Push custom HTML file for Apache
  copy:
    src: index.html
    dest: /var/www/html/index.html
    group: root
    mode: '0644'
  when: ansible_facts.services["apache2.service"] is defined and ansible_facts.services["apache2.service"].state == "running"
  notify: reload apache
  
- name: Display post-installation message for Apache
  debug:
    msg: "Apache installation and configuration complete!"

%%

%%

apache_role/handlers/main.yml

---
- name: start apache
  service:
    name: apache2
    state: started
 
- name: reload apache
  ansible.builtin.service:
    name: apache2
    state: reloaded
 

%%

%% nginx_role/tasks/main.yml

---
# tasks file for nginx_role
- name: Install NGINX
  apt:
    name: nginx
    state: present
    update_cache: yes
  notify: start nginx
 
- name: Gather the NGINX service facts
  service_facts:
  
- name: Push custom HTML file for NGINX
  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
 
- name: Display post-installation message for NGINX
  debug:
    msg: "NGINX installation and configuration complete!"

nginx_role/handlers/main.yml

---
# handlers file for nginx_setup
- name: start nginx
  service:
    name: nginx
    state: started
 
- name: reload nginx
  ansible.builtin.service:
    name: nginx
    state: reloaded

site.yml:

---
- hosts: apache
  become: yes
  roles:
    - apache_role
    - java_role
 
- hosts: nginx
  become: yes
  roles:
    - nginx_role

To run these playbooks, I would use the ansible-playbook command, specifying the inventory file and the main site playbook:

ansible-playbook -i hosts.ini site.yml

%%

I re-run the playbook after stopping the nginx %%

I’ve executed the playbook multiple times already (troubleshooting an issue), resulting in all tasks being marked green. This indicates that the current state matches the desired state, and no changes were required. In addition, the appearance of our custom message confirms the successful execution of the playbook.

Success