I have a template file like this.
{
id: “{{ item.id }}”,
url: “{{ item.url }}”,
}
Let say I have two variables looks like this.
var1:
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
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 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 }}” }
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.