Bobcares

Ansible Managing AWS resources using Dynamic Inventory

by | Jan 6, 2021

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 *

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