# How to map variables in the play to variables in the template file?

**URL:** <https://forum.ansible.com/t/how-to-map-variables-in-the-play-to-variables-in-the-template-file/14345>\
**Category:** Ansible Project\
**Created:** [January 15, 2014, 1:47am UTC](https://forum.ansible.com/t/how-to-map-variables-in-the-play-to-variables-in-the-template-file/14345 "2014-01-15T01:47:15Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Teerapap\_Changwichuk](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Teerapap\_Changwichuk](https://forum.ansible.com/u/Teerapap_Changwichuk)\
**Post date:** [January 15, 2014, 1:47am UTC](https://forum.ansible.com/t/how-to-map-variables-in-the-play-to-variables-in-the-template-file/14345/1 "2014-01-15T01:47:15Z")

</div>

I have a template file like this.

> {  
> id: “{{ item.id }}”,  
> url: “{{ item.url }}”,  
> }

Let say I have two variables looks like this.

var1:

- a: “id1”,
- a: “id2”,

var2:

- b:  
url: “url1”
- b:  
url: “url2”

Actually they are more complex. I think I have to use with\_together. Is there a way to map var1.a to item.id and var2.b.url to item.url?  
Because I don’t want the template file to assume the variable names/structures in the play. They might changes in different contexts.

Thank you

---

<div class="post-metadata">

**Author:** ![Michael\_DeHaan2](https://avatars.discourse-cdn.com/v4/letter/m/da6949/32.png) [@Michael\_DeHaan2](https://forum.ansible.com/u/Michael_DeHaan2)\
**Post date:** [January 15, 2014, 1:06pm UTC](https://forum.ansible.com/t/how-to-map-variables-in-the-play-to-variables-in-the-template-file/14345/2 "2014-01-15T13:06:19Z")

</div>

with\_together or with\_nested, depending on whether you want the permutations or them just zipped up together, yeah.

If you have an example of something more complex, it would be helpful to see it.

" Is there a way to map var1.a to [item.id](http://item.id/) and var2.b.url to item.url?"

Don’t understand this part of the question.

"Because I don’t want the template file to assume the variable names/structures in the play. "

If you want the logic in your template instead, it will need to use particular variables. Though I don’t think that’s a problem at all in many cases, you could of course wrap something in a role and pass the list in as input, like so:

- { role: foo, my\_list\_one: “{{ alist }}”, my\_list\_two: “{{ blist }}” }

---

<div class="post-metadata">

**Author:** ![Teerapap\_Changwichuk](https://avatars.discourse-cdn.com/v4/letter/t/ee59a6/32.png) [@Teerapap\_Changwichuk](https://forum.ansible.com/u/Teerapap_Changwichuk)\
**Post date:** [January 16, 2014, 2:00am UTC](https://forum.ansible.com/t/how-to-map-variables-in-the-play-to-variables-in-the-template-file/14345/3 "2014-01-16T02:00:09Z")

</div>

How to zip those variables in Ansible to new variable?

I want the behaviour similar to this task.

- template: src=template.j2 dest=output.conf  
with\_items:
- { id: “{{ var1[0].a }}”, url: “{{ var2[0].b.url }}” }
- { id: “{{ var1[1].a }}”, url: “{{ var2[1].b.url }}” }  
…
- { id: “{{ var1[n].a }}”, url: “{{ var2[n].b.url }}” }

I haven’t used role. I’ll take a look at it.
