Bootstrap FreeKB - Ansible - Importing tasks using import_tasks
Ansible - Importing tasks using import_tasks

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


import_tasks can be used to import 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: all
  - import_tasks: hello.yml


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

- name: greeting
    msg: Hello World


Running the main.yml playbook . . . 

ansible-playbook main.yml


Should return something like this.

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
  - import_tasks: roles/foo/tasks/main.yml

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


Did you find this article helpful?

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


Add a Comment

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