I have following play book:
test2.yml:
- hosts: localhost
connection: local
gather_facts: false
vars:
dict1:
v1:
- 111
- 222
- 333
v2:
ver: "{{ ver }}"
tasks:
- name: Gather list
set_fact: rblist="{{ pitem }}"
with_dict: "{{ dict1 }}"
when: "pitem.key in ver"
loop_control:
loop_var: pitem
register: plist
- name: lets include the task if the value is not empty
include: test3.yml
when: rblist.value
test3.yml:
---
- name: display value if not empty
debug: msg={{ rblist.value }}
I'd like to invoke test3.yml only when rblist.value is not empty, but its ignoring that and invokes the task even when the value is empty.
When the value is not empty: it works fine
ansible-playbook test2.yml -e "ver=v1"
PLAY [localhost] ************************************************************************************************************************************************************************************************************************
TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v1', u'value': [111, 222, 333]})
TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
ok: [localhost] => {
"msg": [
111,
222,
333
]
}
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost : ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
When the value is empty: seems to behave same as when the list
ansible-playbook test2.yml -e "ver=v2"
PLAY [localhost] ************************************************************************************************************************************************************************************************************************
TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v2', u'value': None})
TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
Following How to test that a registered variable is not empty? and other solutions, I have tried the following
when: rblist.value | length > 0
I get this:
TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'rblist.value | length > 0' failed. The error was: Unexpected templating type error occurred on ({% if rblist.value | length > 0 %} True {% else %} False {% endif %}): object of type 'NoneType' has no len()\n\nThe error appears to be in '/stage/ap/ansible/test/test3.yml': line 2, column 5, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n---\n - name: display value if not empty\n ^ here\n"}
When I try this:
when: rblist.value != None
I get this:
ansible-playbook test2.yml -e "ver=v2"
PLAY [localhost] ************************************************************************************************************************************************************************************************************************
TASK [Gather list] **********************************************************************************************************************************************************************************************************************
ok: [localhost] => (item={u'key': u'v2', u'value': None})
TASK [display value if not empty] *******************************************************************************************************************************************************************************************************
PLAY RECAP ******************************************************************************************************************************************************************************************************************************
localhost : ok=1 changed=0 unreachable=0 failed=0 skipped=1 rescued=0 ignored=0
I Tried this:
when: rblist.value != ""
Same results as above that is when used != None