Skip to content

Instantly share code, notes, and snippets.

@jeanmonet
Forked from spiette/jinja_variables_types.yml
Created November 25, 2020 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jeanmonet/e4f51f37c4603dc663dd8fd075e3a431 to your computer and use it in GitHub Desktop.
Save jeanmonet/e4f51f37c4603dc663dd8fd075e3a431 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 %}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment