# Trouble with using variables for ec2 tags

**URL:** <https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494>\
**Category:** Ansible Project\
**Tags:** aws\
**Created:** [June 18, 2014, 8:09pm UTC](https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494 "2014-06-18T20:09:42Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 18, 2014, 8:09pm UTC](https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494/1 "2014-06-18T20:09:42Z")

</div>

I'm trying to create a VPC with certain tags which are derived from  
variables. I feel like I can't find the right YAML/Jinja/whatever  
syntax to make it work though.

At first I tried setting them in the resource\_tags directly when using  
the ec2\_vpc module like so:

&nbsp;&nbsp;&nbsp;- name: Create VPC for new environment  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;module: ec2\_vpc  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;state: present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cidr\_block: 10.0.0.0/16  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;resource\_tags: { "Name": "marketplace-{{ environment }}" }  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;region: "{{ aws\_region }}"  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns\_hostnames: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dns\_support: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;internet\_gateway: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;validate\_certs: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;instance\_tenancy: default  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;wait: true  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;subnets:   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;route\_tables:   
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;register: new\_vpc

While this executes without errors it results in a VPC with the Name  
tag of "marketplace-{}" which is obviously not what I want.

And if my Name tag starts out with a "{" like "{{ environment  
}}-marketplace" then I get this error:

failed: [localhost] =\> {"failed": true, "parsed": false}  
invalid output was: Traceback (most recent call last):  
&nbsp;&nbsp;File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2\_vpc",  
line 2001, in \<module\>  
&nbsp;&nbsp;&nbsp;&nbsp;main()  
&nbsp;&nbsp;File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2\_vpc",  
line 599, in main  
&nbsp;&nbsp;&nbsp;&nbsp;(vpc\_dict, new\_vpc\_id, subnets\_changed, changed) =  
create\_vpc(module, vpc\_conn)  
&nbsp;&nbsp;File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2\_vpc",  
line 278, in create\_vpc  
&nbsp;&nbsp;&nbsp;&nbsp;previous\_vpc = find\_vpc(module, vpc\_conn, id, cidr\_block)  
&nbsp;&nbsp;File "/home/mpeters/.ansible/tmp/ansible-tmp-1403121708.45-114116167152383/ec2\_vpc",  
line 240, in find\_vpc  
&nbsp;&nbsp;&nbsp;&nbsp;if resource\_tags and  
set(resource\_tags.items()).issubset(set(vpc\_tags.items())):  
TypeError: unhashable type: 'dict'

So I though, maybe ec2\_vpc does run the Jinja templating on the values  
in the resource\_tags dictionary. So I then tried to create the VPC  
with a dummy name tag, save it's vpc\_id and then use ec2\_tag to add  
the tags in a separate task:

&nbsp;&nbsp;&nbsp;&nbsp;- name: Tag the new VPC  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;local\_action: ec2\_tag resource={{ new\_vpc.vpc\_id }} region={{  
aws\_region }} state=present  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;args:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags:  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Name: "marketplace-{{ environment }}"

But this results in the same thing (vpc with a Name tag of  
"marketplace-{}" instead of something like "marketplace-branch-XYZ".

Am I confused about the YAML/Jinja syntax to accomplish what I want or  
is using variables in EC2 tags not supported? And if it's not  
supported would pull requests to add it be something folks would like?

Thanks

---

<div class="post-metadata">

**Author:** ![James\_Cammarata](https://avatars.discourse-cdn.com/v4/letter/j/7c8e57/32.png) [@James\_Cammarata](https://forum.ansible.com/u/James_Cammarata)\
**Post date:** [June 19, 2014, 5:18am UTC](https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494/2 "2014-06-19T05:18:56Z")

</div>

This is definitely the same YAML parsing error, but because it’s happening in the complex args it doesn’t seem to be triggering the same error detection/handling code. Could you please open a github issue for this as well?

Thanks!

---

<div class="post-metadata">

**Author:** ![Michael\_Peters](https://avatars.discourse-cdn.com/v4/letter/m/c2a13f/32.png) [@Michael\_Peters](https://forum.ansible.com/u/Michael_Peters)\
**Post date:** [June 19, 2014, 1:40pm UTC](https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494/3 "2014-06-19T13:40:49Z")

</div>

I've filed it as 2 separate issues since they could be related but  
different (one completing but with the wrong results and the other  
failing with the traceback):

[https://github.com/ansible/ansible/issues/7833](https://github.com/ansible/ansible/issues/7833)  
[https://github.com/ansible/ansible/issues/7835](https://github.com/ansible/ansible/issues/7835)

But I'm still a bit confused about the why the first attempt I  
mentioned is failing. It's not throwing the traceback, it's just  
giving weird results. Is this still a YAML error? Can I write my YAML  
in a way that doesn't trigger this problem? Or is there a limit on  
which values can be templated?

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:** [June 19, 2014, 9:22pm UTC](https://forum.ansible.com/t/trouble-with-using-variables-for-ec2-tags/15494/4 "2014-06-19T21:22:10Z")

</div>

Already got this on github, yes, but the problem is the ‘environment’ variable in Ansible is reserved.

The “environment” keyword stores variables here.

We may consider changing this to ansible\_environment as this occasionally happens.
