Bo Wang
Bo Wang

Reputation: 509

Ansible skip import_playbook with variable definition

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?

Upvotes: 7

Views: 10794

Answers (2)

vbourdouvalis
vbourdouvalis

Reputation: 49

In case you do not need any conditional format and just skip running the playbook 2.yaml you may use tags by modifying the main.yaml as the following:

- import_playbook: 1.yaml
    
- import_playbook: 2.yaml
  tags: playbook_2

And run as by skipping tag playbook_2:

bill@inspiron:~/tmp_ansible$ ansible-playbook main.yaml --skip-tags=playbook_2
[WARNING]: provided hosts list is empty, only localhost is available. Note that the implicit localhost does not match 'all'

PLAY [Go1] **************************************************************************************************************************************************************************************

TASK [debug] ************************************************************************************************************************************************************************************
ok: [localhost] => {
    "msg": "Message from 1.yaml"
}

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

PLAY RECAP **************************************************************************************************************************************************************************************
localhost                  : ok=1    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0 

Upvotes: 0

Konstantin Suvorov
Konstantin Suvorov

Reputation: 68229

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.

Upvotes: 7

Related Questions