# Add hosts to the inventory by their Name (or other) tag in ec2

**URL:** <https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153>\
**Category:** Ansible Developer\
**Created:** [May 4, 2014, 7:39am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153 "2014-05-04T07:39:03Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 4, 2014, 7:39am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/1 "2014-05-04T07:39:03Z")

</div>

Related to PRs: [https://github.com/ansible/ansible/pull/6721](https://github.com/ansible/ansible/pull/6721) and [https://github.com/ansible/ansible/pull/3697](https://github.com/ansible/ansible/pull/3697)

I know you rejected similar changes in the past, but I think seeing some indicative hostname in playbook runs is very useful. I generate my machine’s hostname on creation (so they’re still ‘cattle’), and ‘prod-nginx-3’ is more useful than “[ec2-xx-xx-xx.kill.me.now.com](http://ec2-xx-xx-xx.kill.me.now.com)”. 🙂

I modified the ec2 inventory script and added a ‘hostname\_variable’ config option, that changes the name the host is added to the inventory with. It can use tags or instance variables. The addressing still uses destination\_variable, and the default behavior if not defined is to use the destination\_variable.

Just wanted to ask if the previous rejections are due to bad implementation or conscious decision against the idea. Also ideas for improvement are welcome.

Thanks.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 6, 2014, 3:45am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/2 "2014-05-06T03:45:00Z")

</div>

It’s fine if you want to modify ec2.py to do something different for your environment – doesn’t mean it’s right for core, however.

This part:

" The addressing still uses destination\_variable, and the default behavior if not defined is to use the destination\_variable."

I have a hard time parsing this.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 6, 2014, 7:18am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/3 "2014-05-06T07:18:31Z")

</div>

Hi Michael, thanks for the reply.

> It’s fine if you want to modify ec2.py to do something different for your environment – doesn’t mean it’s right for core, however.

Well, of course. But I would prefer to see it in core as I think I’m the only once who needs it, hence my questions about the reasons similar changes were rejected in the past. 🙂

> This part:
> 
> " The addressing still uses destination\_variable, and the default behavior if not defined is to use the destination\_variable."
> 
> I have a hard time parsing this.

Gaaah, forgot to end that sentence. The default is to use destination\_variable for the name as well. Example:

destination\_variable = public\_dns\_name  
hostname\_variable = tag\_Name

inventory\_name will be according to the Name tag, while ansible\_ssh\_host will be the instance’s public DNS name. If the tag isn’t defined or the whole config line is commented out, the public DNS name will be the inventory\_name as well.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 6, 2014, 7:19am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/4 "2014-05-06T07:19:21Z")

</div>

> Hi Michael, thanks for the reply.
> 
> > It’s fine if you want to modify ec2.py to do something different for your environment – doesn’t mean it’s right for core, however.
> 
> Well, of course. But I would prefer to see it in core as I think I’m the only once who needs it, hence my questions about the reasons similar changes were rejected in the past. 🙂

\*not the only once… proofreading after waking up not a good idea.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 13, 2014, 11:55pm UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/5 "2014-05-13T23:55:47Z")

</div>

Your explanation helps me understand a good bit.

As long as the documentation explains what this does and the defaults do what things do now.

However, I’m still not entirely sold on how many people would use a tag to assign the hostname, to make it generally applicable, so a bit on the fence yet.

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [May 14, 2014, 4:51am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/6 "2014-05-14T04:51:26Z")

</div>

We also use the Name tag for hostnames. As long as hostname\_variable could be tag\_Whatever (i.e. not restricted to just Name) this could be very useful to us too.

Will

---

<div class="post-metadata">

**Author:** ![willthames](https://avatars.discourse-cdn.com/v4/letter/w/8dc957/32.png) [@willthames](https://forum.ansible.com/u/willthames)\
**Post date:** [May 14, 2014, 4:51am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/7 "2014-05-14T04:51:48Z")

</div>

We also use the Name tag for hostnames. As long as hostname\_variable could be tag\_Whatever (i.e. not restricted to just Name) this could be very useful to us too.

Will

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [May 15, 2014, 6:24am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/8 "2014-05-15T06:24:13Z")

</div>

We have definitely had instances where we wanted to set hostvars in ec2. What about using tag\_hostname\_[hostname]?

Similarly, it would be really nice to be able to assign groups with tag\_(group|role)\_[name] so that we can use the same group vars between cloud providers and bare metal.

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 15, 2014, 6:33am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/9 "2014-05-15T06:33:08Z")

</div>

A lot of what you want can be done with a combination of static (even empty) inventory for defining group/host vars and the ec2 script. Put them both in a directory and use that as your inventory.

As for assigning groups, the ec2 script groups by ec2 tags already. You can use the static inventory to put the generated ec2 groups as child groups of your bare-metal ones.

---

<div class="post-metadata">

**Author:** ![Peter\_Gehres](https://avatars.discourse-cdn.com/v4/letter/p/f17d59/32.png) [@Peter\_Gehres](https://forum.ansible.com/u/Peter_Gehres)\
**Post date:** [May 15, 2014, 7:03am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/10 "2014-05-15T07:03:03Z")

</div>

> A lot of what you want can be done with a combination of static (even  
> empty) inventory for defining group/host vars and the ec2 script. Put them  
> both in a directory and use that as your inventory.

If I am understanding this correctly, I've never heard of this. Is there  
documentation anywhere for this method of inventory?

That seems sub-optimal and not very easy for new users as well as for  
troubleshooting. We actually share our roles among many teams in the  
company where each team has a different ansible-playbooks and AWS account  
(ops, qa, perf, development, support, etc). Not being able to have this be  
automagical when another user utilizes a shared role makes things more  
difficult.

This certainly isn't a show-stopper but it would make things a lot more  
friendly.

> As for assigning groups, the ec2 script groups by ec2 tags already. You  
> can use the static inventory to put the generated ec2 groups as child  
> groups of your bare-metal ones.

Oh, we do assign groups with tags. I am just tired of seeing "tag\_" in my  
group names and not having these map 1:1 to role names like everything else  
🙂

Apparently I also forgot that you can't have multiple tags with the same  
key so that idea also gets more fuzzy (I guess you'd have to do something  
like tag\_group\_g1\_g2\_g3 and then split the value on \_).

---

<div class="post-metadata">

**Author:** ![Hagai\_Kariti](https://avatars.discourse-cdn.com/v4/letter/h/4491bb/32.png) [@Hagai\_Kariti](https://forum.ansible.com/u/Hagai_Kariti)\
**Post date:** [May 15, 2014, 8:01am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/11 "2014-05-15T08:01:02Z")

</div>

> > A lot of what you want can be done with a combination of static (even empty) inventory for defining group/host vars and the ec2 script. Put them both in a directory and use that as your inventory.
> 
> If I am understanding this correctly, I’ve never heard of this. Is there documentation anywhere for this method of inventory?

It’s briefly mentioned here: [http://docs.ansible.com/intro\_dynamic\_inventory.html](http://docs.ansible.com/intro_dynamic_inventory.html), at the bottom.

> That seems sub-optimal and not very easy for new users as well as for troubleshooting. We actually share our roles among many teams in the company where each team has a different ansible-playbooks and AWS account (ops, qa, perf, development, support, etc). Not being able to have this be automagical when another user utilizes a shared role makes things more difficult.

Seems very simple to me:  
inventory/  
ec2.py  
ec2.ini  
static  
host\_vars/  
group\_vars/

is pretty elegant imho. Put group and hostvars in their relevant directories (you can do that now because you have a static inventory file), and define group membership/hierarchy and bare-metal machines in static.

But this is turning into an ansible-project discussion, so let’s stop here 🙂

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 10:11pm UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/12 "2014-05-15T22:11:19Z")

</div>

> > A lot of what you want can be done with a combination of static (even  
> > empty) inventory for defining group/host vars and the ec2 script. Put them  
> > both in a directory and use that as your inventory.
> 
> If I am understanding this correctly, I've never heard of this. Is there  
> documentation anywhere for this method of inventory?
> 
> That seems sub-optimal and not very easy for new users as well as for  
> troubleshooting. We actually share our roles among many teams in the  
> company where each team has a different ansible-playbooks and AWS account  
> (ops, qa, perf, development, support, etc). Not being able to have this be  
> automagical when another user utilizes a shared role makes things more  
> difficult.

You can have an inventory directory full of dynamic and file sources, and  
it acts as one inventory source if you -i the directory.

It's quite useful if you have EC2 and Rackspace in one source, or a mixture  
of EC2 and on-premise.

If you don't like it, just don't use it. Don't have to claim it's not  
easy or anything not even having tried it.

> As for assigning groups, the ec2 script groups by ec2 tags already. You
> 
> > can use the static inventory to put the generated ec2 groups as child  
> > groups of your bare-metal ones.
> 
> Oh, we do assign groups with tags. I am just tired of seeing "tag\_" in  
> my group names and not having these map 1:1 to role names like everything  
> else 🙂

I'd suggest you write your own inventory script! The provided ones are  
merely an example.

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan1](https://avatars.discourse-cdn.com/v4/letter/m/5e9695/32.png) [@Michael\_DeHaan1](https://forum.ansible.com/u/Michael_DeHaan1)\
**Post date:** [May 15, 2014, 10:14pm UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/13 "2014-05-15T22:14:00Z")

</div>

Relevant pull request to this thread: [https://github.com/ansible/ansible/pull/7395](https://github.com/ansible/ansible/pull/7395)

---

<div class="post-metadata">

**Author:** ![Jean-Baptiste\_Barth](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@Jean-Baptiste\_Barth](https://forum.ansible.com/u/Jean-Baptiste_Barth)\
**Post date:** [December 26, 2014, 11:06am UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/14 "2014-12-26T11:06:03Z")

</div>

Hi,

I just found this thread and I can only speak for my own use case, but having the instance name and region in ansible failing runs is quite useful to me, so we can debug later in the AWS console or elsewhere.

I was wondering if you think that “promoting” route53 aliases as the main name of an instance would be a better practice than tags ? I implemented it here: [https://github.com/jbbarth/ansible/commit/f66ea2c0be2d9d007c07c04252d777193fcdddee](https://github.com/jbbarth/ansible/commit/f66ea2c0be2d9d007c07c04252d777193fcdddee) and I’d be very happy to improve it in the next few days/weeks by adding tests or improving the code if you think a merge upstream is possible.

I wanted to have your opinion before submitting a pull request. Let me know what you think.

Anyway thanks for a fantastic tool, keep up the good work!

Cheers,

---

<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:** [December 26, 2014, 1:14pm UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/15 "2014-12-26T13:14:59Z")

</div>

That change seems reasonable to me.

Along with the existing PRs for setting an alternate hostname (  
[https://github.com/ansible/ansible/pull/7395](https://github.com/ansible/ansible/pull/7395),  
[https://github.com/ansible/ansible/pull/7601](https://github.com/ansible/ansible/pull/7601) and  
[https://github.com/ansible/ansible/pull/9825](https://github.com/ansible/ansible/pull/9825)), it provides another option  
for certain use cases.

---

<div class="post-metadata">

**Author:** ![Jean-Baptiste\_Barth](https://avatars.discourse-cdn.com/v4/letter/j/ee7513/32.png) [@Jean-Baptiste\_Barth](https://forum.ansible.com/u/Jean-Baptiste_Barth)\
**Post date:** [December 26, 2014, 6:56pm UTC](https://forum.ansible.com/t/add-hosts-to-the-inventory-by-their-name-or-other-tag-in-ec2/15153/16 "2014-12-26T18:56:19Z")

</div>

Thanks for the feedback. I opened a PR here: [https://github.com/ansible/ansible/pull/9900](https://github.com/ansible/ansible/pull/9900) ; any other feedback welcome !

Cheers,
