In Jinja2, how do you test if a variable is undefined?
From the Jinja2 template designer documentation: {% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %}
From the Jinja2 template designer documentation: {% if variable is defined %} value of variable: {{ variable }} {% else %} variable is not defined {% endif %}
Use the none test (not to be confused with Python’s None object!): {% if p is not none %} {{ p.User[‘first_name’] }} {% else %} NONE {% endif %} or: {{ p.User[‘first_name’] if p is not none else ‘NONE’ }} or if you need an empty string: {{ p.User[‘first_name’] if p is not none }}
Just pass items to template.render as a keyword argument – it should be a list (really any iterable will do) of items. If you need sub-items use a class or a dictionary. In the simplest case, you can use a dictionary: items = [] for i in range(1, 11): i = str(i) # dict == … Read more
To filter a list of dicts you can use the selectattr filter together with the equalto test: network.addresses.private_man | selectattr(“type”, “equalto”, “fixed”) The above requires Jinja2 v2.8 or later (regardless of Ansible version). Ansible also has the tests match and search, which take regular expressions: match will require a complete match in the string, while … Read more