![](/img/trans.png)
[英]Ansible set environment variable from a file and access it within the playbook
[英]How to set an Ansible tag from within a playbook?
我想基於條件值跳過一些戲劇。 我可以在開始時執行一項任務,在滿足條件時跳過其他一些任務。
我知道我總是可以使用set_fact
並將其作為運行其他游戲(角色和任務)的條件,或者在我想跳過的每個游戲中直接評估所述條件。
但是,由於游戲已經有了標記系統,在set_tags
中有類似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
如果我理解正確,應該做以下事情。
- 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子句基本相同。 因此,如果條件為假,將檢查每個任務但跳過。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.