# Is it possible to use a variable as a key in a key:value pair?

**URL:** <https://forum.ansible.com/t/is-it-possible-to-use-a-variable-as-a-key-in-a-key-value-pair/24528>\
**Category:** Ansible Project\
**Created:** [May 19, 2017, 7:53pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-a-variable-as-a-key-in-a-key-value-pair/24528 "2017-05-19T19:53:57Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Dayton\_Jones](https://avatars.discourse-cdn.com/v4/letter/d/22d042/32.png) [@Dayton\_Jones](https://forum.ansible.com/u/Dayton_Jones)\
**Post date:** [May 19, 2017, 7:53pm UTC](https://forum.ansible.com/t/is-it-possible-to-use-a-variable-as-a-key-in-a-key-value-pair/24528/1 "2017-05-19T19:53:57Z")

</div>

I’m trying to populate a file based on some default vars and I’m wondering if the following is possible (and if so, how?) because I’ve been unable to get it to work.

given:

ansible 2.3.0.0

python version = 2.7.13 (default, May 10 2017, 20:04:28) [GCC 6.3.1 20161221 (Red Hat 6.3.1-1)]

…/defaults/main.yml

> hosts:
> 
> - {name: “{{inventory\_hostname}}”, ‘{{ansible\_ec2\_placement\_region}}’: true, “{{ansible\_ec2\_placement\_availability\_zone}}”: true, test: true, group1: true}

> host\_groups:
> 
> - name: ‘{{ansible\_ec2\_placement\_region}}’
> 
> - name: ‘{{ansible\_ec2\_placement\_availability\_zone}}’

> - name: ‘group1’
> - name: ‘test’

…/templates/groups.cfg

> {% for group in host\_groups %}
> 
> group\_name {{group.name}};
> 
> members {{hosts|selectattr(group.name, ‘defined’)|map(attribute=‘name’)|join(‘,’)}}
> 
> {% endfor %}

…/tasks/groups.yml

> - name: Generate groups  
> become: true  
> template:  
> src: groups.cfg  
> dest: “{{object\_dir}}/groups.cfg”  
> mode: 0644  
> with\_items:
> - “{{host\_groups}}”

> 

I would like for the template to be converted to:

> group\_name test;  
> members my.host.name;

> group\_name us-west-2;  
> members my.host.name;
> 
> group\_name us-west-2a  
> members my.host.name;

> group\_name group1;  
> members my.host.name;

but what I get is:
