Skip to content

Use namespace as global variable in Ansible Jinja templates

A simple task, or so I thought: in a Jinja template keep track of the number of items in a loop. And then use that count afterwards.

Disclaimer: the number of items is not equal the number of times the loop runs, so I can't use the loop variables.

Turns out that Jinja has other opinions, and variables inside a loop are all local. When a variable is changed inside the loop, the scoop of the variable stays local, and once the loop ends the original value of the variable is restored. That's even true for variables which are created outside the loop.

 

Continue reading "Use namespace as global variable in Ansible Jinja templates"