top of page

How to Setup Ansible AWS Dynamic Inventory


Ansible AWS Dynamic Inventory

The Ansible AWS dynamic inventory is a powerful tool used to automatically discover and manage AWS resources within Ansible. Here's how it's useful:

Dynamic Resource Management: Instead of manually maintaining an inventory file with all your AWS resources, the dynamic inventory automatically fetches the current state of your AWS environment. This saves time and effort, especially in dynamic and rapidly changing environments.

Scalability: In large-scale AWS environments with numerous instances and resources, manually managing inventory files becomes impractical. Dynamic inventory ensures scalability by automatically adapting to changes in your AWS infrastructure.

Real-Time Updates: Since the dynamic inventory fetches information directly from AWS APIs, it provides real-time updates on your infrastructure. This ensures that Ansible always has the latest information about your AWS resources when executing tasks or playbooks.

Tag-Based Management: Dynamic inventory supports filtering resources based on tags, allowing you to organize and manage resources more efficiently. You can target specific groups of instances based on their tags, simplifying playbook execution.

Simplified Configuration: With dynamic inventory, you don't need to manually specify hosts and groups in static inventory files. This reduces the risk of misconfiguration and ensures consistency across environments.

Integration with AWS Services: Ansible's dynamic inventory integrates seamlessly with various AWS services, including EC2, RDS, S3, ELB, and more. This enables you to manage a wide range of AWS resources using Ansible playbooks.


To set up Ansible with AWS dynamic inventory, follow these steps:


1.Make sure you have python3 & pip3 installed on your Ansible server.

[root@siddhesh ~]# yum install python3 python3-pip 

2. Install the boto3 library. Ansible utilizes the boto core to make API calls to AWS for retrieving details of EC2 instances.

[root@siddhesh ~]#  pip3 install boto3

3. Create an inventory directory in /opt and cd into it.

[root@siddhesh ~]# mkdir -p /opt/ansible/inventory
[root@siddhesh ~]# cd /opt/ansible/inventory
[root@siddhesh inventory]#

4.Create a file named aws_ec2.yaml in the inventory directory.

[root@siddhesh inventory]# cat /opt/ansible/inventory/aws_ec2.yaml
---
plugin: aws_ec2
aws_access_key: XXXXXXXXXXXXXXXXXXXXX
aws_secret_key: XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX
keyed_groups:
  - key: tags
    prefix: tag
[root@siddhesh inventory]#

Let's break down the content of this YAML file:

plugin: Specifies the plugin that Ansible will use for dynamic inventory generation. In this case, it's aws_ec2, which indicates that Ansible will use the AWS EC2 plugin to fetch information about EC2 instances from your AWS account.

aws_access_key: This field contains the AWS access key that Ansible will use to authenticate with the AWS API. The value XXXXXXXXXXXXXXXXXXXXXXX is typically replaced with the actual AWS access key.

aws_secret_key: This field contains the AWS secret key corresponding to the access key provided above. The value XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX is typically replaced with the actual AWS secret key.

keyed_groups: This section specifies how Ansible should organize hosts into groups based on certain criteria. In this example, it's using the tags attribute of EC2 instances.

key: Specifies the attribute of EC2 instances to use for grouping. In this case, it's tags, which means Ansible will group EC2 instances based on their tags.

prefix: Specifies the prefix to be added to the group names. Here, it's set to tag, which means group names will start with tag_.


5.Enable the aws_ec2 plugin of Ansible.

[root@siddhesh inventory]# grep ^enable_plugins /etc/ansible/ansible.cfg
enable_plugins = aws_ec2
[root@siddhesh inventory]#

You will find this option under the [inventory] section of ansible.cf


6.Now, let's test the dynamic inventory setup by listing the EC2 instances.

[root@siddhesh inventory]# ansible-inventory -i /opt/ansible/inventory/aws_ec2.yaml --list

7.To list the dynamic inventory groups.

[root@siddhesh inventory]# ansible-inventory --graph -i /opt/ansible/inventory/aws_ec2.yaml
  |--@tag_Name_BUILDDEVOPS_Node1:
  |  |--ip-xxx-xx-x-xx.ap-south-1.compute.internal
  |--@tag_Name_BUILDDEVOPS_Node2:
  |  |--ip-xxx-xx-x-xx.ap-south-1.compute.internal
  |--@tag_Name_BUILDDEVOPS_Node3:
  |  |--ip-xxx-xx-x-xx.ap-south-1.compute.internal
  |--@tag_Name_BUILDDEVOPS_Node4:
  |  |--ip-xxx-xx-x-xx.ap-south-1.compute.internal
  |--@tag_Name_BUILDDEVOPS_Redis_Nod1:
  |  |--ip-xxx-xx-x-xx.ap-south-1.compute.internal
[root@siddhesh inventory]#

Overall, Ansible AWS dynamic inventory streamlines the management and automation of AWS infrastructure, providing flexibility, scalability, and real-time insights into your environment.

2件のコメント

5つ星のうち0と評価されています。
まだ評価がありません

評価を追加
ゲスト
3月18日
5つ星のうち5と評価されています。

Nicely Explained

いいね!

ゲスト
3月18日
5つ星のうち5と評価されています。
いいね!
bottom of page