ec2_tag module: unable to delete tags using variable names

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