Deploying WebServer on AWS using Dynamic Inventory with Ansible!

Sumayya Khatoon
6 min readJun 29, 2021

--

Hello Folks! In this Blog I’m gonna show you the demo on configuring Apache WebServer using Dynamic Inventory with Ansible. So, Guys let’s get going…

What is Ansible?

Ansible is an open source IT configuration management (CM) and automation platform, provided by Red Hat. Ansible is well known for its Configuration Management. Ansible is Python Product. Ansible works on Declarative Approach. Ansible loves to do the repetitive tasks which is very tedious in the Agile World. Ansible works on PUSH mechanism. Ansible is a DevOps tool which basically used to automate the things. It uses playbook to describe automation jobs. Playbook uses a simple language called YAML. One Playbook can contain one or more Plays.

What is Apache Web Server?

Apache HTTP Server is a web server that delivers web content through the internet. It is commonly referred to as Apache and after development, it quickly became the most popular HTTP client on the web.

Why we need Dynamic Inventory?

Static Inventory doesn’t help when we’ve complex infrastructure ,especially when we keep on installing the Operating System. In that case, Dynamic Inventory takes place.

What is AWS?

AWS stands for Amazon Web Services. AWS is a Public Cloud. AWS is a platform that offers flexible, reliable, scalable, easy-to-use and cost-effective cloud computing solutions.

Is there any Pre-requisites?🤔

  1. Umm🤔Yes, Ansible to be installed and Configured.
  2. AWS account should be created.
  3. IAM User should be created.

So, let’s implement the Practical →

Step 1: Installing Boto and Boto3 SDK

As Ansible is built on Python Language, Python has a Software Development Kit (SDK) which is called Boto or Boto3. This SDK has the capability to contact to AWS Cloud.

  pip3 install boto boto3
Boto & Boto3 gets installed

Step 2: Launching EC2 Instance on AWS using Ansible Playbook

Firstly, create an Ansible Vault to secure our AWS Credentials i.e Access Key and Secret Key

  ansible-vault create <name.yml>

Secondly , writing an Ansible Playbook that will launch EC2 Instance for Apache Web Server . So, here is the Playbook for the same.

YAML Code for Web Server

Thirdly, Launch the Playbook using the Ansible command

 ansible-playbook --ask-vault-pass <name.yml>
Playbook ran Successfully
Instances launched via Ansible Playbook

Step 3: Downloading some file that will Retrieve the IP Address of the Instances Launched in AWS

We can manually go to the AWS Portal and can fetch the IP Addresses but that doesn’t make any sense as we’re creating the Dynamic Inventory. Dynamic Inventory means the Inventory should come by its own by doing some operation. For doing so, follow the below given command

To retrieve the IP of our instance dynamically we need two files ec2.py and ec2.ini and need to modify them. To download the files use the command:

First make one separate directory. In case I’ve created directory named /etc/host and then install both the files using wget command.

wget https://raw.githubusercontent.com/ansible/ansible/stable-  2.9/contrib/inventory/ec2.py wget https://raw.githubusercontent.com/ansible/ansible/stable-2.9/contrib/inventory/ec2.ini

After Downloading both of the files you have to make both of the files executeable.

chmod +x ec2.py   chmod +x ec2.ini
File ec2.py downloaded successfully
File ec2.ini downloaded successfully

Step 4: Doing some sort of Modification in the downloaded file

After Downloading both the files , we need to do some modification to it…

Go inside the file called ec2.py using vim command and in the first line itself make some changes. Instead of #!/usr/bin/env python write #!/usr/bin/python3

Now at line number 172 there’s a bug comment that line

Now go inside the ec2.ini file using vim command and at last line provide our AWS credentials i.e. AWS Access Key and Secret Key. Also change the region= ap-south-1

Step 5: Exporting the Shell Variables.

Now we’re good to go…

Step 6: Finally Retrieving the IP Addresses.

Do some configuration in Ansible Configuration file:

Now, To retrieve the Dynamic IP use:

  ansible all --list-hosts

To check the connectivity between Controller Node and Target Nodes

  ansible all -m ping

Step 7: Retrieving the Tag of the Instances

To retrieve the tag go inside the directory where you’ve downloaded ec2.py file and run the below given command

  ./ec2.py

We can use those tags while configuring Apache Webserver…

Now , Everything is set we can create an Ansible Role for Configuring Webserver

Step 8: Creating Roles for Configuring Web Server.

Role is a way to manage the Playbook in an Efficient Manner.

Following is the command to create Roles in Ansible:

  ansible-galaxy init <role_name>
  tree -C
Hierarchy of webserver role

Step 9: Configuring Apache server inside the role created

To configure Webserver inside the role we’ve to go to our tasks set up inside the webserver role /webserver/tasks/main.yml

   main.yml

Now, create a playbook in the root project directory as setup.yml

   vim setup.yml

It’s Testing Time Guys…🙈

Run the Setup Playbook using command:

  ansible-playbook setup.yml

Step 10: Testing the Webserver.

Fetch the IP of Instance and it’s Port Number 80 and browse it in your Browser…

<IP_of_Instance:80>

Webserver deployed successfully!!

Voila!! Web Server has been Configured Successfully

Hope you find this Blog Easy!!🤞

Do like Share and give a clap!!

Thank You For Reading!!

Source Code:

--

--

Sumayya Khatoon
Sumayya Khatoon

Written by Sumayya Khatoon

Machine Learning || Deep Learning || Kubernetes|| Docker || AWS || Jenkins || Ansible(RH294) || Python || Linux(RHEL8 )

No responses yet