
There are a few different ways to invoke one or more roles in a playbook.
- roles
- import_playbook (this article)
- import_role
- include_role
- include_tasks
- import_tasks
Let's say you have the following structure.
├── /usr/local
│ ├── main.yml
│ ├── roles
│ │ ├── foo
│ │ ├── ├── tasks
│ │ ├── ├── ├── main.yml
│ │ ├── bar
│ │ ├── ├── tasks
│ │ ├── ├── ├── main.yml
IMPORTANT - import_playbook cannot be used in the tasks plugin
For example, this would not work. If you want to include the tasks in a playbook during tasks execution, the include_role module or include_tasks module can be used instead.
---
- hosts: all
tasks:
import_playbook: foo.yml
...
Here is how you could import the foo.yml playbook, then run some tasks, and then import the bar.yml playbook.
---
- hosts: all
- name: import 'foo.yml'
import_playbook: foo.yml
tasks:
- file:
path: /tmp/foo.txt
state: touch
- name: import 'bar.yml'
import_playbook: bar.yml
...
IMPORTANT - The when parameter cannot be used with import_playbook.
Attempting to use the when parameter, like this, would return a fatal error. For this reason, the include_role module or include_tasks module can be used instead, as the when parameter can be used with these modules.
--- hosts: all
- import_playbook: foo.yml
when: foo == bar
...
Did you find this article helpful?
If so, consider buying me a coffee over at