Reputation: 419
The variables are being passed as extra_args by loading them from a YAML file. I have tried the following but it doesn't work:
- name: Check if variable are of type boolean
fail:
msg: "Variable '{{ item }}' is not a boolean"
when: item is not bool
with_items: "{{ required_boolean_vars }}"
Also, similar to boolean, how could I do the same for integer, dictionary and object type.
Upvotes: 3
Views: 3989
Reputation: 1923
The proper way to ensure a variable is not a boolean, is to use the sameas Jinja2 test like this:
when: item is not sameas true and item is not sameas false
Upvotes: 0
Reputation: 68559
There is a general type_debug
filter which returns the type, so for Boolean the condition is:
when: "item | type_debug == 'bool'"`
Another way:
when: item is sameas true or item is sameas false
For a dictionary:
when: item is mapping
For a list:
when: item is iterable
Also, the above conditional checks item
for being Boolean as you asked in the title. Add not
if you wanted to test for the opposite as your code suggests...
Upvotes: 5
Reputation: 419
A workaround I found to fail task if variables NOT of type boolean was
- name: Check if variable are of type boolean
assert:
that: "{{ item }} == false or {{ item }} == true"
msg: "Variable {{ item }} is not of type boolean"
with_items: "{{ required_boolean_vars }}"
where required_boolean_vars contains a list of variables I want to check.
Upvotes: 0