AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 1130612
Accepted
alexus
alexus
Asked: 2023-05-08 10:13:22 +0800 CST2023-05-08 10:13:22 +0800 CST 2023-05-08 10:13:22 +0800 CST

您应该将任务的 `loop_control` 选项中的 `loop_var` 值设置为其他值以避免变量

  • 772

我的环境:

# 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

阅读一些之后:

  • 使用 loop_var 定义内部和外部变量名称
  • 将控件添加到循环

我将任务修改为如下内容:

- 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"}

请指教。

ansible
  • 1 1 个回答
  • 36 Views

1 个回答

  • Voted
  1. Best Answer
    Vladimir Botka
    2023-05-08T12:01:47+08:002023-05-08T12:01:47+08:00

    目前还不清楚为什么您需要注册变量item 的(未记录的)外循环。例如,给定树

    shell> tree /tmp/test
    /tmp/test
    ├── file1
    ├── file2
    └── file3
    

    下面的戏

    shell> cat pb.yml
    - hosts: localhost
    
      tasks:
    
        - find:
            paths: /tmp/test
          register: find
    
        - file:
            path: "{{ item.path }}"
            state: absent
          loop: "{{ find.files }}"
          loop_control:
            label: "{{ item.path }}"
    

    在--check --diff模式下运行

    shell> ansible-playbook -CD pb.yml
    
    PLAY [localhost] ******************************************************************************
    
    TASK [find] ***********************************************************************************
    ok: [localhost]
    
    TASK [file] ***********************************************************************************
    --- before
    +++ after
    @@ -1,2 +1,2 @@
     path: /tmp/test/file3
    -state: file
    +state: absent
    
    changed: [localhost] => (item=/tmp/test/file3)
    --- before
    +++ after
    @@ -1,2 +1,2 @@
     path: /tmp/test/file2
    -state: file
    +state: absent
    
    changed: [localhost] => (item=/tmp/test/file2)
    --- before
    +++ after
    @@ -1,2 +1,2 @@
     path: /tmp/test/file1
    -state: file
    +state: absent
    
    changed: [localhost] => (item=/tmp/test/file1)
    
    PLAY RECAP ************************************************************************************
    localhost: ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0
    

    笔记:

    • 如果你定义loop_var使用它。下面的任务给出了相同的结果
    - file:
        path: "{{ my_loop_var.path }}"
        state: absent
      loop: "{{ find.files }}"
      loop_control:
        label: "{{ my_loop_var.path }}"
        loop_var: my_loop_var
    
    • 下面的条件是多余的。去掉它。当找不到任何东西时 ( find.matched: 0 ) 文件列表为空 ( fined.files: [] ) 并且无论如何都会跳过循环
      when: find.matched is defined and find.matched != 0
    
    • 如果find是模块find的注册变量,则没有什么可以展平的。过滤器压平可以移除
      loop: "{{ find.files }}"
    
    • 0

相关问题

  • Ansible:将字符串转换为字典

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve