Host web application on EC2 instance with Ansible Playbook
Tags: ansible, ansibleplaybook, aws, ec2
Published: 2024-06-03 10:35:48

**<u>Ansible</u>** is an open-source automation tool used for configuration management, application deployment, orchestration, and task automation. It is designed to simplify complex IT workflows and streamline repetitive tasks across large infrastructures.

**<u>Project Summary</u>**

In this project, lets learn about launching an application with configuration management tool Ansible. Launching a web application using an Ansible playbook involves several steps, including setting up the infrastructure, installing necessary software, and deploying the application. Here is a simplified example of an Ansible playbook that can help you deploy a web application

**<u>Prerequisites</u>**
**1. AWS Account** - you should have a valid AWS account to launch EC2 instances for installing ansible and hosting an application.
**2. Linux Background** - should have some background on Linux as we will be running various commands.


**<u>Step 1 - Launch AWS EC2 instance using Ubuntu & CenOS</u>**

- **Launch Ubuntu Instance for Ansible** - Use **t2.micro** instance type (Free tier) with name as **'Control'**. This is the instance where we will install the Ansible and use it as control machine for other webservices or application (installed on another EC2 instance).

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2kiw2fjn3rbibysqrb8m.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/p08gc7cw96xzfzk9ifrj.png)

- **Key Pair** - Create new key pair with control-key.pem file

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mwpl4kq31syonkzbkv2a.png)


- **Security Group** - Create new security group with **inbound rules** to allow port 22 from my IP address and launch the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1v2pv4dcugz6gu7cm0zx.png)

- **Launch CentOs Instance for web services** - Use **t2.micro** instance type (Free tier) with name as **'web01'**. This is the instance where we will deploy web application. For OS, go to 'Browse more AMI'-> AWS Marketplace AMIs and search for centos9.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/vqvm0jxfuw9d08ajdcfl.png)

- **Key Pair** - Create new key pair with client-key.pem file.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1g4yh888x8qkfvdb2vdv.png)

- **Security Group** - Create new security group with **inbound rules** to allow **port 22** from my IP address & from Ansible security group too. Also allow **port 80** (http) from my IP address. After this launch the instance.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mqd8n6fhod075643fobd.png)



**<u>Step 2 - Installing Ansible on Ubuntu OS</u>**

- **SHH into Control Instance** - SSH into the control EC2 instance and to login.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tkh6u8qk5b8scf278nks.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/csy6n11d4k6dlwkjidrq.png)

- **Install Ansible** - Using below mentioned commands, Install Ansible software in your control EC2 instance.

```
  $ sudo apt update
  $ sudo apt install software-properties-common
  $ sudo add-apt-repository --yes --update ppa:ansible/ansible
  $ sudo apt install ansible
```

**<u>Step 3 - Create project directory, Inventory and playbook files</u>**

- **Create new directory** - After Ansible installation, create a new directory 'project1' where we will create the Ansible playbook for web appl.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/qni9uhdidjs5zd7j9tpo.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/oomu73wzbpbvni4z4bho.png)

- **Inventory file** - Create a inventory file inside the 'project1' directory. The purpose of this file is to defines the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. Sample inventory file code will looks like below. Make sure the spacing or indentation is correct while using inventory file.

```
all:
  hosts:
    web01:
      ansible_host: 172.31.17.59
      ansible_user: ec2-user
      ansible_ssh_private_key_file: clientkey.pem

```

```
**explanation:-**
hosts = web01 (give you web instance
ansible_host = should be the private IP address of your web01 instance
ansible_ssh_private_key_file = should be the private key that you use to authenticate with web01 instance
```
- **Client Key** - Since we are using clientkey.pem in our inventory file, we need to copy the private key of client EC2 instance to 'project1' directory with the same name that we have given in inventory file i.e clientkey.pem. This is to authenticate the web01 server. 

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7gjwoj0xkn061icgmgj7.png)

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7crw18pm72f28v9ot5kj.png)

- **Key Permission** - After copying the private key to clientkey.pem and place it into 'project1' directory, you need to modify the permission of the key.

Current Permission:

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/7grhwsnlhg3shqiq85qd.png)

Modify the permission using below command:-

```
chmod 400 clientkey.pem
```
After modification, it should be as per below:-

```
ubuntu@control:~/project1$ ls -l
total 16
-r-------- 1 ubuntu ubuntu 1675 Jun  3 09:18 clientkey.pem
```

- **Ansible Playbook** - An Ansible playbook is a file written in YAML (Yet Another Markup Language) that defines a series of automation tasks to be executed on managed nodes. Playbooks are the heart of Ansible's configuration management, allowing users to describe configurations, deployments, and orchestrations in a straightforward, human-readable format. Lets create Ansible playbook with name '**web01.yaml**'.

```
---
- name: Webserver setup
  hosts: web01
  become: yes
  tasks:
    - name: Install httpd
      ansible.builtin.yum:
        name: httpd
        state: present

    - name: Start service
      ansible.builtin.service:
        name: httpd
        state: started
        enabled: yes

    - name: Copy the index file or template
      copy:
        src: myfile/index.html
        dest: /var/www/html/index.html
```

```
**explanation:-**
name - This is the name of the ansible playbook or task, you can give based on your choice.
hosts - This is the host name which you have given in the inventory file
tasks - It stores the list of tasks that you want to execute automatically
```


**<u>Step 4 - Run and Test the application</u>**

- **Run** - Finally its time to run the command as per below to execute the playbook and deploy the application to web server (which is web01 EC2 instance)

```
 ansible-playbook -i inventory web01.yaml
```
Output should be as per below:-

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/1y3ac04rr47aqkiy7di9.png)

- **See Application on Browser** - Lets take the public Ip address of the web01 instance and paste into the browser and a simple web application should be launched:-
**Note**:- Make sure that security group of web01 instance should allow inbound rule for port 80 through control (ansible) instance from my IP or anywhere. If you are not doing this then it will timeout.

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ms0bi96eoij78n9qhhvl.png)

**<u>Conclusion!</u>**
Deploying a web application using Ansible on an AWS EC2 instance offers a powerful and automated solution to streamline the process of provisioning infrastructure, installing necessary software, and managing application deployments. By leveraging Ansible's playbooks, you can ensure consistent, repeatable, and scalable deployments, significantly reducing manual intervention and potential errors. This approach not only enhances operational efficiency but also allows for rapid iteration and deployment of web applications. Whether you are managing a single server or a complex infrastructure, using Ansible to automate the deployment process on AWS EC2 instances empowers you to focus more on developing and improving your application rather than dealing with infrastructure complexities.

**Happy Learning!**