Ansible: ec2 aws plugin - How to associate ec2 host to ansible group based on aws_profile?

Viewed 193

I'm sure there must be a simple answer, but, when I use the aws_ec2 plugin to create a dynamic inventory for Ansible, I use the following setup so that I can pass the profile in to the inventory (to get the correct credentials):

plugin: aws_ec2
aws_profile: "{{ lookup('env', 'AWS_PROFILE') | default('dev-profile', true) }}"
regions:
  - us-east-1

Now, I want all of these hosts to be associated with a group which has the name of the AWS_PROFILE passed in. How do I do this?

I tried:

groups:
    dev_group: aws_profile == 'dev-profile'
    test_group: aws_profile == 'test-profile'

but was unsuccessful (no groups were created).

1 Answers

The fact that you sets a value to the parameter aws_profile in your inventory does not makes it available in a variable.

So what you have to do is to reuse the same lookup in your groups conditions:

groups:
  dev_group: lookup('env', 'AWS_PROFILE') | default('dev-profile', true) == 'dev-profile'
  test_group: lookup('env', 'AWS_PROFILE') | default('dev-profile', true) == 'test-profile'
Related