# ec2\_tag existing instances

**URL:** <https://forum.ansible.com/t/ec2-tag-existing-instances/17882>\
**Category:** Ansible Project\
**Created:** [March 31, 2015, 1:16pm UTC](https://forum.ansible.com/t/ec2-tag-existing-instances/17882 "2015-03-31T13:16:24Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Kegan\_Holtzhausen](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Kegan\_Holtzhausen](https://forum.ansible.com/u/Kegan_Holtzhausen)\
**Post date:** [March 31, 2015, 1:16pm UTC](https://forum.ansible.com/t/ec2-tag-existing-instances/17882/1 "2015-03-31T13:16:24Z")

</div>

Hi,

Sorry for a bit of a noobish question, but I am provisioning some instances in ec2, and I can successfully create and tag the instances in my _provision_ playbook.  
However, later I try and add a tag to exiting hosts based on a tag\_Name, but I cannot seem to get the resource-id’s I need for ec2\_tag.

Example

`

---

<div class="post-metadata">

**Author:** ![Ivan\_S\_Freitas](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ivan\_S\_Freitas](https://forum.ansible.com/u/Ivan_S_Freitas)\
**Post date:** [March 31, 2015, 4:01pm UTC](https://forum.ansible.com/t/ec2-tag-existing-instances/17882/2 "2015-03-31T16:01:23Z")

</div>

The ec2 (and hence the ec2.instance) variable is not defined in this  
scope, it only exists after the task responsible for creating a new  
instance is completed successfully.

To tag existing instances with hosts from EC2, you can use the ec2\_id  
variable defined by the aws dynamic inventory script. For example:

---

<div class="post-metadata">

**Author:** ![Kegan\_Holtzhausen](https://avatars.discourse-cdn.com/v4/letter/k/898d66/32.png) [@Kegan\_Holtzhausen](https://forum.ansible.com/u/Kegan_Holtzhausen)\
**Post date:** [April 1, 2015, 10:55am UTC](https://forum.ansible.com/t/ec2-tag-existing-instances/17882/3 "2015-04-01T10:55:58Z")

</div>

Thanks! that did the trick!

Are these variables documented anywhere in detail?

Thanks  
/K

---

<div class="post-metadata">

**Author:** ![Ivan\_S\_Freitas](https://avatars.discourse-cdn.com/v4/letter/i/e99b99/32.png) [@Ivan\_S\_Freitas](https://forum.ansible.com/u/Ivan_S_Freitas)\
**Post date:** [April 1, 2015, 5:25pm UTC](https://forum.ansible.com/t/ec2-tag-existing-instances/17882/4 "2015-04-01T17:25:29Z")

</div>

I think those variables are documented in the ec2 dynamic inventory  
script. I don't now if there's a way to only add a tag, but you could  
try using a different group in your hosts line, e.g., try selecting  
your hosts based in another tag value besides Name. Or you may be able  
to limit this task with a "when" clause using variables defined by the  
ec2 script, but I never tested this.
