Ansible – Test variables types
Here is a playbook for understanding the Ansible test with variables types
---
- hosts: localhost
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:
- name: "dict is mapping"
debug: msg="dict is mapping"
when: dict is mapping
- name: "list is mapping"
debug: msg="list is mapping"
when: list is mapping
- name: "string is mapping"
debug: msg="string is mapping"
when: string is mapping
- name: "dict is sequence"
debug: msg="dict is sequence"
when: dict is sequence
- name: "list is sequence"
debug: msg="list is sequence"
when: list is sequence
- name: "string is sequence"
debug: msg="string is sequence"
when: string is sequence
- name: "dict is iterable"
debug: msg="dict is iterable"
when: dict is iterable
- name: "list is iterable"
debug: msg="list is iterable"
when: list is iterable
- name: "string is iterable"
debug: msg="string is iterable"
when: string is iterable
- name: "dict is string"
debug: msg="dict is string"
when: dict is string
- name: "list is string"
debug: msg="list is string"
when: list is string
- name: "string is string"
debug: msg="string is string"
when: string is string
- copy:
dest: ./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 }}"
{% endfor -%}
{% elif v is sequence -%}
{% for n in v -%}
"- {{ n }}"
{% endfor -%}
{% else -%}
"what?"
{% endif -%}
{% endfor -%}
This will produce this output
PLAY [localhost] ***************************************************************************************************************************************************************************************
TASK [dict is mapping] *********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "dict is mapping"
}
TASK [list is mapping] *********************************************************************************************************************************************************************************
skipping: [localhost]
TASK [string is mapping] *******************************************************************************************************************************************************************************
skipping: [localhost]
TASK [dict is sequence] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "dict is sequence"
}
TASK [list is sequence] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "list is sequence"
}
TASK [string is sequence] ******************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "string is sequence"
}
TASK [dict is iterable] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "dict is iterable"
}
TASK [list is iterable] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "list is iterable"
}
TASK [string is iterable] ******************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "string is iterable"
}
TASK [dict is string] **********************************************************************************************************************************************************************************
skipping: [localhost]
TASK [list is string] **********************************************************************************************************************************************************************************
skipping: [localhost]
TASK [string is string] ********************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": "string is string"
}
TASK [copy] ********************************************************************************************************************************************************************************************
ok: [localhost]
PLAY RECAP *********************************************************************************************************************************************************************************************
localhost : ok=9 changed=0 unreachable=0 failed=0
And this file
"i1: v1" "i2: v2" "- item1" "- item2" "string: string"
Please follow and like us: