Question:
Sometimes I need to test some jinja2 templates that I use in my ansible roles. What is the simplest way for doing this?
For example, I have a template (test.j2):
1 2 3 4 5 |
{% if users is defined and users %} {% for user in users %}{{ user }} {% endfor %} {% endif %} |
and vars (in group_vars/all):
1 2 3 4 5 6 7 |
--- users: - Mike - Smith - Klara - Alex |
Answer:
At this time exists 4 different variants:
1_Online (using https://cryptic-cliffs-32040.herokuapp.com/)
Based on jinja2-live-parser code.
2_Interactive (using python and library jinja2, PyYaml)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import yaml from jinja2 import Template >>> template = Template(""" ... {% if users is defined and users %} ... {% for user in users %}{{ user }} ... {% endfor %} ... {% endif %} ... """) >>> values = yaml.load(""" ... --- ... users: ... - Mike ... - Smith ... - Klara ... - Alex ... """) >>> print "{}".format(template.render(values)) Mike Smith Klara Alex |
3_Ansible (using –check)
Create test playbook jinja2test.yml:
1 2 3 4 5 6 |
--- - hosts: 127.0.0.1 tasks: - name: Test jinja2template template: src=test.j2 dest=test.conf |
and run it:
1 2 |
ansible-playbook jinja2test.yml --check --diff --connection=local |
sample output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
PLAY [127.0.0.1] ************************************************************** GATHERING FACTS *************************************************************** ok: [127.0.0.1] TASK: [Test jinja2template] *************************************************** --- before: test.conf +++ after: /Users/user/ansible/test.j2 @@ -0,0 +1,4 @@ +Mike +Smith +Klara +Alex changed: [127.0.0.1] PLAY RECAP ******************************************************************** 127.0.0.1 : ok=2 changed=1 unreachable=0 failed=0 |
4_Ansible (using -m template)
Make a file called test.txt.j2
1 2 3 4 5 6 |
{% if users is defined and users %} {% for user in users %} {{ user }} {% endfor %} {% endif %} |
Call ansible like so:
1 2 |
ansible all -i "localhost," -c local -m template -a "src=test.txt.j2 dest=./test.txt" --extra-vars='{"users": ["Mike", "Smith", "Klara", "Alex"]}' |
It will output a file called test.txt
in the current directory, which will contain the output of the evaluated test.txt.j2
template.
I understand this doesn’t directly use a vars file, but I think it’s the simplest way to test a template without using any external dependencies. Also, I believe there are some differences between what the jinja2 library provides and what ansible provides, so using ansible directly circumvents any discrepancies. When the JSON that is fed to --extra-vars
satisfies your needs, you can convert it to YAML and be on your way.