- 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
Top comments (6)
This is the error I am getting while fetching the inventory
Please check one of the following
Still, I am not able to get it working.
Output of Describe Instances:
Output of
ansible-inventory -i aws_ec2.yml --list
Did you install aws_ec2 plugin? Read more here: docs.ansible.com/ansible/latest/co...
Plus, it requires boto3 and botocore python modules.
Thanks! its working, actually the issue was with the file name, as mentioned in the given documentation its should end end with aws_ec2.(yml/yaml)
Great!