Ansible: How to call a playbook from another?

Question:

I have written a simple playbook to print java process ID and other information of that PID

And when I am calling this with ansible-playbook check_java_pid.yamlit’s working fine.

Now I am trying to call the above playbook from another one but only for a specific host. So I have written the 2nd playbook as below

But while doing ansible-playbook instance_restart.yaml, I am getting below errors

Its saying syntax error but there isn’t one really AFAIK as I have executed Playbook check_java_pid.yaml without any issues.

Requesting your help on understanding this issue.

Answer:

With include on the task level Ansible expects a file with tasks only, not a full playbook. Yet you provide a full playbook as an argument.

You could do it (include) on a play level, but it won’t let you achieve what you want.

The play with hosts: all defined will always run against all targets (unless you limit it in the command call or the inventory).

Moreover, you will have troubles accessing the java_status value from the other playbook (if this was your goal).


You need to rethink your structure, for example you can for example extract the task(s) and include them from both plays:

my_tasks.yml


check_java_pid.yml

instance_restart.yml

Leave a Reply