Tuesday, August 8, 2017

Difference between Import and Include

Import

All import* statements are pre-processed at the time playbooks are parsed. If you use any import* Task (import_playbook, import_tasks, etc.), it will be static. 

For static imports, the parent task options will be copied to all child tasks contained within the import.

Also in import it executes the code  and substitutes the result into the program. And no code is copied like include and hence no waste of memory or processor’s time.

With import loops cannot be used at all.

Include

# include, replace it with the contents of the file, and continue. All include* statements are processed as they encountered during the execution of the playbook. If you use any include* Task (include_tasks, include_role, etc.), it will be dynamic.

For dynamic includes, the task options will only apply to the dynamic task as it is evaluated, and will not be copied to child tasks.

Tags & Tasks which only exist inside a dynamic include will not show up in –list-tags output & –list-tasks output. You cannot use notify to trigger a handler name which comes from inside a dynamic include You cannot use --start-at-task to begin execution at a task inside a dynamic include.

When using variables for the target file or role name, variables from inventory sources (host/group vars, etc.) cannot be used.

Example for Import and Include

[root@ansim0 ymls]# cat Inc.yml
---
 - hosts: test
   tasks:
    - include: test.yml
      with_items: [ 1,2,3,4]

...

TASK [include] *****************************************************************
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2
included: /root/ymls/test.yml for ansic2

TASK [testing for include and import] ******************************************
ok: [ansic2] => {
    "msg": 1

}



No comments:

Post a Comment