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.yaml
it'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.
You can include playbooks inside other playbooks by using the import_playbook directive.
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With