---
- hosts: localhost
tasks:
- command: 'true'
when: none_var is not none and none_var.changed
vars:
none_var: "{{ {} if foo is defined else none }}"
ansible-playbook t.yml
PLAY [localhost] *******************************************************************************************************************************************************************************************
TASK [command] *********************************************************************************************************************************************************************************************
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'none_var is not none and none_var.changed' failed. The error was: error while evaluating conditional (none_var is not none and none_var.changed): 'unicode object' has no attribute 'changed'\n\nThe error appears to be in '/tmp/t.yml': line 4, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - command: 'true'\n ^ here\n"}
PLAY RECAP *************************************************************************************************************************************************************************************************
localhost
If I change it to this, it works:
---
- hosts: localhost
tasks:
- command: 'true'
when: none_var is mapping and none_var.changed
vars:
none_var: "{{ foo if foo is defined else '' }}"
I have reproduced something similar:
---
- hosts: localhost
tasks:
- command: 'true'
when: none_var is not none and none_var.changed
vars:
none_var: "{{ {} if foo is defined else none }}"
ansible-playbook t.yml
PLAY [localhost] ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* *****
TASK [Gathering Facts] ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******
ok: [localhost]
TASK [command] ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* *******
fatal: [localhost]: FAILED! => {"msg": "The conditional check 'none_var is not none and none_var.changed' failed. The error was: error while evaluating conditional (none_var is not none and none_var.changed): 'unicode object' has no attribute 'changed'\n\nThe error appears to be in '/tmp/t.yml': line 4, column 7, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n tasks:\n - command: 'true'\n ^ here\n"}
PLAY RECAP ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ******* ****
localhost
If I change it to this, it works:
---
- hosts: localhost
tasks:
- command: 'true'
when: none_var is mapping and none_var.changed
vars:
none_var: "{{ foo if foo is defined else '' }}"