Ignore imported playbooks completely when defined

Hi,

I’m using ansible version 2.7.17 and combining multiple playbooks in one file.
One of my playbooks I’m running conditionally with when and a condition.
My problem is when my condition is false, I want ansible to completely ignore my playbook and not load anything related to this playbook.
I tried using both import_playbook and include, which seems to have similar results.
The call to the playbook looks like that:

  • name: Include kubespray tasks
    include: …/kubespray/cluster.yml
    when: cluster_installation

in my output you can see ansible run the handlers inside the ignored playbook (keeping for 2 more minutes):

PLAYBOOK: cluster.yml *******************************************************************************************************************************************************************************************
16 plays in cluster.yml

PLAY [localhost] ************************************************************************************************************************************************************************************************
META: ran handlers
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.043) 0:00:00.043 *********
META: ran handlers
META: ran handlers
[WARNING]: Could not match supplied host pattern, ignoring: bastion

PLAY [bastion[0]] ***********************************************************************************************************************************************************************************************
skipping: no hosts matched

PLAY [k8s-cluster:etcd] *****************************************************************************************************************************************************************************************
META: ran handlers
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.037) 0:00:00.081 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.049) 0:00:00.130 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.048) 0:00:00.178 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.051) 0:00:00.230 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.024) 0:00:00.254 *********

Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.021) 0:00:00.275 *********

Thanks for the help,
Ronen

Hi,

I'm using ansible version 2.7.17 and combining multiple playbooks in one file.
One of my playbooks I'm running conditionally with when and a condition.
My problem is when my condition is false, I want ansible to completely ignore my playbook and not load anything related
to this playbook.
I tried using both import_playbook and include, which seems to have similar results.
The call to the playbook looks like that:

- name: Include kubespray tasks
include: ../kubespray/cluster.yml
when: cluster_installation

in my output you can see ansible run the handlers inside the ignored playbook (keeping for 2 more minutes):

PLAYBOOK: cluster.yml
*******************************************************************************************************************************************************************************************
16 plays in cluster.yml

PLAY [localhost]
************************************************************************************************************************************************************************************************
META: ran handlers
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.043) 0:00:00.043 *********
META: ran handlers
META: ran handlers
[WARNING]: Could not match supplied host pattern, ignoring: bastion

PLAY [bastion[0]]
***********************************************************************************************************************************************************************************************
skipping: no hosts matched

PLAY [k8s-cluster:etcd]
*****************************************************************************************************************************************************************************************
META: ran handlers
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.037) 0:00:00.081 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.049) 0:00:00.130 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.048) 0:00:00.178 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.051) 0:00:00.230 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.024) 0:00:00.254 *********
Tuesday 31 March 2020 18:55:46 +0000 (0:00:00.021) 0:00:00.275 *********

Thanks for the help,
Ronen

Hello Ronen,

as far as I know that's not possible. Ansible will parse the role even if the condition evalulates to false.
Also note that allows you to use the variables in the role, even if all tasks are skipped due to the condition.

Does it really run the handlers? I don't see no output indicating this.

Regards
         Racke

I assumed from the: META: ran handlers in the output that it’s something related to the handlers.

It’s odd that only parsing takes 2 minutes, I know that kubespray is a big project, but 2 minutes just to parse files is a lot of time.