我的环境:
# 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
#
以下任务是我的 Ansible 角色的一部分:
- 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
然而,我注意到一条警告信息:
[警告]:任务:XYZ:_file - 状态:不存在:循环变量“item”已在使用中。您应该将任务选项
loop_var
中的值设置为其他值,以避免变量冲突和意外行为。loop_control
阅读一些之后:
我将任务修改为如下内容:
- 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
这个任务在我的角色中被调用了两次,第一次工作正常,第二次失败
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"}
请指教。
目前还不清楚为什么您需要注册变量item 的(未记录的)外循环。例如,给定树
下面的戏
在--check --diff模式下运行
笔记: