'check variable value from host_vars null or not
I have defined a variable in host_vars/hostname.yml with no value assigned to it. I have the below task defined to check if the variable is empty. My play should fail.
host_vars/hostname.yml
tls_password:
- name: Check for null
fail: msg="Error value is null"
when: tls_password == ""
When I run this task it is getting skipped. Any idea how to fix this? Thanks in advance.
Solution 1:[1]
The variable tls_password is not an empty string. It is YAML None equivalent of Python null
- debug:
var: tls_password|type_debug
gives
tls_password|type_debug: NoneType
Therefore the comparison
- debug:
msg: '{{ tls_password == "" }}'
will evaluate to false and your task will be skipped
msg: false
The solution is simple. Test the None value
- name: Check for null
fail:
msg: Error value is null
when: tls_password == None
gives what you want
fatal: [localhost]: FAILED! => changed=false
msg: Error value is null
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | Vladimir Botka |
