Ansible AWS EC2 Dynamic Inventory Plugin

Ansible AWS EC2 Dynamic Inventory Plugin

ยท

3 min read

  • Ansible can pull inventory information from dynamic sources by various dynamic inventory plugins.
  • One of them is the aws_ec2 plugin, a great way to manage AWS EC2 Linux instances without having to maintain a standard local inventory.

Here is just a quick example of how to use it.

1. Install aws_ec2 ansible plugin amazon.aws.aws_ec2 โ€“ EC2 inventory source Note: Uses a YAML configuration file that ends with aws_ec2.(yml|yaml)

2. Setup ansible.cfg

[defaults]
enable_plugins = aws_ec2
host_key_checking = False
pipelining = True
remote_user = ec2-user
private_key_file=/pem/key-pem

3. Create inventory my_aws_ec2.yml file to group target Filter here is tag:name and state of the instance (running)

---
plugin: aws_ec2
aws_profile: default
regions:
  - us-east-1
filters:
  tag:Name:
    - dev-*
    - share-resource
    - hotfix
  instance-state-name : running
keyed_groups:
  - prefix: env
    key: tags['env']
  - prefix: dev
    key: tags['ssm']

4. Check the list and the host group

$ ansible-inventory -i my_aws_ec2.yml --list
    "all": {
        "children": [
            "aws_ec2",
            "env_dev",
            "dev_ssm",
            "ungrouped"
        ]
    },
    "aws_ec2": {
        "hosts": [
            "ec2-111-111-111-111.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-112.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-113.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-114.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-115.us-east-1.compute.amazonaws.com",
        ]
    },
    "env_dev": {
        "hosts": [
            "ec2-111-111-111-111.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-112.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-113.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-114.us-east-1.compute.amazonaws.com",
            "ec2-11-111-111-115.us-east-1.compute.amazonaws.com",
        ]
    },
    "dev_ssm": {
        "hosts": [
            "ec2-111-111-111-111.us-east-1.compute.amazonaws.com"
        ]
    }

5. Now send the task to the expected group The task here is to update the env files to to env_dev group. File name and value are parsed from ansible host and item list (Ansible echo into file)

update_env.yaml

---
- hosts: all
  become: yes
  tasks:
    - name: Get hostname
      command: echo {{ ansible_hostname.split('-')[1] }}
      register: hostname

    - name: Update env files
      become: yes
      become_user: root
      shell: |
        echo "AGENT_ID={{ hostname.stdout }}-{{ item }}::" >> "/opt/workdir/{{ item }}.env"
      with_items:
        - app
        - pet
        - gate
        - api
      tags: runcmd
      register: result

    - name: Print output
      debug:
        var: result

Run

$ ansible-playbook update_env.yaml -i my_aws_ec2.yml --limit env_dev -vv