Selecting ansible_user in compose based on a ec2 inventory tag

64 Views Asked by At

I'm trying to get a list of ec2 hosts that are divided into two groups of tags tag_OS_ec2_user and tag_OS_ubuntu, but there is a problem with determining the value of ansible_user depending on the tag that includes the host for connecting to dynamic inventory hosts.

Here is the condition under which hosts are connected via login, depending on whether the host is included in the tag or not:

ec2-inventory.yml:

plugin: aws_ec2
regions:
  - eu-north-1

filters:

keyed_groups:
  - prefix: region_is
    key: placement.region
    
  - prefix: arch
    key: architecture

  - prefix: tag
    key: tags
  
compose:
  ansible_user: "{{ 'ec2-user' if 'tag_OS_ec2_user' in tags else ('ubuntu' if 'tag_OS_ubuntu' in tags else '') }}"

Inventory output:

"tag_OS_ec2_user": {
        "hosts": [
            "ec2-1-1-1-1.eu-north-1.compute.amazonaws.com",
            "ec2-2-2-2-2.eu-north-1.compute.amazonaws.com"
        ]
    },
    "tag_OS_ubuntu": {
        "hosts": [
            "ec2-3-3-3-3.eu-north-1.compute.amazonaws.com",
            "ec2-3-3-3-3.eu-north-1.compute.amazonaws.com"
        ]

But the problem is that even when passing ansible_user: {{ 'ec2-user' }} for only one tag tag_OS_ec2_user the ansible does not see the string in jinja format. When i pass just 'ec2-user' ssh login is successful

I want connect to host via ssh with a tag-based

0

There are 0 best solutions below