Including playbooks

Playbook files can include other whole playbook files. This construct can be useful to tie together a few independent playbooks into a larger, more comprehensive playbook. Playbook inclusion is a bit more primitive than task inclusion. You cannot perform variable substitution when including a playbook, you cannot apply conditionals, and you cannot apply tags, either. The playbook files to be included must exist at the time of execution as well.

Prior to Ansible 2.4, playbook inclusion was achieved using the include keyword—however, this is deprecated and will be removed in Ansible 2.8, and so it should not be used. Instead, you should now use import_playbook. This is a play-level directiveit cannot be used as a task. However, it is very easy to use. Let's define a simple example to demonstrate this. First, let's create a playbook that will be included, called includeme.yaml:

---
- name: include playbook
hosts: localhost
gather_facts: false

tasks:
- name: an included playbook task
debug:
msg: "I am in the included playbook"

As you will no doubt recognize by now, this is a complete standalone playbook and we could run it in isolation:

However, we can also import this into another playbook. Modify the original includer.yaml playbook so that it looks like this:

---
- name: include playbook
hosts: localhost
gather_facts: false

tasks:
- name: a task
debug:
msg: "I am in the main playbook"

- name: include a playbook
import_playbook: includeme.yaml

When we run this, we can see that both debug messages are displayed, and the imported playbook is run after the initial task, which is the sequence we defined in the original playbook:

In this way, it is very easy to reuse whole playbooks without needing to restructure them into the format of roles or otherwise. Note, however, that this feature is subject to active development in Ansible, so it is recommended that you always refer to the documentation to ensure you can achieve the results you are looking for.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.116.118.198