# EC2 inventory source enhancement: set the hostname to a tag

**URL:** <https://forum.ansible.com/t/ec2-inventory-source-enhancement-set-the-hostname-to-a-tag/16884>\
**Category:** Ansible Developer\
**Created:** [November 25, 2014, 9:19pm UTC](https://forum.ansible.com/t/ec2-inventory-source-enhancement-set-the-hostname-to-a-tag/16884 "2014-11-25T21:19:00Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![Timothy\_Gerla](https://avatars.discourse-cdn.com/v4/letter/t/977dab/32.png) [@Timothy\_Gerla](https://forum.ansible.com/u/Timothy_Gerla)\
**Post date:** [November 25, 2014, 9:19pm UTC](https://forum.ansible.com/t/ec2-inventory-source-enhancement-set-the-hostname-to-a-tag/16884/1 "2014-11-25T21:19:00Z")

</div>

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](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

---

<div class="post-metadata">

**Author:** ![Chris\_Church1](https://avatars.discourse-cdn.com/v4/letter/c/ac8455/32.png) [@Chris\_Church1](https://forum.ansible.com/u/Chris_Church1)\
**Post date:** [November 26, 2014, 3:41am UTC](https://forum.ansible.com/t/ec2-inventory-source-enhancement-set-the-hostname-to-a-tag/16884/2 "2014-11-26T03:41:13Z")

</div>

I think [https://github.com/ansible/ansible/pull/7601](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](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](https://github.com/ansible/ansible/pull/9630) might really be looking for this feature and just doesn’t know it yet.
