How do I include another playbook in current playbook?

5,117 views
Skip to first unread message

Xinhuan Zheng

unread,
Jan 22, 2020, 3:46:59 PM1/22/20
to Ansible Project
Hello,

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

---
# file: current_playbook.yml

- hosts: mywebservers
  gather_facts: yes

#- import_playbook: another.yml

  roles:
    - role: testrole1
      tags: testrole1

  post_tasks:
    - name: Install configuration file
      template:
        src: myconf.j2
        dest: /remote-path/myconf
      tags: testrole1

    - name: Install cron
      cron:
         name: 'run every day'
         minute: '0'
         hour: '0'
         job: "/remote-path/job"
       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
  gather_facts: yes

  roles:
    - role: myrole1
    - 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
  ^ here

How do I call another.yml playbook in my current_playbook.yml file?

Thank you,

- Xinhuan Zheng

Mauricio Tavares

unread,
Jan 22, 2020, 4:13:19 PM1/22/20
to ansible...@googlegroups.com
Take a look again at the example in
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?

> --
> You received this message because you are subscribed to the Google Groups "Ansible Project" group.
> To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.
> To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/d725708b-622c-4f93-b25c-89cddc114d38%40googlegroups.com.

Xinhuan Zheng

unread,
Jan 22, 2020, 4:24:45 PM1/22/20
to Ansible Project

      Take a look again at the example in
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

alicia

unread,
Jan 23, 2020, 12:33:57 PM1/23/20
to ansible...@googlegroups.com
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. You may want to edit “another.yml” to make it a tasks file instead of a playbook for greater flexibility.

Hope this helps,
Alicia

--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-proje...@googlegroups.com.

Xinhuan Zheng

unread,
Feb 11, 2020, 4:31:51 PM2/11/20
to Ansible Project
Hello Alicia,

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

- Xinhuan
  import_playbook: another.yml

To unsubscribe from this group and stop receiving emails from it, send an email to ansible...@googlegroups.com.
Reply all
Reply to author
Forward
0 new messages