Ansible skip import_playbook with variable definition

ansible

I have a main.yaml like below:

- import_playbook: 1.yaml
    
- import_playbook: 2.yaml
  vars:
    allow2: False 
  when: allow2

I want the playbook 2.yaml can be skipped totally (not try to execute any tasks inside 2.yaml).
But it looks all tasks in 2.yaml will be called but not executed.

File 1.yaml:

- name: Go1
  hosts: test
  gather_facts: false 

  tasks:
    - debug: msg="Message from 1.yaml"

File 2.yaml:

- name: Go2
  hosts: test
  gather_facts: false 
  tasks:
    - debug: msg="Message from 2.yaml"

The output is:

$ ansible-playbook main.yaml 

PLAY [Go1] ***********
TASK [debug] *********
Thursday 05 October 2017  03:10:12 -0400 (0:00:00.116)       0:00:00.116 ****** 
ok: [test1] => {}

MSG:

Message from 1.yaml


PLAY [Go2] ************

TASK [debug] ************
Thursday 05 October 2017  03:10:12 -0400 (0:00:00.090)       0:00:00.206 ****** 
skipping: [test1]

The you can say the task in 2.yaml also was called but skipped.
But I want no any tasks will be called in 2.yaml.

Is it possible?

Best Answer

No, this is not possible this way.

Please see answer at serverfault about import/include difference.

import_playbook is static, so it's always done and when statements attached to everything inside it.

Related Topic