我配置了几个set_fact任务。然而,对我来说,感觉这并不干燥。
例如,我配置了 2 个不同的任务来根据不同的 when 子句设置事实:
- set_fact:
installed: false
when: "'abc' not in ansible_facts.packages"
- set_fact:
installed: true
when: "'abc' in ansible_facts.packages"
Run Code Online (Sandbox Code Playgroud)
我使用的另一个例子:
- name: set fact for bootstrapper
set_fact:
bootstrapper: true
when: cluster.bootstrapper
- name: set fact for not bootstrapper
set_fact:
bootstrapper: false
when: not cluster.bootstrapper
Run Code Online (Sandbox Code Playgroud)
问:是否有更有效的方法可以在单个任务中设置所有这些事实?
不要根据布尔条件设置硬编码布尔值,而是将变量设置为评估条件的结果。
- set_fact:
installed: "{{ 'abc' in ansible_facts.packages }}"
bootstrapper: "{{ cluster.bootstrapper is truthy }}"
Run Code Online (Sandbox Code Playgroud)
(您可能需要根据预期内容cluster.bootstrapper以及您使用的 Ansible 版本调整第二个示例。)
| 归档时间: |
|
| 查看次数: |
1711 次 |
| 最近记录: |