合并导入一个剧本和一个角色在一个块内。

huangapple go评论73阅读模式
英文:

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&gt; ansible-playbook pb2.yml -l test_11 -e my_var=foo -e env_tag=azure

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  inventory_hostname: test_11

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  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&gt; cat pb3.yml
- import_playbook: pb2.yml
  when: condition_01
shell&gt; cat pb2.yml
- import_playbook: pb1.yml

- hosts: all
  tasks:
    - debug:
        var: inventory_hostname

gives the same result

<sup>

shell&gt; ansible-playbook pb3.yml -l test_11 -e my_var=foo -e env_tag=azure

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  inventory_hostname: test_11

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  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&gt; cat group_vars/all/conditions.yml
condition_01: &quot;{{ env_tag|d(&#39;&#39;) == &#39;azure&#39; and my_var is defined }}&quot;

Then the playbooks

shell&gt; cat pb1.yml
- hosts: all
  tasks:
    - debug:
        var: inventory_hostname
shell&gt; 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&gt; ansible-playbook pb2.yml -l test_11 -e my_var=foo -e env_tag=azure

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  inventory_hostname: test_11

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  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&gt; cat pb3.yml
- import_playbook: pb2.yml
  when: condition_01
shell&gt; cat pb2.yml
- import_playbook: pb1.yml

- hosts: all
  tasks:
    - debug:
        var: inventory_hostname

gives the same result

<sup>

shell&gt; ansible-playbook pb3.yml -l test_11 -e my_var=foo -e env_tag=azure

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  inventory_hostname: test_11

PLAY [all] ************************************************************************************

TASK [debug] **********************************************************************************
ok: [test_11] =&gt; 
  inventory_hostname: test_11

PLAY RECAP ************************************************************************************
test_11: ok=2    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

</sup>

huangapple
  • 本文由 发表于 2023年5月23日 01:43:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/76308720.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定