1

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?

fr0zt
  • 733
  • 4
  • 12
  • 30

1 Answers1

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

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

  • 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

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
Vladimir Botka
  • 58,131
  • 4
  • 32
  • 63