# ec2\_tag module: unable to delete tags using variable names

**URL:** <https://forum.ansible.com/t/ec2-tag-module-unable-to-delete-tags-using-variable-names/16469>\
**Category:** Ansible Project\
**Created:** [October 8, 2014, 6:07pm UTC](https://forum.ansible.com/t/ec2-tag-module-unable-to-delete-tags-using-variable-names/16469 "2014-10-08T18:07:47Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Bruce\_Pennypacker](https://avatars.discourse-cdn.com/v4/letter/b/0ea827/32.png) [@Bruce\_Pennypacker](https://forum.ansible.com/u/Bruce_Pennypacker)\
**Post date:** [October 8, 2014, 6:07pm UTC](https://forum.ansible.com/t/ec2-tag-module-unable-to-delete-tags-using-variable-names/16469/1 "2014-10-08T18:07:47Z")

</div>

I’m trying to do something that I thought was fairly straightforward but I’m not having any luck. In a nutshell I need to delete a tag from an EC2 instance, but the name is a concatenation of a variable and a constant. What I’m trying is basically this:

vars:

- foo: sometag

- name: Delete tag  
local\_action: ec2\_tag state=absent  
aws\_access\_key={{ AWS\_ACCESS\_KEY\_ID }}  
aws\_secret\_key={{ AWS\_SECRET\_ACCESS\_KEY }}  
region={{ AWS\_REGION }}  
resource={{ instanceId }}  
args:  
tags:  
“{{ foo }}-bar”: some\_value

The problem is that “{{ foo }}-bar” doesn’t seem to get converted into “sometag-bar” before the module is called. (foo is actually a fact of the host, so I can’t just statically set the tag name) Bug? Feature?

Unrelated issue (or maybe not), but also dealing with the ec2\_tag module: What’s the correct way to reference the value of a tag when trying to delete it? For whatever reason when using “state=absent” the ec2\_module will only remove a tag if you specify both the name and the current value of the tag. In other words, if you have an instance with a tag named foo that has a value of “bar” then the following will not remove the tag:

- name: Delete tag  
local\_action: ec2\_tag state=absent  
aws\_access\_key={{ AWS\_ACCESS\_KEY\_ID }}  
aws\_secret\_key={{ AWS\_SECRET\_ACCESS\_KEY }}  
region={{ AWS\_REGION }}  
resource={{ instanceId }}  
args:  
tags:  
foo: ‘’

The only way to get the ec2\_tag module to actually delete the tag is to specify both the name & value of the tag, so you need to set the args as:

args:  
tags:  
foo: bar

Because of this, and the fact that some of our tags have dynamic values, I’m trying to do something like this:

- name: Get tags

local\_action: ec2\_tag state=list  
aws\_access\_key={{ AWS\_ACCESS\_KEY\_ID }}  
aws\_secret\_key={{ AWS\_SECRET\_ACCESS\_KEY }}  
region={{ AWS\_REGION }}  
resource={{ instanceId }}  
register: mytags

- name: Delete env tag  
local\_action: ec2\_tag state=absent  
aws\_access\_key={{ AWS\_ACCESS\_KEY\_ID }}  
aws\_secret\_key={{ AWS\_SECRET\_ACCESS\_KEY }}  
region={{ AWS\_REGION }}  
resource={{ instanceId }}  
args:  
tags:  
foo: mytags[foo]

But I’m unable to get anything here to work either, even if I set the name of the tag to a static value. What’s the proper way to reference the tags from “state=list” so that you can do something like delete a tag?

-Bruce
