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

Ansible when条件为false时未跳过任务 执行加载不存在文件报错

问题根因

第二个任务的when条件逻辑运算符使用错误,你当前使用的是or(或逻辑),只要任意一个判断项为真,整个条件就会成立。
当传入market_input = "target1"时,两个判断项的结果如下:

  • "target1" not in market_input:结果为false
  • "target2" not in market_input:结果为true
    最终false or true的计算结果为true,所以第二个任务会被触发执行,和你预期的"非target1/非target2才执行"的逻辑不符。
排查方法

你可以在两个include_vars任务前新增一个调试任务,打印条件的实际计算结果,确认逻辑是否符合预期:

- name: 打印条件判断结果
  debug:
    msg:
      - "第一个任务条件结果:{{ 'target1' in market_input or 'target2' in market_input }}"
      - "第二个任务当前条件结果:{{ 'target1' not in market_input or 'target2' not in market_input }}"
修复方案

将第二个任务when条件中的or改为and(与逻辑),只有同时满足"既不包含target1也不包含target2"时,才会执行第二个任务,修改后的完整任务如下:

- name: include tcrj01 variables from prefix_lists
  include_vars:
    file: "../collection-data/output/{{ market_input | lower }}tcrj01_prefix_lists.yml"
    name: prefix01
  delegate_to: localhost
  when: '"target1" in market_input or "target2" in market_input'

- name: include dsrj01 variables from prefix_lists
  include_vars:
    file: "../collection-data/output/{{ market_input | lower }}dsrj01_prefix_lists.yml"
    name: prefix01
  delegate_to: localhost
  when: '"target1" not in market_input and "target2" not in market_input'

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.09.23 21:06:05