英文:
Combine importing a playbook and a role inside a block
问题
我有以下的剧本:
---
- import_playbook: myplaybook.yml
when:
- env_tag == 'azure'
- my_var is defined
- hosts: myserver
gather_facts: no
ignore_errors: True
roles:
- role: execute-script
vars:
script_name: myscript.py
args: "{{ arg1 }}"
when:
- env_tag == 'azure'
- my_var is defined
这个在运行。
我想使用一个块来做同样的事情,但是避免使用两个相等的条件。这样做可能吗?我应该怎么做?
英文:
I have the following playbook:
---
- import_playbook: myplaybook.yml
when:
- env_tag == 'azure'
- my_var is defined
- hosts: myserver
gather_facts: no
ignore_errors: True
roles:
- role: execute-script
vars:
script_name: myscript.py
args: "{{ arg1 }}"
when:
- env_tag == 'azure'
- my_var is defined
This is working.
I'd like to use a block to do the same thing but to avoid using 2 equal when(s).
Is it possible? How can I do that?
答案1
得分: 2
以下是您要翻译的内容:
-
You can apply the when condition to a task, block, and role only. import_playbook is the only way how to apply a condition to a playbook.
-
For testing, simplify the code by putting the condition into variables. For example,
shell> cat group_vars/all/conditions.yml
condition_01: "{{ env_tag|d('') == 'azure' and my_var is defined }}"
Then the playbooks
shell> cat pb1.yml
- hosts: all
tasks:
- debug:
var: inventory_hostname
shell> cat pb2.yml
- import_playbook: pb1.yml
when: condition_01
- hosts: all
tasks:
- debug:
var: inventory_hostname
when: condition_01
work as expected
<sup>
shell> ansible-playbook pb2.yml -l test_11 -e my_var=foo -e env_tag=azure
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY RECAP ************************************************************************************
test_11: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</sup>
<hr>
- You can use the condition only once with a third playbook. For example,
shell> cat pb3.yml
- import_playbook: pb2.yml
when: condition_01
shell> cat pb2.yml
- import_playbook: pb1.yml
- hosts: all
tasks:
- debug:
var: inventory_hostname
gives the same result
<sup>
shell> ansible-playbook pb3.yml -l test_11 -e my_var=foo -e env_tag=azure
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY RECAP ************************************************************************************
test_11: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</sup>
英文:
-
You can apply the when condition to a task, block, and role only. import_playbook is the only way how to apply a condition to a playbook.
-
For testing, simplify the code by putting the condition into variables. For example,
shell> cat group_vars/all/conditions.yml
condition_01: "{{ env_tag|d('') == 'azure' and my_var is defined }}"
Then the playbooks
shell> cat pb1.yml
- hosts: all
tasks:
- debug:
var: inventory_hostname
shell> cat pb2.yml
- import_playbook: pb1.yml
when: condition_01
- hosts: all
tasks:
- debug:
var: inventory_hostname
when: condition_01
work as expected
<sup>
shell> ansible-playbook pb2.yml -l test_11 -e my_var=foo -e env_tag=azure
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY RECAP ************************************************************************************
test_11: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</sup>
<hr>
- You can use the condition only once with a third playbook. For example,
shell> cat pb3.yml
- import_playbook: pb2.yml
when: condition_01
shell> cat pb2.yml
- import_playbook: pb1.yml
- hosts: all
tasks:
- debug:
var: inventory_hostname
gives the same result
<sup>
shell> ansible-playbook pb3.yml -l test_11 -e my_var=foo -e env_tag=azure
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY [all] ************************************************************************************
TASK [debug] **********************************************************************************
ok: [test_11] =>
inventory_hostname: test_11
PLAY RECAP ************************************************************************************
test_11: ok=2 changed=0 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
</sup>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论