# issue setting tags on ec2 module

**URL:** <https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734>\
**Category:** Ansible Project\
**Created:** [July 22, 2014, 5:53pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734 "2014-07-22T17:53:52Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Hernandes\_Sousa](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@Hernandes\_Sousa](https://forum.ansible.com/u/Hernandes_Sousa)\
**Post date:** [July 22, 2014, 5:53pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734/1 "2014-07-22T17:53:52Z")

</div>

I’m having trouble setting up instance\_tags in the following playbook:

vars:  
key\_name: mykey  
instance\_type: t2.micro  
security\_group: sg-xxxxxxxx  
image: ami-f5b815e8  
region: sa-east-1  
subnet\_id: subnet-5eaxxxxb  
instance\_tags:

foo: bar

tasks:

- name: Launch instance  
local\_action: ec2  
key\_name={{ key\_name }}  
group\_id={{ security\_group }}  
instance\_type={{ instance\_type }}  
image={{ image }}  
wait=yes  
region={{ region }}  
assign\_public\_ip=yes  
vpc\_subnet\_id={{ subnet\_id }}  
**instance\_tags=“{{ instance\_tags }}” # not working**  
register: ec2

I get “msg: this module requires key=value arguments”, does anyone know the correct way to add the dictionary in this case? I’ve tried a variety of combinations without much success.

---

<div class="post-metadata">

**Author:** ![Brent\_Langston](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Brent\_Langston](https://forum.ansible.com/u/Brent_Langston)\
**Post date:** [July 22, 2014, 6:02pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734/2 "2014-07-22T18:02:15Z")

</div>

This might be old’ish style at this point, but this is how I do it:

- name: tag my launched instances  
local\_action: ec2\_tag resource={{ [item.id](http://item.id) }} region={{ region }} state=present  
with\_items: ec2.instances  
args:  
tags:  
Name: “{{assigned\_hostname.stdout}}”  
color: “{{oscar\_color.stdout}}”  
environment: “{{oscar\_environment}}”  
os: “{{os}}”  
owner: “{{owner}}”  
phi: “{{phi}}”  
pool: “{{pool}}”  
primary\_role: “{{primary\_role}}”  
branch: “{{branch}}”

Hope that helps.

---

<div class="post-metadata">

**Author:** ![Hernandes\_Sousa](https://avatars.discourse-cdn.com/v4/letter/h/eb8c5e/32.png) [@Hernandes\_Sousa](https://forum.ansible.com/u/Hernandes_Sousa)\
**Post date:** [July 22, 2014, 6:18pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734/3 "2014-07-22T18:18:58Z")

</div>

That was it Brent thanks a lot! You pointed me to the right direction. I knew the ec2\_tags was an option but I’d like to place the tags in the same call. So I followed the same structured as you proposed:

vars:  
key\_name: mykey  
instance\_type: t2.micro  
security\_group: sg-xxxxxxxx  
image: ami-f5b815e8  
region: sa-east-1  
subnet\_id: subnet-5eaxxxxb  
instance\_tags:

foo: bar

tasks:

- name: Launch instance  
local\_action: ec2  
key\_name={{ key\_name }}  
group\_id={{ security\_group }}  
instance\_type={{ instance\_type }}  
image={{ image }}  
wait=yes  
region={{ region }}  
assign\_public\_ip=yes  
vpc\_subnet\_id={{ subnet\_id }}  
args:  
instance\_tags: “{{ instance\_tags }}”  
register: ec2

I will raise a PR adding this example to the documentation may anyone need it.

If someone knows of a more elegant way of doing this please let me know.

Thanks  
Hernandes

---

<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:** [July 22, 2014, 7:13pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734/4 "2014-07-22T19:13:03Z")

</div>

You can stick absolutely everything under args if you want, might make it feel a bit more consistent.

Also “local\_action” is usually not needed in cloud provisioning tasks as you can just have a play that talks to localhost

- hosts: localhost  
tasks:
- ec2:  
x: 1  
y: 2
- …

etc

---

<div class="post-metadata">

**Author:** ![Brent\_Langston](https://avatars.discourse-cdn.com/v4/letter/b/73ab20/32.png) [@Brent\_Langston](https://forum.ansible.com/u/Brent_Langston)\
**Post date:** [July 24, 2014, 9:25pm UTC](https://forum.ansible.com/t/issue-setting-tags-on-ec2-module/15734/5 "2014-07-24T21:25:45Z")

</div>

yeah, this was written before localhost was handled the way it is today. I  
haven't gone back and edited this in quite some time.
