-
-
Save kenwoodjw/8a4f4d70d176cfbed2170c8135b18339 to your computer and use it in GitHub Desktop.
Test the variables types in jinja2 templates, used here with ansible
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
- hosts: all | |
gather_facts: no | |
vars: | |
string: "string" | |
list: | |
- item1 | |
- item2 | |
dict: | |
key1: value1 | |
key2: value2 | |
object: | |
k1: "string" | |
k2: [ "item1", "item2" ] | |
k3: { 'i1': 'v1', 'i2': 'v2' } | |
tasks: | |
- debug: msg="dict is mapping" | |
when: dict is mapping | |
- debug: msg="list is mapping" | |
when: list is mapping | |
- debug: msg="string is mapping" | |
when: string is mapping | |
- debug: msg="dict is sequence" | |
when: dict is sequence | |
- debug: msg="list is sequence" | |
when: list is sequence | |
- debug: msg="string is sequence" | |
when: string is sequence | |
- debug: msg="dict is iterable" | |
when: dict is iterable | |
- debug: msg="list is iterable" | |
when: list is iterable | |
- debug: msg="string is iterable" | |
when: string is iterable | |
- debug: msg="dict is string" | |
when: dict is string | |
- debug: msg="list is string" | |
when: list is string | |
- debug: msg="string is string" | |
when: string is string | |
- copy: | |
dest: /tmp/variable.txt | |
content: | | |
{% for k,v in object.iteritems() %} | |
{% if v is string %}string: {{ v }} | |
{% elif v is mapping %}{% for j,l in v.iteritems() %}{{ j }}: {{ l }}\n{% endfor %} | |
{% elif v is sequence %}{% for n in v %}- {{ n }}\n{% endfor %} | |
{% else %}huh? | |
{% endif %} | |
{% endfor %} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment