Skip to content

Example 3

hosts.yml

centos:
  hosts:
    provisioner:
      ansible_connection: local
    centos1:
      ansible_host: '192.168.77.22'
ubuntu:
  hosts:
    ubuntu1:
      ansible_host: '192.168.77.23'

tasks_import.yml

- name: Import1
  debug:
    msg: Import1
- name: Set import_tasks_condition
  set_fact:
    import_tasks_condition: false
- name: Import2
  debug:
    msg: Import2
- name: Import3
  debug:
    msg: Import3

tasks_include.yml

- name: Include1
  debug:
    msg: Include1
- name: Set include_tasks_condition
  set_fact:
    include_tasks_condition: false
- name: Include2
  debug:
    msg: Include2
- name: Include3
  debug:
    msg: Include3

playbook.yml

- name: Example playbook
  hosts: centos1
  gather_facts: false
  tasks:
    - name: Include tasks run
      include_tasks: tasks_include.yml
      when: include_tasks_condition is not defined
    - name: Import tasks run
      import_tasks: tasks_import.yml
      when: import_tasks_condition is not defined

Tip

You can use facts to regulate the behavior of include_* and import_* statements.

Execute the following command

ansible-playbook playbook.yml

What is the expected output of the command ?

Since the import_* statements are pre-processed the execution should be skipped for Import 2 and Import 3.