1

I am trying to create custom groups, based on tags. I am using this doc as a reference: https://docs.ansible.com/ansible/latest/plugins/inventory.html#inventory-plugins

As a baseline, in my aws_ec2.yaml file, my keyed_groups are set up as such:

  - key: tags
    prefix: tag
  - key: placement.region
    prefix: aws_region

When I run ansible-inventory --graph, I see these tag groupings:

@all:
  |--@aws_ec2:
     -hostname
     -hostname
     -etc.
  |--@aws_region_us_west_2:
     -hostname
     -hostname
     -etc.
  |--@tag_Name_dev:
     -hostname
     -hostname
     -etc.
  |--@tag_environment_qa:
     -hostname
     -hostname
     -etc.

I now want to create groups of hosts based on the above tagging, as per the documentation.

From the documentation, I see they've set it up like so:

keyed_groups:
  # add hosts to tag_Name_value groups for each aws_ec2 host's tags.Name variable
  - key: tags.Name
    prefix: tag_Name_
    separator: ""
groups:
  # add hosts to the group development if any of the dictionary's keys or values is the word 'devel'
  development: "'devel' in (tags|list)"

I want to do the same for my aws_ec2.yaml and respective tags, so I try to emulate the example like so:

My keyed_groups and groups that I want:

keyed_groups:
  - key: tags
    prefix: tag
  - key: placement.region
    prefix: aws_region

groups:
  test: "'environment' in (tags|list)"

When I run ansible-inventory --graph, I see that the @test group is created and the host I want goes under the "test" group I created. But I can't get multiple hosts under it. I want multiple hosts under a group like in the example:

@all:
  |--@aws_ec2:
  |  |--ec2-12-345-678-901.compute-1.amazonaws.com
  |  |--ec2-98-765-432-10.compute-1.amazonaws.com
  |  |--...
  |--@development:
  |  |--ec2-12-345-678-901.compute-1.amazonaws.com
  |  |--ec2-98-765-432-10.compute-1.amazonaws.com
  |--@tag_Name_ECS_Instance:
  |  |--ec2-98-765-432-10.compute-1.amazonaws.com
  |--@tag_Name_Test_Server:
  |  |--ec2-12-345-678-901.compute-1.amazonaws.com
  |--@ungrouped

How can I get my inventory yaml to produce these groups like in the example? And some tags work while others do not. What are the rules for this? What is the syntax for matching the patterns? In the document's comment for the groups, it says "# add hosts to the group development if any of the dictionary's keys or values is the word 'devel'"

The doc mentions "If any of the key or values is the word 'devel'".. so can my pattern contain the word that I am looking for or does it have to be an exact match?

Also, what does the "(tags|list)" syntax signify? I've tried trial and error in trying different pattern matches, and it's not working. I've tried

The doc mentions: "If a host does not have the variables in the configuration above (in other words, tags.Name, tags, private_ip_address), the host will not be added to groups other than those that the inventory plugin creates and the ansible_host host variable will not be modified."

But my hosts do have the variables in configuration above, meaning they do contain a corresponding tags and private_ip_address value, as shown by ansible-inventory --graph. So the hosts should be added to the custom group I make.

Any idea how to get this to work? I really need this to work for my environment. Thank you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.