Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ansible: How to call a playbook from another?

Tags:

ansible

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

[root@server thebigone]# cat check_java_pid.yaml --- - hosts: all   gather_facts: no    tasks:     - name: Check PID of existing Java process       shell: "ps -ef | grep [j]ava"       register: java_status      - debug: var=java_status.stdout 

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

    [root@server thebigone]# cat instance_restart.yaml     ---     - hosts: instance_1       gather_facts: no        tasks:         - include: check_java_pid.yaml 

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

    ERROR! no action detected in task. This often indicates a misspelled      module name, or incorrect module path.      The error appears to have been in      '/home/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3, but      may be elsewhere in the file depending on the exact syntax problem.      The offending line appears to be:       ---      - hosts: all        ^ here         The error appears to have been in        '/home/root/ansible/thebigone/check_java_pid.yaml': line 2, column 3,        but may be elsewhere in the file depending on the exact syntax problem.       The offending line appears to be:        ---       - hosts: all         ^ here 

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.

like image 426
rɑːdʒɑ Avatar asked Apr 25 '17 11:04

rɑːdʒɑ


People also ask

How do I add another playbook to Ansible?

You can include playbooks inside other playbooks by using the import_playbook directive.

How do I run multiple playbooks in Ansible?

you can run more playbooks using "ansible-playbook [OPTIONS] *. yml" command. This will execute all the playbooks NOT IN PARALLEL WAY, but in serial way, so first one playbook and after the execution, another playbook. This command can be helpful if you have many playbooks.

What is Include_tasks in Ansible?

Note. This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name include_tasks even without specifying the collections: keyword.


1 Answers

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

- name: Check PID of existing Java process   shell: "ps -ef | grep [j]ava"   register: java_status    - debug: var=java_status.stdout 

check_java_pid.yml

--- - hosts: all   gather_facts: no    tasks:     - include my_tasks.yml 

instance_restart.yml

--- - hosts: instance_1   gather_facts: no    tasks:     - include: my_tasks.yml 
like image 165
techraf Avatar answered Sep 21 '22 15:09

techraf