Configuration of Apache Webservers Dynamically using Ansible on AWS

Ananya Jain
4 min readMay 2, 2021

Statement: Deploy Web Server on AWS through ANSIBLE!

🔅Provision EC2 instance through ansible.

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

🔅Configure the web server through ansible!

So Let’s Start…

First we have to install ansible in our controller node through pip3 as is a python module and written in python language.

pip3 install ansible

For launching an ec2 instance on AWS we have to consider target node same as our controller node. For that we have one IP named localhost which denotes the IP of our controller node only and for checking we can ping to localhost.

ansible localhost -m ping

So let’s start building the playbook which will launch an ec2 instance for us. Before that we have to first install the SDK file so that ansible can go to that AWS account and provision an ec2 instance for us. For that we need to download Boto3 software.

yum install python3
pip3 install boto3

We need to write some code in the configuration file of ansible i.e., ansible.cfg .

In default section we have provided our inventory file which contains Public IP after the instance gets successfully launched. Then we have host_key_checking = false for disabling the ssh key. To avoid some warning we can use command_warning = false .

To login in that newly launched instance we need to provide its respective key (in my case awsansiblekey.pem) in the read mode for that we have to give execute permission to that key

chmod 400 awskeyansible.pem

The ec2-instance Instances on AWS login firstly into the ec2-user. So we need to give the remote user by remote-user = ec2-user .Then we have to give information of user from which we will be configure it as webserver. If our user is not in the sudoers file then we need to run using sudo command that’s why we have become_method = sudo and become_user = root and we can use become_ask_pass = false in case we don’t want to use password while login every time.

Now let’s begin with the playbook

Let’s run this playbook

ansible-playbook aws.yml

Let’s see whether our instance launched or nor and the IP is copied in our inventory file

Let’s Check our updated inventory file:

Now let’s write a playbook for configuration of httpd webserver

Let’s write our final playbook which will include both the playbooks

Let’s run this playbook

ansible-playbook awshttpd.yml

Now, Simply we can go on google chrome and write our Public IP.

Yay !! We have successfully completed task.😊

THANK YOU EVERYONE !!✨

--

--

Ananya Jain

Google Cloud ACE Certified | Google Cloud Student Mentor | Operation Team @Amazon Alexa Community | Aviatrix Certified Engineer | Machine Learning enthusiast