AWS-EC2 Ansible dynamic inventory implementation
The below requirements are needed on the local ansible controller node that executes this inventory.
python >= 3.6 boto3 >= 1.18.0 botocore >= 1.21.0 aws cli
pip install boto3
sudo yum install ansible -y
sudo yum install -y amazon-linux-extras
sudo yum install python3.8
sudo amazon-linux-extras enable python3.8
sudo yum install python3.8 -y
alias python=python3.8
ansible-galaxy collection install amazon.aws
plugin: aws_ec2
aws_profile: default
regions:
- ap-northeast-1
command: ansible-inventory -i aws_ec2.yaml --list
###result:
"all": {
"children": [
"aws_ec2",
"ungrouped"
]
},
"aws_ec2": {
"hosts": [
"ec2-13-230-17-252.ap-northeast-1.compute.amazonaws.com",
"ec2-18-183-254-106.ap-northeast-1.compute.amazonaws.com",
"ec2-18-183-59-236.ap-northeast-1.compute.amazonaws.com",
"ec2-43-207-100-0.ap-northeast-1.compute.amazonaws.com",
"ec2-54-249-82-218.ap-northeast-1.compute.amazonaws.com",
"ec2-54-250-163-242.ap-northeast-1.compute.amazonaws.com",
"ec2-54-250-197-106.ap-northeast-1.compute.amazonaws.com"
]
}
}
plugin: aws_ec2
aws_profile: default
filters:
tag:OS:
- ubuntu-linux
command: ansible-inventory -i aws_ec2.yaml --list
},
"all": {
"children": [
"aws_ec2",
"ungrouped"
]
},
"aws_ec2": {
"hosts": [
"ec2-18-183-254-106.ap-northeast-1.compute.amazonaws.com",
"ec2-54-250-163-242.ap-northeast-1.compute.amazonaws.com"
]
}
}
command: ansible-inventory -i aws_ec2.yaml --graph
@all:
|--@aws_ec2:
| |--ec2-18-183-254-106.ap-northeast-1.compute.amazonaws.com
| |--ec2-54-250-163-242.ap-northeast-1.compute.amazonaws.com
|--@ungrouped:
plugin: aws_ec2
aws_profile: default
filters:
tag:OS:
- ubuntu-linux
- amazon-linux
hostnames:
- ip-address
@all:
|--@aws_ec2:
| |--13.230.17.252
| |--18.183.254.106
| |--54.249.82.218
| |--54.250.163.242
|--@ungrouped:
plugin: aws_ec2
aws_profile: default
hostnames:
- ip-address
keyed_groups:
- prefix: distro
key: tags.OS
@all:
|--@aws_ec2:
| |--13.230.17.252
| |--18.183.254.106
| |--18.183.59.236
| |--43.207.100.0
| |--54.249.82.218
| |--54.250.163.242
| |--54.250.197.106
|--@distro_amazon_linux:
| |--13.230.17.252
| |--54.249.82.218
|--@distro_ubuntu_linux:
| |--18.183.254.106
| |--54.250.163.242
|--@ungrouped:
plugin: aws_ec2
aws_profile: default
hostnames:
- ip-address
groups:
amazon_linux2: "'amazon-linux' in tags.OS"
ubuntu: "'ubuntu-linux' in tags.OS"
@all:
|--@amazon_linux2:
| |--13.230.17.252
| |--54.249.82.218
|--@aws_ec2:
| |--13.230.17.252
| |--18.183.254.106
| |--18.183.59.236
| |--43.207.100.0
| |--54.249.82.218
| |--54.250.163.242
| |--54.250.197.106
|--@ubuntu:
| |--18.183.254.106
| |--54.250.163.242
|--@ungrouped: