是否阻止了它们内部的 ansible 支持条件
Does block in ansible support conditionals inside them
我想知道ansible是否支持以下场景:
- block 有 when 条件。
- 块内的任务再次有进一步的条件 (when)。
下面是我目前的main.yml
来说明:
- block:
- name: task 1
shell: check_install
when: ansible_distribution == 'CentOS'
- name: task 2
shell: echo "test task"
when: out.rc > "0" #assuming **out** is registered above
become: true
when: sw_install|default(False) == True
根据 ansible 文档,
Most of what you can apply to a single task (with the exception of
loops) can be applied at the block level, which also makes it much
easier to set data or directives common to the tasks. This does not
mean the directive affects the block itself, but is inherited by the
tasks enclosed by a block. i.e. a when will be applied to the tasks,
not the block itself.
所以基本问题是:如果我 运行 一个带有上述任务的剧本,条件流是如何解释的?它 运行 像 and
条件吗?
类似于:
run task 1 when: block conditional & task one conditional satisfies
run task 2 when: block conditional & task two conditional satisfies
向块添加条件与向块内的每个任务添加相同条件完全相同。
如果您有一个块级别的条件和另一个任务级别的条件,它们都将在任务 运行 时间进行评估。
这也意味着,如果您的任务之一在 运行 期间更改了阻止条件,则每次都会重新评估。举个例子:
- name: Set a block condition true
set_fact:
block_condition: true
- name: Set a task condition true
set_fact:
task_condition: true
- block:
- name: Write a message
debug:
msg: Block and task conditions are true
when: task_condition | bool
- name: Change condition
set_fact:
block_condition: false
- name: Write a message
debug:
msg: Block condition is true
when: block_condition | bool
在这种情况下,块的最后一个任务将被跳过,因为 block_condition
将在 运行 时评估为 false
。
我想知道ansible是否支持以下场景:
- block 有 when 条件。
- 块内的任务再次有进一步的条件 (when)。
下面是我目前的main.yml
来说明:
- block:
- name: task 1
shell: check_install
when: ansible_distribution == 'CentOS'
- name: task 2
shell: echo "test task"
when: out.rc > "0" #assuming **out** is registered above
become: true
when: sw_install|default(False) == True
根据 ansible 文档,
Most of what you can apply to a single task (with the exception of loops) can be applied at the block level, which also makes it much easier to set data or directives common to the tasks. This does not mean the directive affects the block itself, but is inherited by the tasks enclosed by a block. i.e. a when will be applied to the tasks, not the block itself.
所以基本问题是:如果我 运行 一个带有上述任务的剧本,条件流是如何解释的?它 运行 像 and
条件吗?
类似于:
run task 1 when: block conditional & task one conditional satisfies
run task 2 when: block conditional & task two conditional satisfies
向块添加条件与向块内的每个任务添加相同条件完全相同。
如果您有一个块级别的条件和另一个任务级别的条件,它们都将在任务 运行 时间进行评估。
这也意味着,如果您的任务之一在 运行 期间更改了阻止条件,则每次都会重新评估。举个例子:
- name: Set a block condition true
set_fact:
block_condition: true
- name: Set a task condition true
set_fact:
task_condition: true
- block:
- name: Write a message
debug:
msg: Block and task conditions are true
when: task_condition | bool
- name: Change condition
set_fact:
block_condition: false
- name: Write a message
debug:
msg: Block condition is true
when: block_condition | bool
在这种情况下,块的最后一个任务将被跳过,因为 block_condition
将在 运行 时评估为 false
。