My environment:
# cat /etc/debian_version
11.7
# ansible-playbook --version
ansible-playbook [core 2.13.1]
config file = None
configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.9/dist-packages/ansible
ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections
executable location = /usr/local/bin/ansible-playbook
python version = 3.9.2 (default, Feb 28 2021, 17:03:44) [GCC 10.2.1 20210110]
jinja version = 3.1.2
libyaml = True
#
The following task is part of my Ansible role:
- name: _file - state:absent
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ find.files | flatten }}"
register: file
when: find.matched is defined and find.matched != 0
yet, I've noticed a warning message:
[WARNING]: TASK: XYZ : _file - state:absent: The loop variable 'item' is already in use. You should set the loop_var
value in the loop_control
option for the task to something else to avoid variable
collisions and unexpected behavior.
after reading some:
I modified my task to something like this:
- name: _file - state:absent
ansible.builtin.file:
path: "{{ item.path }}"
state: absent
loop: "{{ find.files | flatten }}"
loop_control:
label: "{{ item.path }}"
loop_var: find
register: file
when: find.matched is defined and find.matched != 0
this task is being called twice within my role, while first time it works fine, second time it fails
TASK [XYZ : _file - state:absent] ******************************************************************************************************************************************************************************
[WARNING]: TASK: XYZ : _file - state:absent: The loop variable 'find' is already in use. You should set the `loop_var` value in the `loop_control` option for the task to something else to avoid variable
collisions and unexpected behavior.
failed: [127.0.0.1] (item=None) => {"ansible_loop_var": "find", "changed": false, "find": {"atime": 1683511713.8529496, "checksum": "ecd34202c34bf761e4c2c9800a39e18dffad5d9e", "ctime": 1683511714.972948, "dev": 2049, "gid": 0, "gr_name": "root", "inode": 150677, "isblk": false, "ischr": false, "isdir": false, "isfifo": false, "isgid": false, "islnk": false, "isreg": true, "issock": false, "isuid": false, "mode": "0644", "mtime": 1683511714.972948, "nlink": 1, "path": "tmp/FILE.CSV", "pw_name": "root", "rgrp": true, "roth": true, "rusr": true, "size": 642, "uid": 0, "wgrp": false, "woth": false, "wusr": true, "xgrp": false, "xoth": false, "xusr": false}, "msg": "Failed to template loop_control.label: 'item' is undefined", "skip_reason": "Conditional result was False"}
Please advise.