Search code examples
amazon-web-servicesamazon-ec2ansibleansible-inventory

Ansible EC2 dynamic inventory - tag containing a hyphen


I'm trying to run an ad-hoc ansible command on hosts that have been tagged as Name = foo-bar (notice the hyphen). When I run:

ansible tag_Name_foo_bar -i ec2.py -m ping

I get: No hosts matched

However, there is such a host. If I run the same command against a host that is tagged with a name not containing a hyphen, it works fine, e.g for a host that is tagged Name = foobar, the following works:

ansible tag_Name_foobar -i ec2.py -m ping

H


Solution

  • What is your ansible version? It works for me. According to Example: AWS EC2 External Inventory Script

    Tags

    Each instance can have a variety of key/value pairs associated with it called Tags. The most common tag key is ‘Name’, though anything is possible. Each key/value pair is its own group of instances, again with special characters converted to underscores, in the format tag_KEY_VALUE e.g. tag_Name_Web can be used as is tag_Name_redis-master-001 becomes tag_Name_redis_master_001 tag_aws_cloudformation_logical-id_WebServerGroup becomes tag_aws_cloudformation_logical_id_WebServerGroup

    It is possible ansible's ec2 cache is not refreshed. Try:

    ec2.py --refresh-cache
    

    and then run your ansible command again. When I changed my instance tag name to foo_bar, ot worked correctly.