Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@spiette
Created March 3, 2016 14:52
Show Gist options
  • Star 21 You must be signed in to star a gist
  • Fork 12 You must be signed in to fork a gist
  • Save spiette/2eaa77f84369dc640be1 to your computer and use it in GitHub Desktop.
Save spiette/2eaa77f84369dc640be1 to your computer and use it in GitHub Desktop.
Test the variables types in jinja2 templates, used here with ansible
---
- 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 %}
@logabot
Copy link

logabot commented Mar 18, 2022

For python3.

    - copy:
        dest: /tmp/variable.txt
        content: |
          {% for k,v in object.items() %}
          {% if v is string %}string: {{ v }}
          {% elif v is mapping %}{% for j,l in v.items() %}{{ j }}: {{ l }}\n{% endfor %}
          {% elif v is sequence %}{% for n in v %}- {{ n }}\n{% endfor %}
          {% else %}huh?
          {% endif %}
          {% endfor %}

ansible-doc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment