ansiblerhelansible-facts

How can I exclude task in playbook file for OS version?


We have the following example playbook file with import_playbook tasks:

- import_playbook: "{{playbook_folder}}/install_stage1.yml"
- import_playbook: "{{playbook_folder}}/install_stage2.yml"
- import_playbook: "{{playbook_folder}}/install_stage3.yml"
- import_playbook: "{{playbook_folder}}/install_stage4.yml"
- import_playbook: "{{playbook_folder}}/install_stage5.yml"
- import_playbook: "{{playbook_folder}}/install_stage6.yml"
- import_playbook: "{{playbook_folder}}/install_stage7.yml"

Each of above import_playbook tasks is installing rpm's on RHEL OS Linux machine

What we need is to exclude install_stage7.yml only for RHEL 7.x, while the others - install_stage1.yml - install_stage6.yml can be runs on RHEL version 7.9 and 8.6

From my understanding we can use the following

 when: ansible_facts['distribution_version'] == "8.6"

But not sure how to set that condition for an import_playbook


Solution

  • Just add a when clause to your import:

    - import_playbook: "{{playbook_folder}}/install_stage1.yml"
    - import_playbook: "{{playbook_folder}}/install_stage2.yml"
    - import_playbook: "{{playbook_folder}}/install_stage3.yml"
    - import_playbook: "{{playbook_folder}}/install_stage4.yml"
    - import_playbook: "{{playbook_folder}}/install_stage5.yml"
    - import_playbook: "{{playbook_folder}}/install_stage6.yml"
    - import_playbook: "{{playbook_folder}}/install_stage7.yml"
      when: ansible_facts['distribution_version'] == "8.6"
    

    Note that the condition is not applied to the import but to every task which is imported. So when the condition is false, all the tasks inside the imported playbook will appear as skipped in the output.