Exploring Dynamic Inventory in Ansible for AWS EC2 Instances

Ayushmaan Srivastav
2 min readFeb 24, 2024

--

Introduction: Dynamic inventory in Ansible is a powerful feature that allows you to manage your infrastructure more efficiently, especially in dynamic environments like cloud platforms. In this blog, we will delve into the concept of dynamic inventory, its use cases, and provide a step-by-step guide on creating a dynamic inventory in Ansible to extract IP addresses from AWS EC2 instances.

Understanding Dynamic Inventory:

Dynamic inventory in Ansible is a mechanism that enables Ansible to automatically discover and use the resources in your infrastructure. Unlike static inventory files that require manual updates, dynamic inventory scripts fetch real-time information, making it ideal for dynamic environments like cloud platforms.

Use Cases of Dynamic Inventory:

  1. Cloud Environments: Dynamic inventory is particularly beneficial in cloud environments where resources are frequently added or removed.
  2. Scaling Infrastructure: It is useful when dealing with large-scale infrastructures that dynamically scale based on demand.
  3. Dynamic IP Address Assignment: In scenarios where IP addresses are not static, dynamic inventory ensures the most up-to-date information is used during playbook execution.

Creating a Dynamic Inventory for AWS EC2 Instances:

Step 1: Install Boto3 and Botocore

Ensure you have Boto3 and Botocore installed, as they are essential for interacting with AWS services.

pip install boto3 botocore

Step 2: Configure AWS CLI

Make sure your AWS CLI is configured with the necessary credentials.

aws configure

Step 3: Create a Dynamic Inventory Script

Create a Python script, e.g., aws_inventory.py, to fetch EC2 instance information dynamically.

#!/usr/bin/env python
import boto3

def get_ec2_inventory():
ec2 = boto3.resource(‘ec2’)
instances = ec2.instances.all()

inventory = {
‘_meta’: {
‘hostvars’: {}
}
}

for instance in instances:
inventory[‘_meta’][‘hostvars’][instance.public_ip_address] = {
‘ansible_ssh_user’: ‘your-ssh-user’,
‘ansible_ssh_private_key_file’: ‘path-to-your-private-key.pem’
# Add more host-specific variables as needed
}

return inventory

if __name__ == “__main__”:
print(get_ec2_inventory())

*make sure to make the indentation right.

Step 4: Make the Script Executable

chmod +x aws_inventory.py

Step 5: Test the Dynamic Inventory Script

Run the script to ensure it fetches the correct EC2 instance information.

./aws_inventory.py

Step 6: Configure Ansible

Update your Ansible configuration (ansible.cfg) to use the dynamic inventory script.

[defaults]
inventory = /path/to/aws_inventory.py

Conclusion:

Dynamic inventory in Ansible is a game-changer when it comes to managing dynamic environments efficiently. By following this step-by-step guide, you can create a dynamic inventory script for AWS EC2 instances, ensuring your Ansible playbooks always operate with the latest infrastructure information. This flexibility is crucial for modern cloud-based architectures, making Ansible an even more valuable tool in your automation toolkit.

--

--

No responses yet