DevOps Classroom notes 06/Dec/2026

AWS Ansible Dynamic Inventory

  • IAM Role / IAM User

    • EC2 instance describe and list
    • Region
  • Ansible Server

    • Ansible
    • amazon.aws plugin
    • AWS CLI
    • Python Boto3
  • Configure inventory file

Auth Methods

Environment Variables

# Environment variables
export AWS_ACCESS_KEY_ID='AK123'
export AWS_SECRET_ACCESS_KEY='abc123'

AWS Access Key and Secret Key

File: demo.aws_ec2.yml

plugin: amazon.aws.aws_ec2

# The access key for your AWS account.
aws_access_key: <YOUR-AWS-ACCESS-KEY-HERE>

# The secret access key for your AWS account.
aws_secret_key: <YOUR-AWS-SECRET-KEY-HERE>

IAM Role

# demo.aws_ec2.yml
plugin: amazon.aws.aws_ec2
iam_role_arn: arn:aws:iam::1234567890:role/assumed-ansible

Official Documentation

Note: Profile-based authentication is also supported.

Install Ansible Steps

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository --yes --update ppa:ansible/ansible
sudo apt install ansible -y
ansible --version

Install AWS Plugin

ansible-galaxy collection install amazon.aws --force

cd ~/.ansible/collections/ansible_collections/amazon/aws

sudo apt install -y python3-full python3-venv python3-pip

python3 -m venv venv
source venv/bin/activate

pip install -r requirements.txt

# AWS CLI
sudo apt install unzip -y

curl -fsSL https://awscli.amazonaws.com/v2/install.sh | sudo bash -s -- --system

Configure Inventory File

File: demo.aws_ec2.yml

plugin: amazon.aws.aws_ec2

# This sets the region. If empty (the default) default this will include all regions, except possibly
# restricted ones like us-gov-west-1 and cn-north-1.
regions:
  - us-east-2

keyed_groups:
  - key: tags.env
    prefix: enviroment
    separator: "_"

groups:
  enviroment_dev: tags.env == 'dev'

Configure Group Variables

File: group_vars/enviroment_dev.yaml

ansible_user: ubuntu
ansible_ssh_private_key_file: ~/.ssh/id_rsa

Validate

ansible -i demo.aws_ec2.yml -m ping enviroment_dev

Output

ec2-18-227-140-193.us-east-2.compute.amazonaws.com | SUCCESS => {
    "ansible_facts": {
        "discovered_interpreter_python": "/usr/bin/python3.14"
    },
    "changed": false,
    "ping": "pong"
}

Task

  1. Set up an Ansible server with the AWS plugin.
  2. Create 2 instances with the env tag: dev and prod.
  3. Check groups and pass different group variables.

Leave a comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Please turn AdBlock off
Plugin for Social Media by Acurax Wordpress Design Studio
Visit Us On FacebookVisit Us On LinkedinVisit Us On Youtube