You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ansible中能否在loop_control的label里调用ansible.builtin.shell的delta参数?

Ansible loop_control 的 label 配置无法直接引用 shell 模块返回的 delta 字段

核心原因

  • loop_control.label 会在每次循环的任务执行前完成渲染,而 ansible.builtin.shell 模块返回的 delta 字段是任务执行结束后才生成的返回值,二者的生成时序完全不匹配,你给出的示例写法无法生效。

替代实现方案

如果你需要展示每次循环任务的执行耗时,可以通过「注册执行结果+后续debug遍历输出」的方式实现,参考代码如下:

- hosts: localhost
  gather_facts: false
  tasks:
    - name: 执行嵌套循环shell任务
      ansible.builtin.shell: echo {{ item[0] }} {{ item[1] }} {{ item[2] }}
      with_nested:
        - ['700', '600']
        - ['./5.csv', '5.xlsx']
        - [1, 2, 3, 4]
      register: shell_exec_results
      loop_control:
        label: cmd-{{ item[0] }}-{{ item[1] }}-{{ item[2] }}

    - name: 输出带执行耗时的任务结果
      ansible.builtin.debug:
        msg: cmd-{{ item.item[0] }}-{{ item.item[1] }}-{{ item.item[2] }}-{{ item.delta }}
      loop: "{{ shell_exec_results.results }}"
      loop_control:
        label: exec-result-{{ item.item[0] }}-{{ item.item[1] }}-{{ item.item[2] }}

额外说明:Ansible 原生的任务执行日志本身就会打印每个循环项的执行耗时信息,如果只是需要排查执行耗时问题,不需要额外配置即可直接查看。

内容的提问来源于stack exchange,提问作者andrewz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.21 22:24:05