Bootstrap FreeKB - Ansible - Resolve "DEPRECATION WARNING bare variable"
Ansible - Resolve "DEPRECATION WARNING bare variable"

Updated:   |  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 Buy Me A Coffee



Comments


Add a Comment


Please enter 90c90e in the box below so that we can be sure you are a human.