Вы можете взглянуть на встроенные тесты jinja2 . С такими доступными единственная трудность состоит в том, что нет специального теста для поиска списков. Таким образом, вам нужно проверить, являются ли вары последовательностями и исключить строки и словари.
Вот пример playbook:
--
- hosts: localhost
gather_facts: false
vars:
test_vars:
- "I'm a string"
- ['I', 'am', 'a', 'list']
- {this: is, a: dict}
- 10 # This is an integer
- 2.34 # and here we have a float
tasks:
- name: Check if var is a string
debug:
msg: this is a string
when: item is string
loop: "{{ test_vars }}"
- name: Check if var is a list
debug:
msg: this is a list
when:
- item is sequence
- item is not string
- item is not mapping
loop: "{{ test_vars }}"
- name: Check if var is a dict
debug:
msg: this is a dict
when: item is mapping
loop: "{{ test_vars }}"
, который дает:
PLAY [localhost] ***********************************************************************************************************************************************************************************************************************
TASK [Check if var is a string] ********************************************************************************************************************************************************************************************************
ok: [localhost] => (item=I'm a string) => {
"msg": "this is a string"
}
skipping: [localhost] => (item=['I', 'am', 'a', 'list'])
skipping: [localhost] => (item={'this': 'is', 'a': 'dict'})
skipping: [localhost] => (item=10)
skipping: [localhost] => (item=2.34)
TASK [Check if var is a list] **********************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=I'm a string)
ok: [localhost] => (item=['I', 'am', 'a', 'list']) => {
"msg": "this is a list"
}
skipping: [localhost] => (item={'this': 'is', 'a': 'dict'})
skipping: [localhost] => (item=10)
skipping: [localhost] => (item=2.34)
TASK [Check if var is a dict] **********************************************************************************************************************************************************************************************************
skipping: [localhost] => (item=I'm a string)
skipping: [localhost] => (item=['I', 'am', 'a', 'list'])
ok: [localhost] => (item={'this': 'is', 'a': 'dict'}) => {
"msg": "this is a dict"
}
skipping: [localhost] => (item=10)
skipping: [localhost] => (item=2.34)
PLAY RECAP *****************************************************************************************************************************************************************************************************************************
localhost : ok=3 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0