Dynamic Inventory Management with AWS from ansible

Hamza AfzalHamza Afzal
1 min read

Objective

Use Ansible's dynamic inventory script to retrieve and categorize AWS EC2 instances based on tags or regions.

Step 1: Create a Python Script for Dynamic Inventory

Create a Python script named dynamic_inventory.py that will interact with the AWS SDK (Boto3) to fetch EC2 instance details.

#!/usr/bin/env python3

import boto3
import json

def get_ec2_instances():
    # Create an EC2 client
    ec2_client = boto3.client('ec2')

    # Describe EC2 instances
    instances = ec2_client.describe_instances()

    # Initialize lists and sets to store data
    ip_addresses = []
    regions = set()
    tags = set()

    # Loop through reservations and instances to extract details
    for reservation in instances['Reservations']:
        for instance in reservation['Instances']:
            # Check if the instance has a public IP address
            if 'PublicIpAddress' in instance:
                ip_addresses.append(instance['PublicIpAddress'])

            # Add the availability zone to the regions set
            regions.add(instance['Placement']['AvailabilityZone'])

            # Extract tags and add them to the tags set
            for tag in instance.get('Tags', []):
                tags.add(f"{tag['Key']}: {tag['Value']}")

    # Constructing inventory structure
    inventory_data = {
        "IP_Addresses": ip_addresses,
        "Regions": list(regions),
        "Tags": list(tags)
    }

    # Print the inventory data in JSON format
    print(json.dumps(inventory_data, indent=4))

if __name__ == "__main__":
    get_ec2_instances()

Step 2: Make the Script Executable

chmod +x filename.py

Step 3: Test Your Dynamic Inventory Script

You can test your dynamic inventory setup by running:

ansible -i /path/to/your/custom_inventory.py all -m debug -a "msg={{ hostvars }}"
0
Subscribe to my newsletter

Read articles from Hamza Afzal directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Hamza Afzal
Hamza Afzal