Ansible And AWS EC2 Instance

Ansible And AWS EC2 Instance

****Ansible is tool which is used for Configuration Management (CM). In Ansible we only tell to "what to do". We don't need to tell "How to do" because Ansible already knows that "How to do this task" on each type of Operating System .

****AWS is a cloud which is provided by Amazon. Amazon Elastic Compute Cloud (Amazon EC2 ) is a part of Amazon.com's cloud-computing platform, Amazon Web Services, that allows users to rent virtual computers on which to run their own computer applications. AWS EC2 service provisions resources like RAM , HardDisk . CPU etc.

In this article I will Launch "AWS EC2 instance"

In Ansible If we want to do configuration we need Managed Node where we want to configuration , But any cloud doesn't provide Any OS for this

If we want to communicate with AWS then three ways available -

> WebUI

> CLI

> API

In this practical I will use API way. Ansible is purely build in python So , we need a python API which have capable to communicate with AWS . For this "boto" API is available.

Now To communicate AWS with API we need -

> "boto" API

> Access key and Private key (To login in aws)

** Install "boto" API with pip (pip3 install boto) -

No alt text provided for this image

** Access key and Private key -

> Go to IAM service in AWS -

No alt text provided for this image
No alt text provided for this image

> Add user - (Select Programmatic Access ) then Click Next -

No alt text provided for this image

>In my case I am providing "Administrator Access" All power to this user -

No alt text provided for this image
No alt text provided for this image

> Download .csv file - if you want to store you key in your harddisk.

No alt text provided for this image


Now I am creating a Ansible vault to store these keys - (varible file is "pass.yml")

No alt text provided for this image
No alt text provided for this image


Now For creating EC2 Instance We need these information -

> region: "ap-south-1"

No alt text provided for this image


> image: "ami-0ebc1ac48dfd14136"

No alt text provided for this image


> instance_type: "t2.micro"

No alt text provided for this image


> count: 1 (Number of Instance) and > vpc_subnet_id: "subnet-7acfbf36"

No alt text provided for this image

> volumes:

- volume_type: "gp2"

volume_size: 8

delete_on_termination: true

No alt text provided for this image


> group_id: "sg-757bbd11"

No alt text provided for this image

> key_name: "key"

No alt text provided for this image

Now We have gathered all information about ec2 instance. So for launching aws ec2 instance ansible has "ec2" module. To know more about this module visit this link -

So our Ansible playbook code -

No alt text provided for this image


We are running this playbook on localhost so we assign localhost in hosts, See there are no running instance on aws

No alt text provided for this image

Now run this playbook -

No alt text provided for this image

Now our code is successfully run - To checking I am going to aws WebUI -

No alt text provided for this image

Now our practical is successfully done.

Thank you.


要查看或添加评论,请登录

Govind Bhardwaj的更多文章

社区洞察

其他会员也浏览了