EC2 inventory source enhancement: set the hostname to a tag

Hi everyone,

I put this little patch together for a customer a few months ago, and a number of people have found it useful:

https://github.com/tgerla/ansible/compare/ec2-fqdn

Basically it allows you to set the inventory hostname of an EC2 host to the contents of a tag. If the specified tag doesn’t exist for a host, it defaults to the normal EC2 hostname. For example, if your instance has a “tag:Name=foobar” and you specify hostname_tag_map=Name, that instance will come out of inventory with the inventory hostname of “foobar” and the ansible_ssh_host variable will be set to the ec2-xxx-xxx-xxx-xxx hostname.

This has been tested by a number of people, and it’s pretty simple. Only one question: I’m not totally happy with the parameter name of “hostname_tag_map” so suggestions would be appreciated.

I’ll do a pull request shortly, but I’m curious to know what people think!

-Tim

I think https://github.com/ansible/ansible/pull/7601 mostly does the same thing using “tag_destination_variable” as the parameter, though it doesn’t set ansible_ssh_host. That parameter name seems more logical to me since it does the same thing as destination_variable, but using tags.

https://github.com/ansible/ansible/pull/7395 allows for using any other host variable (except tags) to set the hostname, and it does set ansible_ssh_host.

It also appears that https://github.com/ansible/ansible/pull/9630 might really be looking for this feature and just doesn’t know it yet.