Configuring Web Server On AWS EC2 Instances Using Ansible

Ansible is a great tool in doing configuration on any OS what we have to launch an OS using Ansible. So it can be possible using Ansible. But ansible is meant for configuration and for provisioning OS, we can use Terraform. Though Ansible we can manage configuration as well as provisioning there I am using Ansible to provision ec2 instance and also for configuring web server inside that ec2 instance.

Task Description :-

  • Provision EC2 instance through ansible.
  • ️Retrieve the IP Address of instance using dynamic inventory concept.
  • Configure the web server through ansible.

1. Install necessary library

2. Changes in Configuration file of ansible

3. Ansible Playbook to launch EC2 Instance

After retrieving the public IP of the newly launched ec2 instance. we will be copying that public IP in a file named as hosts.txt. Here, i have used the copy module and in that copy module i have used the content parameter.

4. Running Playbook

You can also use ansible-vault to store Access key ID and Secret access key. There wont be much change in code.

Changes in Playbook

Running Playbook with vault

5. Now Retrieving the IP Address of instance using a dynamic inventory concept.

Now For Proceeding we need IP Of Our Instance Launched on aws so we be using dynamic inventory concept using this concept we can get the ip of launched instance on aws using one python program that u can get from ansible github.

  • Make a folder that will contain all the information’s of inventory.
  • Step-2: Go inside /etc/ansible/ansible.cfg directory and set the path of your inventory folder which you made earlier.
  • Step-3: After saving the inventory path, download pre-created python and ec2.ini files. So follow the below syntax to download them.
  • Step-4: Convert both files into an executable mode.
  • Step-5: Open your with help of an editor like vim and vi and comment “from ansible.module_utils import ec2 as ec2_utils” line which is existing at 172 in your ec2.yml file.

Note: If your controller node has Python3 then replace the location of shebang(#!) from #!/usr/bin/env python to #!/usr/bin/

  • Step-6: Now open the ec2.ini file and give your access and secret key which will be mentioned at the bottom of the ec2.ini file.
  • Now your dynamic inventory configuration is done. If your AWS Account has any launched instance the check with the below command.

6. Steps for configuring the webserver through ansible.

Now For Configuring webserver in aws instance we need to do ssh and for this we will require private key.

Transfer your private key to the controller node by using winscp software.

7. Writing Playbook

Running Playbook


I have combined all the above task into one playbook known as main playbook

import_playbook module imports the playbook

Running playbook all together


As you can see one EC2 instance was running already but when I ran the notebook again one more EC2 instance launched

I’m an undergraduate student at IIIT Ranchi, pursuing my B-Tech in Electronics and Communication Engineering.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store