# Day 58 - Ansible Project 🔥

Welcome back to another exciting day of DevOps learning! In this blog, we'll build on our knowledge of Ansible by creating a practical project to deploy a simple web app. Ansible's power to automate infrastructure makes it a key tool in configuration management, and today we’ll demonstrate that by deploying a website on multiple EC2 instances using an Ansible Playbook.

By the end of this tutorial, you’ll have a clear understanding of how to:

* Set up EC2 instances.
    
* Install Ansible on the master node.
    
* Configure SSH access between the master and child nodes.
    
* Write an Ansible Playbook to install Nginx and deploy a static website.
    

### Task Overview

1. **Create 3 EC2 instances** and configure them with the same key pair.
    
2. **Install Ansible** on the host (master) server.
    
3. **Set up SSH access** between the Ansible host and other servers.
    
4. **Create an Ansible Playbook** to install Nginx and deploy a sample website.
    

### Step 1: Create EC2 Instances

To begin, spin up 3 EC2 instances on AWS. One will act as the Ansible master (controller), and the other two as target nodes (child servers).

* Choose the same key pair for all instances to ensure secure access from the Ansible host.
    
* Ensure that all instances are in the same security group for seamless communication.
    

### Step 2: Install Ansible on the Master Node

Once the EC2 instances are running, SSH into the Ansible master node to install Ansible.

```bash
# Update the repository
sudo apt update

# Install Ansible
sudo apt install ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159515189/0462e502-17f6-43d7-9744-f31f22598cb8.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159533642/66137686-df6d-4699-8ddd-753c1d7675e5.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159563948/60acaf38-65e2-4ec1-a3cd-6e14f0ae5d41.png align="center")

You can verify the Ansible installation by checking the version:

```bash
ansible --version
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159585883/f6c164ee-3514-4e6d-b27f-e746c3d4b7c3.png align="center")

### Step 3: Configure SSH Access

To allow the Ansible master to communicate with the child nodes, you need to copy the private key to the Ansible host and configure the inventory file.

#### Copy the Key:

```bash
# Copy the private key to the Ansible host
sudo scp -i "<pem_key>" <pem_key_path> ubuntu@<Ansible_master_IP>:/home/ubuntu/.ssh/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159615194/d3ae3db2-4ff5-4ece-81a8-a380e7e7a739.png align="center")

Set the correct permissions for the private key:

```bash
sudo chmod 700 /home/ubuntu/.ssh/<pem_key>.pem
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159632161/aceabb8d-6035-4886-9e90-0f9a4210c840.png align="center")

### Step 4: Configure the Inventory File

Next, configure the Ansible inventory file to define your hosts. Open the file:

```bash
sudo vim /etc/ansible/hosts
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159656881/e0cb2105-cc65-4ba7-90b4-81216bb51d81.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159672654/5b299f75-1528-4984-8ca5-56274d345be3.png align="center")

Add your EC2 instances to the inventory:

```bash
[my-servers]
server1 ansible_host=<Server1_IPv4>
server2 ansible_host=<Server2_IPv4>
server3 ansible_host=<Server3_IPv4>

[all:vars]
ansible_ssh_private_key_file=/home/ubuntu/.ssh/<pem_key>.pem
ansible_python_interpreter=/usr/bin/python3
ansible_user=ubuntu
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159696972/daeca45d-0b69-4b36-9e98-5e5c58b4c414.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159713631/c13527e6-2b1a-4326-a9cc-25492fef0da6.png align="center")

### Step 5: Create a Playbook to Install Nginx

Now, let’s create an Ansible Playbook to install and configure Nginx on all child nodes. Create a playbook file:

```bash
sudo vim install_nginx.yml
```

Add the following content to the file:

```bash
- name: Install and Start Nginx
  hosts: my-servers
  become: yes
  tasks:
    - name: Update apt packages
      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/v1724159740043/dc0772d8-2d09-4513-bbd0-c8b7cfc9dfb4.png align="center")

### Step 6: Deploy a Static Website

Next, we’ll deploy a simple webpage to the servers. Create an `index.html` file with some sample content:

```bash
vim index.html
```

For instance, you can add the following HTML content to the file:

```bash
<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Ansible Project</title>
</head>
<body>
    <h1>Welcome to Your Website!</h1>
    <p>This web page is deployed using Ansible.</p>
</body>
</html>
```

Modify the playbook to include the step for copying this file to the Nginx web server directory:

```bash
- name: Install and Start Nginx
  hosts: my-servers
  become: yes
  tasks:
    - name: Update apt packages
      apt:
        update_cache: yes
    - name: Install Nginx
      apt:
        name: nginx
        state: latest
    - name: Start Nginx
      service:
        name: nginx
        state: started
        enabled: yes
    - name: Deploy static website
      copy:
        src: /home/ubuntu/index.html
        dest: /var/www/html/index.html
      become: true
      become_user: root
      become_method: sudo
```

### Step 7: Run the Playbook

Execute the playbook to deploy Nginx and the static website on all child nodes.

```bash
ansible-playbook install_nginx.yml
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1724159776011/123632e4-6d05-4de2-b328-616695bfdbfc.png align="center")

### Step 8: Verify the Deployment

Once the playbook finishes executing, open your browser and enter the public IP address of one of the EC2 instances to view the deployed website.

For example:

```bash
http://<Server1_IPv4>
```

### Conclusion

In this Ansible project, we explored how to create and deploy a website using Ansible Playbooks. By automating the process of installing Nginx and deploying a web page on multiple servers, you’ve experienced firsthand how Ansible simplifies configuration management and application deployment.

Ansible empowers DevOps engineers to automate repetitive tasks, allowing for faster deployments and less human error. Keep experimenting with Ansible to further enhance your infrastructure management skills!

~[**Tushar Ranjan**](https://in.linkedin.com/in/tusharranjan001?trk=profile-badge)🙂
