【问题标题】:How to set an Ansible tag from within a playbook?如何从剧本中设置 Ansible 标签?
【发布时间】:2019-04-20 07:25:57
【问题描述】:

我想跳过一些基于条件值的播放。 我可以在开始时有一个任务,当满足条件时会跳过其他一些任务。

我知道我总是可以使用set_fact 并将其用作运行其他游戏(角色和任务)的条件,或者在我想跳过的每个游戏中直接评估所述条件。

但是,由于剧本已经有了一个标记系统,我可以利用 Ansible 中的 set_tags 之类的东西来避免在我的剧本中添加条件并使我已经很重的剧本膨胀。

诸如:

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

我想跳过的两部戏:

- 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

【问题讨论】:

    标签: ansible


    【解决方案1】:

    如果我理解正确,以下应该可以完成工作:

    - 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
    

    这与在角色中的每个任务上设置 when 子句基本相同。因此,每个任务都会被检查,但如果条件为假则跳过。

    【讨论】:

    • 谢谢。这就是我目前正在做的事情。虽然这个问题是特定于利用tags 的,但为了避免必须为每个角色添加when 条件。可以在任何级别(包括游戏级别)分配标签,这就是它的优点。
    猜你喜欢
    • 1970-01-01
    • 2017-12-22
    • 2018-09-21
    • 2022-10-24
    • 1970-01-01
    • 2022-07-12
    • 2020-05-08
    • 2021-10-31
    • 2023-03-17
    相关资源
    最近更新 更多