# Day 59: Ansible Project 🔥

![Tutorial to learn how to boost your Ansible code's quality](https://www.padok.fr/hubfs/Imported_Blog_Media/ansible_code.webp align="left")

It's great to see you working on an Ansible project! Here's a step-by-step guide to help you achieve your tasks:

**Task-01: Create 3 EC2 instances with the same key pair**

1. Use the AWS Management Console or AWS CLI to create three EC2 instances.
    
2. Make sure to use the same key pair for all three instances.
    
3. Note down the public IP addresses of the instances for later use.
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705847564979/c24b5aa9-f58e-4b50-9a8e-24cb661827e3.png align="center")
    
    ![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705847610369/6963d37e-e7ec-4248-bfdf-380ef04f659a.png align="center")
    

**Task-02: Install Ansible in the host server**

Connect to your Ansible host server and install Ansible. Assuming you're using Ubuntu, you can use the following commands:

```bash
sudo apt update
sudo apt install ansible
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705847668952/06225b15-0349-460c-b39c-67fe31820951.png align="center")

**Task-03: Copy the private key from local to Ansible host**

Assuming you have the private key on your local machine, use `scp` to copy it to your Ansible host:

```bash
scp /path/to/your/private-key.pem user@ansible_host_ip:/home/ubuntu/.ssh/
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705847779626/1b6844b6-388e-496c-babd-277e58f5d55a.png align="center")

Replace `/path/to/your/private-key.pem`, `user`, `ansible_host_ip` with the actual path to your private key, your server's username, and the IP address of your Ansible host.

**Task-04: Access the inventory file using sudo vim**

Edit the Ansible inventory file to include the details of your EC2 instances. Open the inventory file using `sudo vim /etc/ansible/hosts` and add the following:

```ini
[web_servers]
ec2_instance_ip1
ec2_instance_ip2
ec2_instance_ip3
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705847837110/a52b1dcd-e6fc-40ee-9ad4-be1dd7f2edfe.png align="center")

Replace `ec2_instance_ip1`, `ec2_instance_ip2`, and `ec2_instance_ip3` with the public IP addresses of your EC2 instances.

**Task-05: Create a playbook to install Nginx**

Create an Ansible playbook, let's say `nginx_install.yml`, with the following content:

```yaml
---
- name: Install Nginx
  hosts: web_servers
  become: yes

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

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

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

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705848263444/c4e56406-618a-4cf6-9200-81020d836a65.png align="center")

**Task-06: Deploy a sample webpage using the Ansible playbook**

Extend the playbook (`nginx_install.yml`) to deploy a sample webpage:

```yaml
---
- name: Install Nginx and deploy sample webpage
  hosts: web_servers
  become: yes

  tasks:
    # ... (previous tasks)

    - name: Create sample index.html
      copy:
        content: "<html><body><h1>Hello from Ansible!</h1></body></html>"
        dest: /var/www/html/index.html

    - name: Restart Nginx service
      service:
        name: nginx
        state: restarted
```

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705848379176/a8d1fff9-7f1b-4159-bed4-2154cfb9fda9.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705848494127/e5c3e101-0242-4b02-8b3d-a4bd42ec8c20.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705848766748/30db3b07-9fa0-4857-8762-91cee590062a.png align="center")

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1705848833838/9b4d1745-c134-46f1-8c0c-b688db8636d7.png align="center")

**Thank you for reading!! I hope you find this article helpful!!**

**Happy Learning!!**

**Lokesh Telange ✨✨**

That's it! Good luck with your Ansible project!
