How to setup Ansible for ec2 instance using concept of dynamic inventory🙂

Tushar Joshi
4 min readJul 23, 2021

♦️ Deploy Web Server on AWS through ANSIBLE!

🔹 Provision EC2 instance through ansible.

🔹 Retrieve the IP Address of instance using the dynamic inventory concept.

🔹 Configure the web server through ansible!

  • As Ansible is built on top of python, a Python Software Development Kit (SDK) is required that enables the configuration of AWS services. The package is an object-oriented API named boto3.So lets start building this setup. We will require a SDK so that ansible can go to that AWS account and do the provisioning first. Boto and latest version of Boto is Boto3. we need to download this two software.

Checking version of ansible

Ansible 2.9 and ansible 2.10 is the latest version. you can work with both. Not any such difference between these two version.As we have installed boto and boto3 library. So, ansible will go on AWS via localhost as it will using API of AWS services.

→STEP-1 :

  • In the first step, I provisioned an ec2 instance with this playbook.
  • Here, the Red Hat system itself calls the API for configuration on AWS, and this procedure is done on the local machine that’s why the host is supposed to be localhost.
  • For authentication to the AWS account, create one IAM user that has less privileged than the root account. The AWS_ACCESS_KEY and AWS_SECRET key are passed explicitly through an Ansible vault named lock.yml

Ansible playbook to provision an OS:

Ansible register allows the user to capture the output and store as variables and can be used in different scenarios. The variables will contain the value returned by the task.

The register variable will print the public IP address of the instance from Ansible facts it gathers.

→STEP-2 :

> The instance has been launched! Next what?🤔

We need to dump the IP address of this instance into the inventory file and do the further

procedure!

Wondering I will simply write the IP in the host file🤭?? NAH ! Not manually🤫

AND THIS IS WHERE 🔥DYNAMIC INVENTORY🔥 COMES TO PLAY:

Ansible dynamic inventory is a concept that contains scripts that work as external APIs and pulls the information(facts) of a particular provider.

→The gathered facts will be dynamically dumped into the host file and further, we can create groups of these hosts according to requirement.

→Copy the following files into the controller node to enable dynamic inventory.

https://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.pyhttps://raw.githubusercontent.com/ansible/ansible/stable-1.9/plugins/inventory/ec2.ini

Both files need to be in executable format:

chmod +x ec2.py
chmod +x ec2.ini

Also, for account authentication, pass AWS_ACCESS_KEY and AWS_SECRET_KEY in the ec2.ini file. This will contact to AWS on our behalf and retrieve the information of the ec2 instance.

→Edit the inventory file in the ANSIBLE.CFG configuration files too.

→STEP-3 :

→With a defined host, now the final step is to deploy our application! In this example, I am deploying an apache webserver.

→Before that, enter the key file in the ansible configuration file.

private_key_file= /root/path-to-private-key 🔒

This file also needs to be executable .. chmod 600 key_name.pem

🙌Out of the box yet important information about file access:

These numbers show different types of permissions given to a file or a directory.

the format is: chmod XYZ

x is the root or owner permissions

y is the group permissions

z is the permission for other users

Now let’s get to know what does these numbers mean. So, there are generally three types of permissions: read (r), write (w), and executable (x)

Each number denotes some kind of permissions. They are:

0 = no permission

1 = only execute (- — x)

2 = only write (-w-)

3 = write an execute (-wx)

4 = only read (r — )

5 = read and execute (r-x)

6 = read and write (rw-)

7 = all (rwx)chmod 777:

here, 7 means all permissions and three 7s means the rwx permission is given to all (owner, group, and other)similarly, you can calculate the same for all the numbers.

Now, run one playbook that downloads the required packages into the instance and copy the code into the document root of the web server.

The output of this playbook :

FINAL OUTPUT :

Thank You for reading !!!

Keep learning Keep sharing !!!

--

--

Tushar Joshi

MLOPS Intern at Linux World || MLOPS 🧠 || DEVOPS(🐳☸👩🏻‍🍳)|| Ansible || Kubernetes|| AWS || ML || DL || Data Science || Jenkins|| Docker || RedHat Linux ||