Launched Ec2 Instance with the help of Ansible.

Nityachawda
4 min readJan 24, 2021

--

This article will help you to launch an EC2 instance on top of AWS Cloud.

What is Ansible?

Ansible is a system of configuration management written in Python programming language which uses a declarative markup language to describe configurations. It’s used for automation of configuration and OS setup. Ansible is often used to manage Linux-nodes, but Windows is also supported. It supports work with devices with Python v2.4 and higher installed through SSH connection. In this article, we are going to review the DevOps tool called Ansible.

What is AWS?

Amazon Web Services (AWS) is the world’s most comprehensive and broadly adopted cloud platform, offering over 175 fully featured services from data centers globally. Millions of customers — including the fastest-growing startups, largest enterprises, and leading government agencies — are using AWS to lower costs, become more agile, and innovate faster.

What is EC2 Instance?

An EC2 instance is nothing but a virtual server in AmazonWeb services terminology. It stands for Elastic Compute Cloud. It is a web service where an AWS subscriber can request and provision a compute server in AWS cloud.

Task Description:

♦️Provision EC2 instance through ansible.

♦️Retrieve the IP Address of instance using a dynamic inventory concept.

♦️Configure the webserver through ansible!

♦️Create a role for the webserver to customize the Instance and deploy the webpage to the root directory.

Setting up the Inventory file.

As we are launching an EC2 instance on the top of the AWS cloud for this we don’t know the IP of the launched instance. It will be known after launching the EC2 Instance.

In this case, we use the Concept of LocalHost.

In the inventory file, we will launch the localhost IP address. The main reason behind doing this is that our controller node will behave like managed node.

Step-1

Now, we will create an ansible vault to store our secret data in encrypted form.

In ansible vault, we will store our access and secret key of the user in AWS.
Command to create ansible vault:

ansible-vault create Nitya@prompt Secretfile.yml

Step-2

Now, we will create a playbook that will launch EC2 instance on the top of AWS cloud.

NOTE:
Playbook name can be anything but the extension should be .yml only.
We only need to run this playbook now.

Command to run the playbook:

ansible-playbook <playbook_name.yml>

Look our EC2 has been launched on top of AWS.

Now, we have to append the public IP of the launched EC2 instance in the inventory file so that we can work with that instance.

Here we need a key to enter into the instance.

Here key will work as a Password. Also, the user name of the instance will vary because of the type of AMI used. In this case, I have used Amazon AMI therefore the user will be ec2-user.

Now, let's check the connectivity with the host.

Command used to check:

ansible all -m ping

Step-3

In this step, we will now configure the httpd server in the launched EC2 instance. To do this we will again write a new playbook to install httpd, copy code and start the service httpd in the launched instance.

Command to run playbook:

ansible-playbook <filename.yml>

This playbook will install httpd, copy the code from the given source to the destination provided. Destination should be /var/www/html. This is the folder where all the code resides on the website.

Now, let’s go and look if the playbook has successfully worked or not.

httpd is installed and it is also running.
File is also copied in the folder.

Now, let’s check our final output.

By using the public IP of the instance and page name we can look at our output.

IP_Address/Filename.html

Finally we have successfuly launched Instance and configured httpd on it.

Github link for the repo:

https://github.com/Nitya2503/Ansible_Task2.git

Thanks for reading this article.

--

--

No responses yet