简体   繁体   English

如何在剧本中设置Ansible标签?

[英]How to set an Ansible tag from within a playbook?

I would like to skip some of the plays based on a conditional value. 我想基于条件值跳过一些戏剧。 I could have a task at the beginning that would skip some of the other tasks when a condition is met. 我可以在开始时执行一项任务,在满足条件时跳过其他一些任务。

I know I could always use set_fact and use this as a condition to run the other plays (roles and tasks) or evaluate the said condition directly in each play I want to skip. 我知道我总是可以使用set_fact并将其作为运行其他游戏(角色和任务)的条件,或者在我想跳过的每个游戏中直接评估所述条件。

However, since the plays already have a tagging system in place, is there something like set_tags in Ansible that I could leverage on to avoid having to add conditions all over my plays and bloat my already heavy playbook. 但是,由于游戏已经有了标记系统,在set_tags中有类似set_tags的东西,我可以利用它来避免在我的游戏中添加条件并膨胀我已经很重的剧本。

Something such as: 像这样的东西:

- name: skip terraform tasks if no file is provided
  hosts: localhost
  tasks:
    set_tags:
      - name: terraform
        state: skip
    when: terraform_files == ""

The two plays I'd like to skip: 我想跳过的两部戏剧:

- name: bootstrap a testing infrastructure
  hosts: localhost
  roles:
    - role: terraform
      state: present
  tags:
    - create_servers
    - terraform

[...]

- name: flush the testing infrastructure
  hosts: localhost
  roles:
    - role: terraform
      state: absent
  tags:
    - destroy_servers
    - terraform

If I understood correctly, the following should do. 如果我理解正确,应该做以下事情。

- name: bootstrap a testing infrastructure
  hosts: localhost
  roles:
    - role: terraform
      state: present
      when: terraform_files != ""
  tags:
    - create_servers
    - terraform

[...]

- name: flush the testing infrastructure
  hosts: localhost
  roles:
    - role: terraform
      state: absent
      when: terraform_files != ""
  tags:
    - destroy_servers
    - terraform

This is basically the same as setting the when clause on every single tasks in the role. 这与在角色中的每个任务上设置when子句基本相同。 So each task will be inspected but skipped if the condition is false. 因此,如果条件为假,将检查每个任务但跳过。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM