Ansible Dynamic Inventory Aws

Raghav Agarwal
3 min readOct 19, 2020

Today we are going to discuss ansible dynamic inventory. The Ansible inventory file describes the hosts and groups of hosts upon which commands, modules, and tasks in a playbook operate. The inventory file contains to list of IP and hostname where you want to perform certain tasks.

Ansible contains two types of inventory files

  1. Static Inventory
  2. Dynamic Inventory

Static Inventory: As the name suggest static inventory is the inventory file where the information of remote system/target system you need to provide. for ex:

Sample File:

[my_servers]
1.2.3.4 ansible_ssh_private_key_file=/home/raghav/.ssh/id_ed25519.pub
202.54.1.5 ansible_ssh_private_key_file=~/.ssh/Lightsail-us-west-2.pem
www1 ansible_ssh_private_key_file=~/.ssh/Linode-us.rsa.pub
vpn-box1 ansible_ssh_private_key_file=~/.ssh/Linode-us.rsa.pub

Here all IP and Hostname information is static. So you need to provide this information to inventory file. That’s why this type of Inventory is known as Static Inventory.

Dynamic Inventory: If your Ansible inventory fluctuates over time, with hosts spinning up and shutting down in response to business demands, the static inventory solutions described in Working with Inventory will not serve your needs. You may need to track hosts from multiple sources

Ansible integrates all of these options via a dynamic external inventory system. So by using Dynamic inventory we can fetch All of the IP from any cloud platform easily.

So for showing this demo I will perform following tasks:

Statement : Deploy Web Server on AWS through ANSIBLE!

🔅Provision EC2 instance through ansible.

🔅Retrieve the IP Address of instance using dynamic inventory concept.

🔅Configure the web server through ansible!

🔅Create role for webserver to customize the Instance and deploy the webpage to root directory.

Summary : One Click Instance Launched and Web Server Deployed!

So First we will installed one plugin of AWS in ansible which will fetch the IP and used as Dynamic Inventory in ansible.

Create ansible.cfg file and add following lines

[defaults]
inventory = mydb
enable_plugins = aws_ec2

Inventory is folder where we store the inventory file either static or dynamic or both. In my case for showing I am using two Inventory file one is static one and one is dynamic one.

So in mydb folder create aws_ec2.yml file will act as dynamic Inventory :

---
plugin: aws_ec2
aws_access_key: AKIA3G5UQF2I3Z7WIR
aws_secret_key: heS5v5XIOB6RecVIzi5lmyD8sXXcpEy5ZrWcJi
#filters:
# instance-state-name: running
# tag:Name: ansible-ec2
keyed_groups:
- key: tags
prefix: tag
- key: tags.Role
separator: ""

In dynamic Inventory we can used Aws filters options that filter only that instances IP which is running and having tag as Key = Name and value= ansible-ec2.

Create one more file staic_hosts in same mydb folder which will act as static inventory.


1.1.1.1

Now if we want to list all IP we will type this command:

ansible-inventory --graph

Cool!!! we are able to fetch the IP Dynamically from Aws.

Now we will implement the above tasks. From Ansible we will launch Aws Instance.

- hosts: localhost
tasks:
- name: creating baic amazon ec2 instance
amazon.aws.ec2:
aws_access_key: AKIA3G5UQF2I3Z7WIR
aws_secret_key: heS5v5XIOB6RecVIzi5lmyD8sXXcpEy5ZrWcJi
key_name: ansible-prac
instance_type: t2.micro
image: ami-0cda377a1b884a1bc
count: 1
assign_public_ip: yes
vpc_subnet_id: subnet-4ff48a03
volumes:
- device_name: /dev/sda1
volume_type: gp2
volume_size: 10
instance_tags:
Name: ansible-ec2
group: common-sg
region: ap-south-1

We can also use Ansible Vault for hiding our AWS secret key and access key. But in this post I am not covering Ansible vaults.

Configure Web Server for the above instance with use of Dynamic Inventory.

- hosts: tag_Name_ansible_ec2
remote_user: ubuntu
tasks:
- name: installing apache software
command:
apt-get update
- name: Installing now
apt:
name: apache2
state: present
- name: starting service
service:
name: apache2
state: started
- name: copying web page
copy:
src: index.html
dest: /var/www/html

So as you can see I am using host as tag_Name_ansible_ec2 which is provide by the Dynamic IP.

So By this we can easily integrate Ansible with Aws and with use of Dynamic Inventory we can do Configuration much more faster and simpler.

Below is the ansible.cfg for further reference

[defaults]
inventory = mydb
enable_plugins = aws_ec2
host_key_checking = false
private_key_file=ansible-prac.pem
[privilege_escalation]
become=true
become_method=sudo
become_user=root
become_ask_pass=false

keep Learning Follow me on LinkedIn for more such articles

https://www.linkedin.com/in/raghav-agarwal-77b384167

--

--