Block
Are you looking to get certified in DevOps, SRE and DevSecOps?
DevOps
Get Certified!
SRE
Ahead from others!
DevSecOps
Security is Key
Kubernetes
Tomorow's Platform!

Ansible include scenario with list of plays or tasks

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

- include: 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