Determine some sensible tags to pass non-critical information to Ansible or other CM solutions:
resource "aws_instance" "main" {
...
tags {
...
"some_tag" = "waka"
}
}
Make sure the dynamic inventory config file (ec2.ini
) has at least the group_by_tag_keys
option commented out
...
# The EC2 inventory output can become very large. To manage its size,
# configure which groups should be created.
# group_by_key_pair = True
group_by_tag_keys = True
At command runtime:
ansible-playbook ... --limit tag_some_tag_Waka
In the playbook:
- hosts: tag_some_tag_Waka
{{ec2_tag_some_tag}}
For this to work the instance must have the AWS CLI installed and have an IAM role assigned with the necessary permissions.
- name: get instance tags
shell: aws ec2 describe-tags --filters="Name=resource-id,Values=$(ec2metadata --instance-id)"
environment:
AWS_DEFAULT_REGION: us-east-1
register: instance_data