Steps to Setup Ansible AWS Dynamic Inventory

Ansible can pull inventory information from dynamic sources, including cloud sources. You can also create a new dynamic inventory provider by creating a script or program that can output JSON in the correct format when invoked with the proper arguments.

Here at Ibmi Media, as part of our Server Management Services, we regularly help our Customers to perform Software Installation and Configuration on their AWS Servers.

In this context, we shall look into how Ansible manages AWS resources.

How to use Ansible to Manage 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:

i. How can we control cloud deployments?

ii. How does DevOps work in the cloud?

iii. Will my deployment be secure?

iv. How can we migrate existing apps to the cloud?

Also, we will see how to manage AWS resources using Ansible with the help of Dynamic Inventory.

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, do the following:

i. Download and ec2.ini and place both files in the same directory.

ii. The “boto” library;

$ pip install boto

How to Configure Ansible to use as the inventory resource ?

In order to rely on AWS resources, we would need to configure our “ansible.cfg” inventory to point to the “” file.

inventory = ./

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:

./ –list

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

“_meta”: {
“hostvars”: {
“”: {
“ansible_host”: “”,
“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_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”: “”,
“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.

How to destroy 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
image: ami-467ca739
wait: yes
instance_type: t2.micro
region: “{{ region }}”
group_id: “{{ security_group.group_id }}”
vpc_subnet_id: “{{ }}”
key_name: “{{ }}”
count_tag: “{{ ec2_tag }}”
exact_count: 1
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. ]


This article will guide you on how to use #AWS resources using Ansible with the help of Dynamic Inventory.

The #Ansible #inventory file defines the #hosts and groups of hosts upon which #commands, #modules, and tasks in a playbook operate. The file can be in one of many formats depending on your Ansible #environment and plugins.

Ansible will use it as an inventory source as long as it returns a #JSON structure like the one above when the script is called with the --list .

For Linux Tutorials

We create Linux HowTos and Tutorials for Sys Admins. Visit us on

Also for Tech related tips, Visit

Related Posts

Focus on your business, not your servers.

Click Here to Learn More

Recent Post