Raja G
Raja G

Reputation: 6673

Ansible: How to call a playbook from another?

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.

Upvotes: 41

Views: 128895

Answers (2)

techraf
techraf

Reputation: 68629

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

Upvotes: 36

QkiZ
QkiZ

Reputation: 878

Here you have examples in official documentation.

https://docs.ansible.com/ansible/2.4/playbooks_reuse_includes.html

I had same error as yours after applying the aproved answer. I resolved problem by creating master playbook like this:

---
- import_playbook: master-okd.yml
- import_playbook: infra-okd.yml
- import_playbook: compute-okd.yml

Upvotes: 38

Related Questions