# Day 59: Ansible Project 🔥  (Jan 24, 2024)

## **🙏 Introduction:**

In this blog, we will deploy a simple web app using ansible.

# **🎯Task: 1**

1. ### **Create 3 EC2 instances, make sure all three are created with same key pair**
    

* Three EC2 instances are created that include Ansible master node and Ansible servers.
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704388707378/829570fe-20a7-417a-a232-38df6b8d133d.png?auto=compress,format&format=webp align="left")

1. ### **Install Ansible in host server**
    

* Create the ansible repository in the master
    

```plaintext
sudo apt-add-repository ppa:ansible/ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704388971588/55e185be-8dd4-4c67-bb43-3c9bc864d480.png?auto=compress,format&format=webp align="left")

* Update the master node
    

```plaintext
sudo apt update
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389002975/7e49e394-d2d4-46f8-bdf5-ac89131a05b9.png?auto=compress,format&format=webp align="left")

* Install Ansible in the master node
    

```plaintext
sudo apt install ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389136874/fb25654a-8211-4898-8981-5ecc9c86aff6.png?auto=compress,format&format=webp align="left")

1. ### **Copy the private key from local to Host server (Ansible\_host) at (/home/ubuntu/.ssh)**
    

* Copy the private key that you have created while creating Ansible master node and Ansible servers
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389358208/e4a28c51-5cf2-43a6-a704-35a83327dc53.png?auto=compress,format&format=webp align="left")

* Create an ansible key file in .ssh directory and store the copied private key
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704389555203/5457c9e0-08a0-40bb-8b4a-533f4d31fd04.png?auto=compress,format&format=webp align="left")

* Give access permission for the ansible\_key
    

```plaintext
sudo chmod 700 ansible_key
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704390117159/2d14032b-0e27-460f-a700-086663a4c2ad.png?auto=compress,format&format=webp align="left")

1. ### **Access the inventory file using sudo vim /etc/ansible/hosts**
    

* Update the inventory file
    

```plaintext
[servers]
host_1 ansible_host=52.39.126.158
host_2 ansible_host=35.89.144.134

[all:vars]
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
ansible_ssh_private_key_file=/home/ubuntu/.ssh/ansible_key
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704392032810/2c92ba34-fb9a-4516-89a0-2aac2daa22fb.png?auto=compress,format&format=webp align="left")

1. ### **Create a playbook to install Nginx**
    

* Create a file with name **install\_nginx.yml**
    

```plaintext
---
- name: Install Nginx
  hosts: all
  become: yes  

  tasks:
    - name: Update apt 
      apt:
        update_cache: yes

    - name: Install Nginx
      apt:
        name: nginx
        state: latest

    - name: Start and enable Nginx service
      service:
        name: nginx
        state: started
        enabled: yes
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391542131/f4ccae55-784d-444e-bc27-4554ddc464d8.png?auto=compress,format&format=webp align="left")

* Run the the ansible-playbook
    

```plaintext
ansible-playbook install_nginx.yml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704391988083/1c20a3e9-ce75-4fe2-91a2-db89a0067ad4.png?auto=compress,format&format=webp align="left")

1. ### **Deploy a sample webpage using the ansible playbook**
    

* Create a webpage index.html file in the master node
    

```plaintext
<!DOCTYPE html>
<html>
    <h1>Hello Guys , Welcome to my page</h1>
    <h2>This is a sample webpage</h2>
</html>
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704392769788/4b4fdc45-b4f4-4517-bcef-e9946f03f859.png?auto=compress,format&format=webp align="left")

* Update the ansible-playbook
    

```plaintext
---
- name: Install Nginx
  hosts: all
  become: 'yes'
  tasks:
    - name: Update apt
      apt:
        update_cache: 'yes'
    - name: Install Nginx
      apt:
        name: nginx
        state: latest
    - name: Start and enable Nginx service
      service:
        name: nginx
        state: started
        enabled: 'yes'
    - name: Deploy webpage
      copy:
        src: /home/ubuntu/index.html
        dest: /var/www/html/index.html
```

* Run the the ansible-playbook
    

```plaintext
ansible-playbook install_nginx.yml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393479456/45abf47c-9755-42fc-a1fc-fc137a4904aa.png?auto=compress,format&format=webp align="left")

* Login to the Ansible-Server1 to check the webpage
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393629005/ea887567-2879-40a9-9d0b-5e737539be8e.png?auto=compress,format&format=webp align="left")

* Login to the Ansible-Server2 to check the webpage
    

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1704393658845/c3c51d90-5a3c-49e7-852d-043c4c70f9c4.png?auto=compress,format&format=webp align="left")
