块内任务的附加条件 [英] Additional conditions for tasks inside a block
问题描述
我正在尝试将一些带有 when 条件的任务包含在块中.此块中的一些任务还有其他条件.问题是这些任务(带有附加条件)被跳过.块的条件和所有附加条件都为真.
I'm trying to enclose tasks in the block with some when condition. Also some tasks inside this block have additional conditions. The problem is such tasks (with additional conditions) are skipped. Both block's condition and all additional conditions are true.
下面是一个示例播放:
- block:
- set_fact:
packages_to_install: "{{ packages_to_install }} + [ '{{ (distrs.stdout | from_json).postgresql }}' ]"
- set_fact:
packages_to_install: "{{ packages_to_install }} + [ '{{ (distrs.stdout | from_json).webserver }}' ]"
when:
- server.webserver is defined
- server.webserver == true
when:
- server is defined
所以,postgresql 被添加到包列表中,但 webserver 没有.
So, postgresql is added to the list of packages, but webserver is not.
根据文档,块内的所有任务将在附加块中的 when 条件并在任务的上下文中对其进行评估后执行.也许在 Ansible 2.4 中不可能有附加条件的封闭任务?
According to the docs, all tasks inside the block will be executed after appending the when condition from the block and evaluating it in the task’s context. Maybe it's not just possible in Ansible 2.4 to have enclosed tasks with additional conditions?
推荐答案
你应该修正 when
声明的缩进.
You should fix the indentation of the when
declarations.
也许在 Ansible 2.4 中不可能有包含附加条件的封闭任务?
Maybe it's not just possible in Ansible 2.4 to have enclosed tasks with additional conditions?
Ansible 2.4 运行正常:
Ansible 2.4 works ok:
tasks:
- block:
- debug:
msg: "task 1"
- debug:
msg: "task 2"
when: false
- debug:
msg: "task 3"
when: true
when: true
结果:
TASK [debug] **************************************************************************************************
ok: [localhost] => {
"msg": "task 1"
}
TASK [debug] **************************************************************************************************
skipping: [localhost]
TASK [debug] **************************************************************************************************
ok: [localhost] => {
"msg": "task 3"
}
<小时>
为了清晰起见,您始终可以重新排列 block
任务中的键:
tasks:
- when: true
block:
- debug:
这篇关于块内任务的附加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!