Ansible Tutorials: Calling one Play & Tasks from another play in Playbook

import_tasks module

The Ansible import_tasks module is used to import a list of tasks from a file into the current playbook for subsequent execution. The name of the imported file is specified directly without any other option. Most keywords, including loops and conditionals, only apply to the imported tasks, not to this statement itself. If you need any of those to apply, use ansible.builtin.include_tasks instead.


- hosts: all
  tasks:
    - ansible.builtin.debug:
        msg: task1

    - name: Include task list in play
      ansible.builtin.import_tasks:
        file: stuff.yaml

    - ansible.builtin.debug:
        msg: task10

include_tasks module


The Ansible include_tasks module is used to dynamically include a task list from a file into the current playbook. This is unlike the import_tasks module, which imports a task list from a file without any dynamic features.

- hosts: all
  tasks:
    - ansible.builtin.debug:
        msg: task1

    - name: Include task list in play
      ansible.builtin.include_tasks:
        file: stuff.yaml

    - ansible.builtin.debug:
        msg: task10

import_playbook module

The Ansible import_playbook module is used to import a playbook from a file into the current playbook for subsequent execution. The name of the imported playbook is specified directly without any other option.


- hosts: localhost
  tasks:
    - ansible.builtin.debug:
        msg: play1

- name: Include a play after another play
  ansible.builtin.import_playbook: otherplays.yaml


Are you trying to use include in your playbook and coming across a error somthing like “A playbook must be a list of plays”. That means you are at right url.

include help you to Includes a file with a list of plays or tasks to be executed in the current playbook. Files with a list of plays can only be included at the top level. Lists of tasks can only be included where tasks normally run (in play).

With include on the task level Ansible expects a file with tasks only, not a full playbook.

------main.yaml------------
- hosts: all
  tasks:
    - debug:
        msg: task1

    - name: Include task list in play
      include: stuff.yaml

------stuff.yaml------------
---
- name: http service state
  service: name=httpd state=started enabled=yes

With include on the task level Ansible expects a file with tasks only, not a full playbook.

------main.yaml------------
- hosts: all
  tasks:
    - debug:
        msg: task1

    - include: stuff.yaml


------stuff.yaml------------
---
- name: http service state
  service: name=httpd state=started enabled=yes

With include on the play level Ansible expects a playbook with hosts/tasks spec. i.e full playbook.

------main.yaml------------
- hosts: all
  tasks:
    - debug:
        msg: task1

- import_playbook: stuff.yaml


------stuff.yaml------------
---
- hosts: web
  tasks:
    - debug:
        msg: task1
    - name: http service state
      service: name=httpd state=started enabled=yes

Example of include calling common tasks.

# 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

Some of more example using Condition

---
tasks:
  - include install_postgres.yml
    when: db == "Y"
Rajesh Kumar
Follow me
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x