AWS Provisioning From Ansible

Gupta Aditya
6 min readFeb 14, 2021

--

Hey, guys hope you all are doing good in today's article we are going to see how can we launch EC2 in an instance in AWS and get public ip so that we can automate some of our stuff.

Let's take a scenario to suppose you have to launch 5 instances of similar type lets say for launching web server so that we can connect them to load balancer now there can be multiple options to launch instance but today we are using ansible that will launch an exact number of instances that you say him if less instance running then your requirement then it will launch more if more instance is running from your need it will terminate them and this all will be taken care by one single script isn’t that amazing just thing you have the requirement to launch 10 instances just one script and that's it is done😎.

This gonna a basic article that shows how to launch the instance and get public ip from ansible. Soon going to bring some large projects on this topic comparing extraordinary concepts.

Note here I am assuming you have setup ansible.

Let's get started with practical 💻

First of all, install the necessary package that is required by ansible to contact to AWS

pip install boto3 
or
pip3 install boto3 (if you have python3)

Here i hope you know how to create IAM user in AWS if not no need to worry soon going to give full detailed article on that so in case you dont know you can refer to that.

When you create IAM user you get one excel file which contain access key and Secret access key which look alike as shown below.

from this file get your credential and go to the terminal from where you gonna run ansible palybokk and there run the following commands:-

export AWS_ACCESS_KEY_ID='your access  key'
export AWS_SECRET_ACCESS_KEY='your secret access key'

Once this is done now we are all set to configure ansible to provision aws.

First go to configuration file of ansile and do the changes as shown below in image.

Ansible Configuration file

In this we are saying ansible to where is our private key so that it can use that while contacting aws instace well thats not necessary for provisioning but if what to do operation in the instance then yes we need location of private file to mentioned so that ansible can use that important point to be notes here that we have set remote user as ec2-user beacuase i have used amzon image which allow user to login as ec2-user so i am telling ansible to login as ec2-user then use sudo method to c\become root so that it can run all the commands thats it we have set in configuration file.

Once this is done we are all set to write playbook.As you can see below we have write the playbook lets understand things line by line.

- hosts: localhost
gather_facts: False
tasks:- name: Provision a set of instances
ec2:
key_name: "key want to attach on instance and uploaded on aws"
region: "Region on which you want to launch instace"
instance_type: "machine type you want"
image: “amzon image id”
wait: true
exact_count: "no of instance you want"
count_tag:
Name: Demo
instance_tags:
Name: Demo
register: ec2
— debug:
msg: ‘{{ec2.tagged_instances[0].public_ip}}’

First two line hosts: localhost and gather_facts: False telling ansible to run on localhost because we dont have anyinstace at this movment we will call aws to laucnh some for us so playbook has to be run somewhere so we tell him to run on localhost and gather facts is depend on you wheather you want or not here i made it false by default it is true.

Moving on to the next lines we are giving key name to attach to the instance here this key is pre created by me and uploaded on aws on comming article will conver how to create your own key and upload it to AWS but be sure to write key which is uploaded on aws else it wil show error after this we have set region in which we want to launch ec2 instance instance_type ,image we are giving machine type and image we want to use in aws wait:true it will wait there for operation to complete and after that we are stating no of instances we want,we have also set count tag name and instace tag name they both are different coun tag name is used to find no of that instace running already and instance_tags are used to label the new launched instance from that name. At end we are using register key word it use to store all data of task into varible and that data we can go through and find what we want .

When you run the below playbook with versbose comand (in case don’t know verbose is basically used to get more data on screen so we can see whole data you can achieve this by adding -vm as you increase number of v data getting increase here i suggest to use -vvv to get data so we can find what we want easily)

- hosts: localhost
gather_facts: False
tasks:- name: Provision a set of instances
ec2:
key_name: ansible
region: us-east-1
instance_type: t2.micro
image: “ami-047a51fa27710816e”
wait: true
exact_count: 1
count_tag:
Name: Demo
instance_tags:
Name: Demo
register: ec2
— debug:
var: ec2

After running above playbook you get output something like shown below

From this we want only want public ip so we have used our first playbook to get public ip as shown below

Hence we have achieved what we want.

Guys, here we come to the end of this blog I hope you all like it and found it informative. If have any query feel free to reach me :)

Github link:-https://github.com/guptaadi123/ansible-awsprovsioning.git

Guys follow me for such amazing blogs and if have any review then please let me know I will keep those points in my mind next time while writing blogs. If want to read more such blog to know more about me here is my website link https://sites.google.com/view/adityvgupta/home.Guys Please do not hesitate to keep 👏👏👏👏👏 for it (An Open Secret: You can clap up to 50 times for a post, and the best part is, it wouldn’t cost you anything), also feel free to share it across. This really means a lot to me.

--

--

No responses yet