gpt4 book ai didi

loops - 带有循环的 Ansible include_tasks 无法检查条件

转载 作者:行者123 更新时间:2023-12-05 05:03:11 26 4
gpt4 key购买 nike

我想根据任务中设置的条件来包含任务。

tasks:
- name: SET INIT STATUS
set_facts:
passed: true

- name: INCLUDE TASKS WITH LOOP
include_tasks: update_status.yml
loop: [1, 2, 3]
when: passed

更新状态.yml:

- name: OUTPUT THE STATUS
debug: msg="ITEM: {{item}} has status {{passed}}"
- name: UPDATE the Status
set_fact:
passed: false

我预计它只会为项目 1 运行一次,因为在它运行后,它会将状态更改为 false,然后无法通过条件检查。但它实际上运行了 3 次,所以看起来它不会在循环期间再次检查状态。

这是输出

TASK: [SET INIT STATUS]
ok: [localhost]

TASK: [INCLUDE TASKS WITH LOOP]
included: simple.yml for localhost
included: simple.yml for localhost
included: simple.yml for localhost

TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 1 has status True
}

TASK: [UPDATE the Status]
ok: [localhost]

TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 2 has status False
}

TASK: [UPDATE the Status]
ok: [localhost]

TASK: [OUTPUT THE STATUS]
ok: [localhost] => {
"msg": ITEM 3 has status False
}

TASK: [UPDATE the Status]
ok: [localhost]

最佳答案

在尝试了不同的方法之后,我能够通过将循环放在一个 block 中来实现我想要的。

- name: LOOP WITH INCLUDE_TASK INSIDE BLOCK
block:
- name: LOOP WITH INCLUDE_TASK
include_tasks: simple.yml
loop: [1, 2, 3]
when: passed

这是结果

TASK [SET passed] ******************************************************************************************
ok: [localhost]

TASK [LOOP WITH INCLUDE_TASK] ******************************************************************************************
included: /home/richard/samples/simple.yml for localhost
included: /home/richard/samples/simple.yml for localhost
included: /home/richard/samples/simple.yml for localhost

TASK [SHOW THE STATUS] ******************************************************************************************
ok: [localhost] => {
"msg": "Item 1 has status True"
}

TASK [CHANGE THE STATUS] ******************************************************************************************
ok: [localhost]

TASK [SHOW THE STATUS] ******************************************************************************************
skipping: [localhost]

TASK [CHANGE THE STATUS] ******************************************************************************************
skipping: [localhost]

TASK [SHOW THE STATUS] ******************************************************************************************
skipping: [localhost]

TASK [CHANGE THE STATUS] ******************************************************************************************
skipping: [localhost]

关于loops - 带有循环的 Ansible include_tasks 无法检查条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61846925/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com