块内任务的附加条件 [英] Additional conditions for tasks inside a block

查看:82
本文介绍了块内任务的附加条件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图用一些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声明的缩进.

也许在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任务中对键进行重新排序:


And you can always reorder the keys in block task for clarity:

tasks:
  - when: true
    block:
      - debug:

这篇关于块内任务的附加条件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆