2019-03-08 21:26:45 +01:00
.. _playbooks_reuse_includes:
2017-06-06 23:39:48 +02:00
Including and Importing
=======================
.. contents :: Topics
Includes vs. Imports
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
As noted in :doc: `playbooks_reuse` , include and import statements are very similar, however the Ansible executor engine treats them very differently.
- All `` import* `` statements are pre-processed at the time playbooks are parsed.
2018-08-29 16:14:09 +02:00
- All `` include* `` statements are processed as they are encountered during the execution of the playbook.
2017-06-06 23:39:48 +02:00
Please refer to :doc: `playbooks_reuse` for documentation concerning the trade-offs one may encounter when using each type.
2018-06-11 19:01:53 +02:00
Also be aware that this behaviour changed in 2.4. Prior to Ansible 2.4, only `` include `` was available and it behaved differently depending on context.
2017-09-07 05:50:50 +02:00
.. versionadded :: 2.4
2017-06-06 23:39:48 +02:00
Importing Playbooks
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
It is possible to include playbooks inside a master playbook. For example::
2017-09-07 05:50:50 +02:00
- import_playbook: webservers.yml
- import_playbook: databases.yml
The plays and tasks in each playbook listed will be run in the order they are listed, just as if they had been defined here directly.
Prior to 2.4 only `` include `` was available and worked for both playbooks and tasks as both import and include.
2017-06-06 23:39:48 +02:00
2017-09-07 05:50:50 +02:00
.. versionadded :: 2.4
2017-06-06 23:39:48 +02:00
Including and Importing Task Files
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
2018-06-11 19:01:53 +02:00
Breaking tasks up into different files is an excellent way to organize complex sets of tasks or reuse them. A task file simply contains a flat list of tasks::
2017-06-06 23:39:48 +02:00
# common_tasks.yml
- name: placeholder foo
command: /bin/foo
- name: placeholder bar
command: /bin/bar
2018-06-11 19:01:53 +02:00
You can then use `` import_tasks `` or `` include_tasks `` to execute the tasks in a file in the main task list::
2017-06-06 23:39:48 +02:00
tasks:
- import_tasks: common_tasks.yml
# or
- include_tasks: common_tasks.yml
You can also pass variables into imports and includes::
tasks:
2018-02-05 21:57:37 +01:00
- import_tasks: wordpress.yml
vars:
wp_user: timmy
- import_tasks: wordpress.yml
vars:
wp_user: alice
- import_tasks: wordpress.yml
vars:
wp_user: bob
2017-06-06 23:39:48 +02:00
2018-06-11 19:01:53 +02:00
See :ref: `ansible_variable_precedence` for more details on variable inheritance and precedence.
2017-06-06 23:39:48 +02:00
2018-06-11 19:01:53 +02:00
Task include and import statements can be used at arbitrary depth.
2017-06-06 23:39:48 +02:00
.. note ::
2018-06-11 19:01:53 +02:00
- Static and dynamic can be mixed, however this is not recommended as it may lead to difficult-to-diagnose bugs in your playbooks.
- The `` key=value `` syntax for passing variables to import and include is deprecated. Use YAML `` vars: `` instead.
2017-06-06 23:39:48 +02:00
2018-06-11 19:01:53 +02:00
Includes and imports can also be used in the `` handlers: `` section. For instance, if you want to define how to restart Apache, you only have to do that once for all of your playbooks. You might make a `` handlers.yml `` that looks like::
2017-06-06 23:39:48 +02:00
# more_handlers.yml
- name: restart apache
2018-09-21 15:42:31 +02:00
service:
name: apache
state: restarted
2017-06-06 23:39:48 +02:00
And in your main playbook file::
handlers:
- include_tasks: more_handlers.yml
# or
- import_tasks: more_handlers.yml
.. note ::
Be sure to refer to the limitations/trade-offs for handlers noted in :doc: `playbooks_reuse` .
You can mix in includes along with your regular non-included tasks and handlers.
Including and Importing Roles
`` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ` ``
Please refer to :doc: `playbooks_reuse_roles` for details on including and importing roles.
.. seealso ::
2018-03-14 20:44:21 +01:00
:ref: `yaml_syntax`
2017-06-06 23:39:48 +02:00
Learn about YAML syntax
2018-03-14 20:44:21 +01:00
:ref: `working_with_playbooks`
2017-06-06 23:39:48 +02:00
Review the basic Playbook language features
2018-03-14 20:44:21 +01:00
:ref: `playbooks_best_practices`
2017-06-06 23:39:48 +02:00
Various tips about managing playbooks in the real world
2018-03-14 20:44:21 +01:00
:ref: `playbooks_variables`
2017-06-06 23:39:48 +02:00
All about variables in playbooks
2018-03-14 20:44:21 +01:00
:ref: `playbooks_conditionals`
2017-06-06 23:39:48 +02:00
Conditionals in playbooks
2018-03-14 20:44:21 +01:00
:ref: `playbooks_loops`
2017-06-06 23:39:48 +02:00
Loops in playbooks
2018-03-14 20:44:21 +01:00
:ref: `all_modules`
2017-06-06 23:39:48 +02:00
Learn about available modules
2018-04-20 02:02:30 +02:00
:ref: `developing_modules`
2017-06-06 23:39:48 +02:00
Learn how to extend Ansible by writing your own modules
`GitHub Ansible examples <https://github.com/ansible/ansible-examples> `_
Complete playbook files from the GitHub project source
2018-07-21 15:48:47 +02:00
`Mailing List <https://groups.google.com/group/ansible-project> `_
2017-06-06 23:39:48 +02:00
Questions? Help? Ideas? Stop by the list on Google Groups