Find the Best Cosmetic Hospitals

Explore trusted cosmetic hospitals and make a confident choice for your transformation.

โ€œInvest in yourself โ€” your confidence is always worth it.โ€

Explore Cosmetic Hospitals

Start your journey today โ€” compare options in one place.

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: task10Code language: CSS (css)

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: task10Code language: CSS (css)

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

Code language: CSS (css)

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=yesCode language: PHP (php)

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

Code language: PHP (php)

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
Code language: PHP (php)

Some of more example using Condition

---
tasks:
  - include install_postgres.yml
    when: db == "Y"Code language: PHP (php)

Find Trusted Cardiac Hospitals

Compare heart hospitals by city and services โ€” all in one place.

Explore Hospitals
Iโ€™m a DevOps/SRE/DevSecOps/Cloud Expert passionate about sharing knowledge and experiences. I have worked at <a href="https://www.cotocus.com/">Cotocus</a>. I share tech blog at <a href="https://www.devopsschool.com/">DevOps School</a>, travel stories at <a href="https://www.holidaylandmark.com/">Holiday Landmark</a>, stock market tips at <a href="https://www.stocksmantra.in/">Stocks Mantra</a>, health and fitness guidance at <a href="https://www.mymedicplus.com/">My Medic Plus</a>, product reviews at <a href="https://www.truereviewnow.com/">TrueReviewNow</a> , and SEO strategies at <a href="https://www.wizbrand.com/">Wizbrand.</a> Do you want to learn <a href="https://www.quantumuting.com/">Quantum Computing</a>? <strong>Please find my social handles as below;</strong> <a href="https://www.rajeshkumar.xyz/">Rajesh Kumar Personal Website</a> <a href="https://www.youtube.com/TheDevOpsSchool">Rajesh Kumar at YOUTUBE</a> <a href="https://www.instagram.com/rajeshkumarin">Rajesh Kumar at INSTAGRAM</a> <a href="https://x.com/RajeshKumarIn">Rajesh Kumar at X</a> <a href="https://www.facebook.com/RajeshKumarLog">Rajesh Kumar at FACEBOOK</a> <a href="https://www.linkedin.com/in/rajeshkumarin/">Rajesh Kumar at LINKEDIN</a> <a href="https://www.wizbrand.com/rajeshkumar">Rajesh Kumar at WIZBRAND</a> <a href="https://www.rajeshkumar.xyz/dailylogs">Rajesh Kumar DailyLogs</a>

Related Posts

Ansible Tower Install and Setup Quick Guide

To installย Red Hat Ansible Tower 3.8.6ย (usingย ansible-tower-setup-3.8.6-2.tar.gz), your environment must meet a set of hardware, software, and system prerequisites. Below are the summarized requirements specific to this Tower…

Read More

Ansible: How to use template in Ansible using Jinja2?

Hereโ€™s a comprehensive tutorial on Ansible Templates and Jinja2, suitable for engineers or anyone preparing for an advanced Ansible role. 1. What are Ansible Templates? Ansible templates…

Read More

What is Ansible and use cases of Ansible?

What is Ansible? Ansible is an open-source automation tool used for configuration management, application deployment, and task automation. It simplifies complex IT tasks by automating them using…

Read More

Ansible Tutorial: Anatomy of Ansible playbook defined!

host defines the target machines: one or more groups or host patterns, separated by colons that should match hosts in the inventory. all is a group that…

Read More

Complete Ansible Certification Guide & tutorials

What is Ansible? Ansible is an open-source tool which can automate the configuration of all the systems. With ansible a small team of system administrator can write…

Read More

TOP 65+ INTERVIEW QUESTIONS FOR ANSIBLE

Ansible most asked FAQ’s and interview questions:- Q1) what is Ansible? Ansible is developed in Python language. It is a software tool. It is useful while deploying…

Read More
Subscribe
Notify of
guest
0 Comments
Newest
Oldest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x