# How do I include another playbook in current playbook?

**URL:** <https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031>\
**Category:** Ansible Project\
**Created:** [January 22, 2020, 8:46pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031 "2020-01-22T20:46:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 22, 2020, 8:46pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031/1 "2020-01-22T20:46:59Z")

</div>

Hello,

I created a playbook which needs to call another playbook. This is my current playbook:

---

<div class="post-metadata">

**Author:** ![Mauricio\_Tavares](https://avatars.discourse-cdn.com/v4/letter/m/b5e925/32.png) [@Mauricio\_Tavares](https://forum.ansible.com/u/Mauricio_Tavares)\
**Post date:** [January 22, 2020, 9:12pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031/2 "2020-01-22T21:12:53Z")

</div>

> Hello,
> 
> I created a playbook which needs to call another playbook. This is my current playbook:
> 
> ---  
> # file: current\_playbook.yml
> 
> - hosts: mywebservers  
> &nbsp;&nbsp;gather\_facts: yes
> 
> #- import\_playbook: another.yml
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: testrole1  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: testrole1
> 
> &nbsp;&nbsp;post\_tasks:  
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Install configuration file  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;template:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;src: myconf.j2  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;dest: /remote-path/myconf  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: testrole1
> 
> &nbsp;&nbsp;&nbsp;&nbsp;- name: Install cron  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cron:  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;name: 'run every day'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;minute: '0'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;hour: '0'  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;job: "/remote-path/job"  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: testrole1
> 
> I want to run playbook like: ansible-playbook -i myinventory -l mywebservers current\_playbook.yml --tags testrole1. But another.yml playbook needs to run first. Here is another.yml playbook:
> 
> ---  
> # file: another.yml
> 
> - hosts: all  
> &nbsp;&nbsp;gather\_facts: yes
> 
> &nbsp;&nbsp;roles:  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: myrole1  
> &nbsp;&nbsp;&nbsp;&nbsp;- role: myrole2
> 
> When I comment back in `#- import\_playbook: another.yml' line in current\_playbook.yml file, I got below error:
> 
> ERROR! 'roles' is not a valid attribute for a PlaybookInclude  
> - import\_playbook: another.yml  
> &nbsp;&nbsp;^ here
> 
> How do I call another.yml playbook in my current\_playbook.yml file?
> 
> Thank you,
> 
> - Xinhuan Zheng

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Take a look again at the example in  
[https://docs.ansible.com/ansible/latest/modules/import\_playbook\_module.html](https://docs.ansible.com/ansible/latest/modules/import_playbook_module.html).  
To me it does not know where

&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role: testrole1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: testrole1

belongs. Should it be

- hosts: mywebservers  
&nbsp;&nbsp;gather\_facts: yes

&nbsp;&nbsp;roles:  
&nbsp;&nbsp;&nbsp;&nbsp;- role: testrole1  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;tags: testrole1

#- import\_playbook: another.yml

i.e. those roles are related to mywebservers? Or are they related to  
all hosts as defined in another.yml?

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [January 22, 2020, 9:24pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031/3 "2020-01-22T21:24:45Z")

</div>

> Take a look again at the example in  
> [https://docs.ansible.com/ansible/latest/modules/import\_playbook\_module.html](https://docs.ansible.com/ansible/latest/modules/import_playbook_module.html).  
> To me it does not know where
> 
> roles:
> 
> - role: testrole1  
> tags: testrole1
> 
> belongs. Should it be
> 
> - hosts: mywebservers  
> gather\_facts: yes
> 
> roles:
> 
> - role: testrole1  
> tags: testrole1
> 
> #- import\_playbook: another.yml
> 
> i.e. those roles are related to mywebservers? Or are they related to  
> all hosts as defined in another.yml?

testrole1 belongs to mywebservers. It isn’t related to all hosts as defined in another.yml file. However, another.yml file needs to be called first. I tried using pre\_tasks with import\_playbook. It doesn’t work either. I also tried using include, still not working. Since another.yml file contains a list of roles, it is supposed to be import\_playbook, but I’m not sure how to make import\_playbook working in current\_playbook.yml file.

Thanks again,

- Xinhuan

---

<div class="post-metadata">

**Author:** ![acozine1](https://avatars.discourse-cdn.com/v4/letter/a/c89c15/32.png) [@acozine1](https://forum.ansible.com/u/acozine1)\
**Post date:** [January 23, 2020, 5:25pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031/4 "2020-01-23T17:25:16Z")

</div>

You cannot import a playbook anywhere inside a play - importing a playbook is a play of its own.

If you want to run the imported playbook first, try:

- name: this play runs ‘another.yml’ on the hosts it defines  
import\_playbook: another.yml

- name: this play runs two roles on all hosts in the mywebservers group  
hosts: mywebservers  
gather\_facts: yes

roles:

- role: testrole1  
tags: testrole1
- role: testrole2  
tags: othertag

If you want to run the roles first, reverse the order of the two plays.

You can also review the general documentation on importing and including at [https://docs.ansible.com/ansible/devel/user\_guide/playbooks\_reuse.html](https://docs.ansible.com/ansible/devel/user_guide/playbooks_reuse.html). You may want to edit “another.yml” to make it a tasks file instead of a playbook for greater flexibility.

Hope this helps,  
Alicia

---

<div class="post-metadata">

**Author:** ![Xinhuan\_Zheng](https://avatars.discourse-cdn.com/v4/letter/x/a88e57/32.png) [@Xinhuan\_Zheng](https://forum.ansible.com/u/Xinhuan_Zheng)\
**Post date:** [February 11, 2020, 9:31pm UTC](https://forum.ansible.com/t/how-do-i-include-another-playbook-in-current-playbook/32031/5 "2020-02-11T21:31:51Z")

</div>

Hello Alicia,

This is great. I got the idea. Thanks for your help!

- Xinhuan
