ansible/ansible — pull request #9496 by dstillman (2014-11-06 02:47:16+00:00)
Title: Run keys through Jinja

I ran into the same problem as in #7963 — I wanted to dynamically set the names for EC2 tags, in my case to record role info. (Since an instance might have multiple roles and EC2 tag names are unique, the role has to go in the name.) But trying to use a variable in the key didn't work:

```
- name: Set role state tag
local_action: ec2_tag resource={{ ec2_id }} region=us-east-1
args:
tags:
role-{{ service }}: "{{ 'started' if result.rc == 1 else 'stopped' }}"
```

Since only values are passed through Jinja, the literal "role-{{ service }}" would be set as the EC2 tag name.

Before discovering #7963, I fixed this by passing keys through Jinja as well. I'm not sure if there are any other core modules where it'd be helpful to dynamically set keys, but this seems like it could be a more natural approach in these situations than requiring the use of two separate variables. (If dynamic variables do pose a problem — performance? overriding other keys? — then the approach in #7963 would be fine.)