I’m testing existing roles and playbooks against Ansible 2.2, and I’ve noticed that several roles that I have written that take advantage of “when: variable is defined” or “when: variable is undefined” in tasks no longer work. This happens when a variable is used from a previous task, but is empty because maybe the results were empty. Here is an example:
- name: Find existing rules
find: path=/etc/software/rules/
register: existing_rules- name: Clean rules
file: path={{ item }} state=absent
with_items: “{{ existing_rules.files | map(attribute=‘path’) | list }}”
when: existing_rules is defined
In this case, there were no existing files, so the variable is empty and I would expect this Clean rules task to be skipped, but instead Ansible errors with something like
FAILED! => {“failed”: true, “msg”: “‘dict object’ has no attribute ‘files’”}
What are the options then to allow for this sort of conditional logic? This definitely worked in Ansible 2.1, but I don’t recall if any DEPRECATED warnings were given.