Hello,
I created a playbook which needs to call another playbook. This is my current playbook:
Hello,
I created a playbook which needs to call another playbook. This is my current playbook:
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: testrole1post_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: testrole1I 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: yesroles:
- role: myrole1
- role: myrole2When 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
^ hereHow do I call another.yml playbook in my current_playbook.yml file?
Thank you,
- Xinhuan Zheng
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?
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 whereroles:
- role: testrole1
tags: testrole1belongs. Should it be
- hosts: mywebservers
gather_facts: yesroles:
- 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,
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:
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
Hello Alicia,
This is great. I got the idea. Thanks for your help!