# how to get name tag from ec2\_facts

**URL:** <https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508>\
**Category:** Ansible Project\
**Created:** [September 19, 2013, 1:25pm UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508 "2013-09-19T13:25:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Brian\_Schott](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Brian\_Schott](https://forum.ansible.com/u/Brian_Schott)\
**Post date:** [September 19, 2013, 1:25pm UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508/1 "2013-09-19T13:25:56Z")

</div>

I have a playbook where I’m launching some instances, adding them to a host group, doing package installs, then I want to bundle each instance with a unique image name. My first thought was to use an instance tag, but can’t seem to find how to read tags on a running instance. Should I use set\_fact when I create the instance instead? Still getting my head around variable scoping.

Thanks!  
Brian

roles/ec2\_bundle\_image/main.yaml:

- name: Fetch the ec2\_facts for the server  
action: ec2\_facts

- name: Calculate the image name  
action: set\_fact ec2\_image\_name={{ NAME\_TAG\_GOES\_HERE }}_{{ ansible\_ec2\_profile }}_{{ ansible\_date\_time[date] }}

- name: Bundle the instance into an image  
local\_action: ec2\_ami aws\_access\_key={{ aws\_access\_key }} aws\_secret\_key={{ aws\_secret\_key }}  
instance\_id={{ ansible\_ec2\_instance\_id }} wait=yes  
name={{ ec2\_image\_name }}

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 20, 2013, 1:28am UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508/2 "2013-09-20T01:28:42Z")

</div>

Seems like the ec2\_facts module should return what you want since it hits the ec2 metadata server.

Shouldn’t need to use set\_fact here and I don’t see how that would possibly help since that sets variables, it doesn’t look them up.

---

<div class="post-metadata">

**Author:** ![Brian\_Schott](https://avatars.discourse-cdn.com/v4/letter/b/96bed5/32.png) [@Brian\_Schott](https://forum.ansible.com/u/Brian_Schott)\
**Post date:** [September 20, 2013, 3:06am UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508/3 "2013-09-20T03:06:36Z")

</div>

Unfortunately, the ec2 tags are not available inside the instance data.  
[http://stackoverflow.com/questions/3883315/query-ec2-tags-from-within-instance](http://stackoverflow.com/questions/3883315/query-ec2-tags-from-within-instance)

I didn’t realize I could add arbitrary variables during add\_host, so I think I can just set ec2\_image\_name to what I want in the add\_host action when I start the instances.

Otherwise, I don’t see a way to list the tags associated with an instance.

---

<div class="post-metadata">

**Author:** ![benno\_joy](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@benno\_joy](https://forum.ansible.com/u/benno_joy)\
**Post date:** [September 20, 2013, 3:30am UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508/4 "2013-09-20T03:30:58Z")

</div>

Hi Brian,

Maybe you can set the user data while instance provisioning

user\_data={{ foo }}

and use the ansible fact “ansible\_ec2\_user\_data” for image provisioning.

Regards,  
Benno

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [September 21, 2013, 1:03pm UTC](https://forum.ansible.com/t/how-to-get-name-tag-from-ec2-facts/13508/5 "2013-09-21T13:03:35Z")

</div>

It would be nice if the facts module could be extended to provide tag info so nodes could be self selecting.

In any case, the ec2 dynamic inventory script does return tag info.
