Bobcares

Ansible Managing AWS resources using Dynamic Inventory

PDF Header PDF Footer

Ansible managing AWS resources is possible using Dynamic Inventory.

As part of our AWS Support Services, we assist our customers with several queries about Ansible.

Today, let us see how Ansible manages AWS resources.

 

Ansible Managing AWS resources

Ansible is a great tool for provisioning servers. However, when it comes to the creation of infrastructure it does not perform as well as Terraform.

Nevertheless, to provision a server it works very well.

Using Ansible to automate applications in AWS greatly increase the chance that our cloud initiative will be a success. The breadth of AWS capability enables IT organizations to dynamically provision entire workloads like never before.

To harness this power, IT organizations must effectively answer:

  • How can we control cloud deployments?
  • How does DevOps work in the cloud?
  • Will my deployment be secure?
  • How can we migrate existing apps to the cloud?

Furthermore, let us see how our Support Engineers use AWS resources using Ansible with the help of Dynamic Inventory.

Ansible Managing AWS resources

Ansible Managing AWS resources using Dynamic Inventory

While provisioning resources, we store the details of our servers in the inventory file. We group them and assign IP etc. However, in the cloud, we cannot manually manage it.

To begin, our Support Techs suggest to:

  • Download ec2.py and ec2.ini and place both files in the same directory.
  • The “boto” library.
    $ pip install boto

 

Configuring Ansible to use ec2.py as the inventory resource

In order to rely on AWS resources, we would need to configure our “

ansible.cfg
” inventory to point to the “
ec2.py
” file

[defaults]
inventory = ./ec2.py

Now, that we have our inventory pointed to the “Dynamic Inventory” file, let us try to ping all our EC2 instances.

  • Pinging all our EC2 instances
ansible all -m ping

If we notice, we have not mentioned any IP addresses. This is the advantage of Dynamic Inventory. Hence, Ansible will ping to all our EC2 instances

  • Listing AWS resources

To list AWS resources, we execute the below command:

./ec2.py –list

This will return the entire list of AWS resources in JSON.

{
“_meta”: {
“hostvars”: {
“65.201.11.219”: {
“ansible_host”: “34.201.11.219”,
“ec2__in_monitoring_element”: false,
“ec2_account_id”: “209518873002”,
“ec2_ami_launch_index”: “0”,
“ec2_architecture”: “x86_64”,
“ec2_block_devices”: {
“xvda”: “vol-008e156f7bc2d99ad”
},
“ec2_client_token”: “”,
“ec2_dns_name”: “ec2-65-201-11-219.compute-1.amazonaws.com”,
“ec2_ebs_optimized”: false,
“ec2_eventsSet”: “”,
“ec2_group_name”: “”,
“ec2_hypervisor”: “xen”,
“ec2_id”: “i-09689b6635c10f3cf2”,
“ec2_image_id”: “ami-467ca739”,
“ec2_instance_profile”: “”,
“ec2_instance_type”: “t2.micro”,
“ec2_ip_address”: “34.222.11.219”,
“ec2_item”: “”,
“ec2_kernel”: “”,
“ec2_key_name”: “ec2_private.pem”,
“ec2_launch_time”: “2018-07-04T20:10:57.000Z”
}}}}

So far we are able to list the AWS resources and ping all our EC2 instances.

  • Destroying AWS resources in Ansible

Creating AWS resources in Ansible is extremely easy but the challenge is destroying them.

Ansible does not store the state of the infrastructure like Terraform, so if we want to destroy resources we have to utilize the Dynamic Inventory feature.

 

Creating AWS EC2 instance

– name: Create EC2 Instance in the public subnet
ec2:
image: ami-467ca739
wait: yes
instance_type: t2.micro
region: “{{ region }}”
group_id: “{{ security_group.group_id }}”
vpc_subnet_id: “{{ subnet.subnet.id }}”
key_name: “{{ keypair.key.name }}”
count_tag: “{{ ec2_tag }}”
exact_count: 1
instance_tags:
tool: “env”
env: “stage”

We need to tag the EC2 server creation as it is important while managing AWS resources in Ansible. This is how Ansible retrieves information of resources by querying the info of the tag.

So to delete the server we will declare the host’s name to match the tag attributes.

hosts: “tag_env_stage”

Now every task will perform on the resources with the tag “

env:stage
”. We no longer need to maintain the IP addresses of our EC2 instances in the inventory file. This is where Dynamic Inventory shines.

[Couldn’t manage Ansible? We’d be happy to assist]

 

Conclusion

To conclude, Ansible managing AWS resources is possible using Dynamic Inventory. Today, we saw how our Support Techs use AWS resources using Ansible with the help of Dynamic Inventory.

PREVENT YOUR SERVER FROM CRASHING!

Never again lose customers to poor server speed! Let us help you.

Our server experts will monitor & maintain your server 24/7 so that it remains lightning fast and secure.

GET STARTED

var google_conversion_label = "owonCMyG5nEQ0aD71QM";
0 Comments

Submit a Comment

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

Get featured on the Bobcares blog and share your expertise with a global tech audience.

WRITE FOR US
server management

Spend time on your business, not on your servers.

TALK TO US

Or click here to learn more.

Speed issues driving customers away?
We’ve got your back!

Privacy Preference Center

Necessary

Necessary cookies help make a website usable by enabling basic functions like page navigation and access to secure areas of the website. The website cannot function properly without these cookies.

PHPSESSID - Preserves user session state across page requests.

gdpr[consent_types] - Used to store user consents.

gdpr[allowed_cookies] - Used to store user allowed cookies.

PHPSESSID, gdpr[consent_types], gdpr[allowed_cookies]
PHPSESSID
WHMCSpKDlPzh2chML

Statistics

Statistic cookies help website owners to understand how visitors interact with websites by collecting and reporting information anonymously.

_ga - Preserves user session state across page requests.

_gat - Used by Google Analytics to throttle request rate

_gid - Registers a unique ID that is used to generate statistical data on how you use the website.

smartlookCookie - Used to collect user device and location information of the site visitors to improve the websites User Experience.

_ga, _gat, _gid
_ga, _gat, _gid
smartlookCookie
_clck, _clsk, CLID, ANONCHK, MR, MUID, SM

Marketing

Marketing cookies are used to track visitors across websites. The intention is to display ads that are relevant and engaging for the individual user and thereby more valuable for publishers and third party advertisers.

IDE - Used by Google DoubleClick to register and report the website user's actions after viewing or clicking one of the advertiser's ads with the purpose of measuring the efficacy of an ad and to present targeted ads to the user.

test_cookie - Used to check if the user's browser supports cookies.

1P_JAR - Google cookie. These cookies are used to collect website statistics and track conversion rates.

NID - Registers a unique ID that identifies a returning user's device. The ID is used for serving ads that are most relevant to the user.

DV - Google ad personalisation

_reb2bgeo - The visitor's geographical location

_reb2bloaded - Whether or not the script loaded for the visitor

_reb2bref - The referring URL for the visit

_reb2bsessionID - The visitor's RB2B session ID

_reb2buid - The visitor's RB2B user ID

IDE, test_cookie, 1P_JAR, NID, DV, NID
IDE, test_cookie
1P_JAR, NID, DV
NID
hblid
_reb2bgeo, _reb2bloaded, _reb2bref, _reb2bsessionID, _reb2buid

Security

These are essential site cookies, used by the google reCAPTCHA. These cookies use an unique identifier to verify if a visitor is human or a bot.

SID, APISID, HSID, NID, PREF
SID, APISID, HSID, NID, PREF