Ansible - Resolve "DEPRECATION WARNING bare variable"
by
Jeremy Canfield |
Updated: June 27 2023
| Ansible articles
Let's say something like this is being returned.
[DEPRECATION WARNING]: evaluating u'item' as a bare variable, this behaviour will go away and you might need to add |bool to the expression in the future. Also see CONDITIONAL_BARE_VARS configuration toggle. This feature will be
removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg
For example, I once got this when attempting to determine if a file exists, is a file, and is readable.
---
- name: main play
hosts: all
tasks:
- name: use 'stat' to determine if file exists
stat:
path: /path/to/example.txt
register: out
- name: fail if file does not exist, is a directory, or is not readable
fail:
msg: "{{ myfile }} does NOT exist, is a directory, or is not readable"
when: item
with_items:
- out.stat.exists == false
- out.stat.isdir == true
- out.stat.readable == false
...
I simply just had to add the bool filter to the when statement in the fail task.
---
- name: main play
hosts: all
tasks:
- name: use 'stat' to determine if file exists
stat:
path: /path/to/example.txt
register: out
- name: fail if file does not exist, is a directory, or is not readable
fail:
msg: "{{ myfile }} does NOT exist, is a directory, or is not readable"
when: item|bool
with_items:
- out.stat.exists == false
- out.stat.isdir == true
- out.stat.readable == false
...
Did you find this article helpful?
If so, consider buying me a coffee over at