Guys, let me start my first blog here…

It’s all on an excellent automation tool–ANSIBLE

I wanted to share you guys the step-by step procedure to deploy, configure the tool in an AWS EC2 Virtual machine. Also, a simple procedure is given at the end of the article to deploy new virtual machines in AWS using Ansib;le playbooks

Okay, let’s start…I have deployed a new RedHat EC2 Virtual machine in AWS with the following details:

AWS EC2 step-by-step Automation by Ansible

You may select appropriate disk, memory, cpu etc.. as per your convenience and future use of this machine. It’s a staraight forward way to create an AWS virtual machine

Few things, you have to make a note is

  • Virtual machine’s public IP address, to connect the machine through SSH using the tool like Putty.
  • Make sure that your AWS security groups have the rules allowed to connect the machine through SSH port 22
  • Make a note of the keypair name. This name we need to be used in Ansible Playbook. Keep the key in your computer so that you can connect the machine through SSH
  • Also, note the AMI ID of the Virtual machine in AWS. This can be noted from the machine properties and looks like the follwoing. We need the following hilighted name only for using this in Ansible playbook
    AMI ID

Connect the machine using the SSH tool. If you are using putty, browse the key file as below (SSH->Auth)

AWS EC2 step-by-step Automation by Ansible

ec2-user is the default login username for this AWS AMI. We don’t need to type the password as there is a keyfile attached.

Here are the further steps by step procedure I have listed as below

  • Excecute sudo su so that you have root permission to install the required packages further
  • Execute sudo yum update to update the AMI with latest available packages. Note that the system will prompt to ask your permission to install the packages and press ‘y’ when prompted. Once the pacges are successfully installed, you can observe the following at the shell
AWS EC2 step-by-step Automation by Ansible
  • Next, we need to install pip (pip is a package management system used to install and manage software packages written in Python) using the command sudo easy_install pip
  • Successfull installation will show the prompt as follows:
AWS EC2 step-by-step Automation by Ansible
  • Using pip, we can install the ANSIBLE pakage with the following command:

sudo pip install ansible

  • Finally, we have successfully installed Ansible package as shown below:
AWS EC2 step-by-step Automation by Ansible
  • Ansible version can be re-cheked with the below command

ansible --version

This will provide you the following details also along with the version of Ansible we just have installed


[root@ip-172-31-15-182 ec2-user]# ansible –version
config file = None
configured module search path = [u’/root/.ansible/plugins/modules’, u’/usr/share/ansible/plugins/modules’]
ansible python module location = /usr/lib/python2.7/site-packages/ansible
executable location = /bin/ansible
python version = 2.7.5 (default, May 3 2017, 07:55:04) [GCC 4.8.5 20150623 (Red Hat 4.8.5-14)]
[root@ip-172-31-15-182 ec2-user]#


  • We also need an important package named ‘boto’ to be installed so that we can create AWS EC2 Virtual machines using Ansible. Boto is the Amazon Web Services (AWS) SDK for Python, which allows Python based programs like Ansible to create EC2 virtual machines in AWS. Boto3 can be installed using the following command:

sudo pip install boto3

Here is how it shows a successful Boto3 installation

AWS EC2 step-by-step Automation by Ansible

So, we have successfully completed the infrastructure requirements for Ansible. Now we can create the AWS EC2 Virtual machines with the help of Ansible as I am describing in following section:

Ansible requires important files :  ansible.cfg, hosts, yml playbook file

If you just want to try out ansible, it generally works without an ansible.cfg file too. (You do need to specify some inventory but you can do that on the command line rather than a file if you want).

Although we have installed ansible the necessary files are to be created separately by the following procedure

[localhost]<br />local

  • Simply run the ping command to test our newly installed Ansible!

ansible localhost -m ping</code></p>
<p style="padding-left: 60px;"><code>[image:12]

  • Now we need to export the AWS Access keys so that the AWS account can be authenticated sucessfully to the AWS platform. There are many ways available to achive this. For this test purpose, I am going to add the keys to my bashrc file as below

vi ~/.bashrc

  • Add the following lines at the end of the file as below. Following keys are obtained from the IAM dashboard. If you don’t have these keys saved yet, you may create these keys again from your AWS user account.

export AWS_ACCESS_KEY_ID='SKIAJ32YA44WRNNO3WMQ'<br />export AWS_SECRET_ACCESS_KEY='AZRWz+68twlGEiqfZjVeJwjUrfZvg+YR0dV1ez34'

  • Save the bashrc file and source the file as below:

source ~/.bashrc

  • Next, I am going to create an ansible paybook file. The file is in yml format. You may use the following contents to create your ansible playbook file. Copy the contents to the yml file for example ec2-create.yml

# sample playbook for by Manu
– name: Provision a Redhat AMI EC2 node in AWS
hosts: local
connection: local
gather_facts: False
tags: provisioning
instance_type: t1.micro
security_group: default # This is the default security group I have pre-created in my AWS
image: ami-10bb2373 # Note that this AMI name should be a valid name in the region we have selected
region: ap-southeast-1 # The region name and following keypair must match
keypair: ansible # Make sure that the region name selected above has got the keypair for the machine we use
– name: Launch new Instance
local_action: ec2 instance_tags=”Name=Ansible-Test” group={{ security_group }} instance_type={{ instance_type}} image={{ image }} wait=true region={{ region }} keypair={{ keypair }}
register: ec2

  • Finally change the folder permission on /etc/ansible directory so that there is execute permission to run ansible commands as below:

chmod 777 *

  • Now, we are ready to create our first AWS EC2 Virtual machine through Ansible :). Following is the syntax. Run it at /etc/ansible directory

ansible-playbook ec2-create.yml

Successful creation of the ec2 instance can be indicated as below:

AWS EC2 step-by-step Automation by Ansible


wow ! it’s the time to check the AWS EC2 dash board and to see how it’s been created over there ! Check it and let me know 🙂


No responses yet

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Comments