Bootstrap FreeKB - Ansible - Including task in a playbook using the include_tasks module
Ansible - Including task in a playbook using the include_tasks module

Updated:   |  Ansible articles

Let's say you have a master playbook, and in the master playbook you want to run the foo.yml and bar.yml playbooks. This could be done using:

Let's say you have the following structure.

├── /usr/local
│   ├── main.yml
│   ├── hello.yml
│   ├── roles
│   │   ├── foo
│   │   ├── ├── tasks
│   │   ├── ├── ├── main.yml
│   │   ├── bar
│   │   ├── ├── tasks
│   │   ├── ├── ├── main.yml

 

include_tasks can be used to include tasks from one playbook in another. In this example, since the main playbook main.yml and hello.yml are in the same directory, you could do something like this.

---
- hosts: localhost
  tasks:
  - include_tasks: hello.yml
...

 

And let's say hello.yml contains the following.

- name: greeting
  debug:
    msg: Hello World

 

Running the main.yml playbook . . . 

ansible-playbook main.yml

 

Should return something like this.

TASK [include foo.yml] 
included: /usr/local/hello.yml for localhost

TASK [greeting] 
ok: [localhost] => {
    "msg": "Hello World"
}

 

Since the main.yml playbooks for the "foo" and "bar" roles are not in the same directory as the main playbook main.yml, in this scenario, the absolute or relative path to the main.yml playbooks for the "foo" and "bar" roles would need to be used.

---
- hosts: localhost
  tasks:
  - include_tasks: roles/foo/tasks/main.yml

  - include_tasks: roles/bar/tasks/main.yml
...

 

vars can be used to pass variables from main.yml to foo.yml like this.

---
- hosts: localhost
  tasks:
  - include_tasks: hello.yml
    vars:
      bar: World
...

 




Did you find this article helpful?

If so, consider buying me a coffee over at Buy Me A Coffee



Comments


Add a Comment


Please enter b37653 in the box below so that we can be sure you are a human.