DEV Community

Cover image for Host web application on EC2 instance with Ansible Playbook
Rohit
Rohit

Posted on

Host web application on EC2 instance with Ansible Playbook

Ansible 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.

Project Summary

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

Prerequisites
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.

Step 1 - Launch AWS EC2 instance using Ubuntu & CenOS

  • 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

Image description

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

Image description

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

Image description

  • 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

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

Image description

  • 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

Step 2 - Installing Ansible on Ubuntu OS

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

Image description

Image description

  • 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
Enter fullscreen mode Exit fullscreen mode

Step 3 - Create project directory, Inventory and playbook files

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

Image description

Image description

  • 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

Enter fullscreen mode Exit fullscreen mode
**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
Enter fullscreen mode Exit fullscreen mode
  • 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

Image description

  • 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

Modify the permission using below command:-

chmod 400 clientkey.pem
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode
  • 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
Enter fullscreen mode Exit fullscreen mode
**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
Enter fullscreen mode Exit fullscreen mode

Step 4 - Run and Test the application

  • 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
Enter fullscreen mode Exit fullscreen mode

Output should be as per below:-

Image description

  • 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

Conclusion!
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!

Top comments (0)