Jinja2

From Federal Burro of Information
Jump to navigationJump to search

COmmnad line testing

template files /home/user/project/files/common/test.j2

{{ something }}
The test worked

the python script:

from jinja2 import Environment, FileSystemLoader
env = Environment(loader=FileSystemLoader('/home/user/project/files/common'))
tmpl = env.get_template('test.j2')
something = 'some randome string'
print tmpl.render(something=something)

test run:

[user@server common]$ python py_check
some randome string
The test worked
[user@server common]$

Conditionals

{% if '-esb-' in ansible_hostname %}
# hostname is {{ ansible_hostname }}
{% else %}
# {{ ansible_hostname }} did not match
{% endif %}