It can be sometime useful to generate dynamically some variables that are arrays.
The Jinja2
templating features allows to achieve this directly when defined a variable.
Here is an example:
cgit_stack_git_repos: >-
{% set result = [] -%}
{% for h in cgit_stack_repos -%}
{% if h.src is not defined -%}
{% set dummy = result.append({'name': h.name, 'desc': h.desc, 'parent_dir': cgit_stack_repos_dir + '/' + h.parent_dir}) -%}
{% endif -%}
{% endfor -%}
{{ result }}
The important part are:
[]
append
function and its return in an unused variableYou should avoid new lines as it won’t be interpreted as a variable.
This tip could be deprecated in new version of Ansible.