Ansible - Including and Importing
include* vs. import* | ( pre 2.4 only include existed and behaved differently) |
import* | pre-processed when playbook is read |
include* | processed when reached during playbook execution |
Include playbooks inside master playbook (tasks/plays run in order ):
- import_playbook: webservers.yml
- import_playbook: databases.yml
Task files can be defined:
# common_tasks.yml
- name: placeholder foo
command: /bin/foo
- name: placeholder bar
command: /bin/bar
import or include task files:
tasks:
- import_tasks: common_tasks.yml
# or
- include_tasks: common_tasks.yml
Using variables with imports / includes:
tasks:
- import_tasks: wordpress.yml
vars:
wp_user: timmy
- import_tasks: wordpress.yml
vars:
wp_user: alice
- import_tasks: wordpress.yml
vars:
wp_user: bob
Using includes for handlers ( so you can share handlers between playbooks):
# more_handlers.yml
- name: restart apache
service:
name: apache
state: restarted
handlers:
- include_tasks: more_handlers.yml
# or
- import_tasks: more_handlers.yml