首页 > 解决方案 > 布尔值 true 或 false 的 Ansible 导入任务

问题描述

我想用布尔值创建一个部署任务/角色。默认情况下为 false 并使其为 true,部署完成。就像在 main.yml 中定义了一个变量

vars:
  - c

- name: task 1
  import_tasks: "a.yml"
  when: c is defined
  tags:
    - a

- name: task 2
  import_tasks: "b.yml"
  when: c is defined
  tags:
    - b

标签: ansible

解决方案


您可以执行以下操作;但是,正如以下评论之一所述,这可能会在某些情况下导致一些意外行为,并且不是实现您所要求的唯一方法:

- set_fact: c=false
  when: c is undefined

- name: task 1
  import_tasks: "a.yml"
  when: c | bool
  tags:
    - a

- name: task 2
  import_tasks: "b.yml"
  when: c | bool
  tags:
    - b

推荐阅读