Ansible - include_tasks module

If you are not familiar with modules, check out Ansible - Getting Started with Modules.

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

include_tasks can be used to include tasks from one playbook in another. Let's say main.yml and foo.yml are located in the same directory.

/usr/local/ansible/playbooks/main.yml
/usr/local/ansible/playbooks/foo.yml

 

Let's say main.yml contains the following. 

---
- hosts: localhost
  tasks:
  - debug:
      msg: "Hello"

  - name: include foo.yml
    include_tasks: foo.yml
...

 

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

- debug:
    msg: "World"

 

Running this command.

ansible-playbook main.yml

 

Should return the following.

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

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

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

 

Let's say main.yml and foo.yml are located in different directories.

/usr/local/ansible/playbooks/main.yml
/usr/local/ansible/roles/foo/tasks/foo.yml

 

In this scenario, the absolute or relative directory to foo.yml must be used.

---
- hosts: localhost
  tasks:
  - debug:
      msg: "Hello"

  - name: include foo.yml
    include_tasks: roles/foo/tasks/foo.yml
...

 

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

---
- hosts: localhost
  tasks:
  - debug:
      msg: "Hello"

  - name: include foo.yml
    include_tasks: foo.yml
    vars:
      bar: World
...

 



Add a Comment




We will never share your name or email with anyone. Enter your email if you would like to be notified when we respond to your comment.





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




Comments

Web design by yours truely - me, myself, and I   |   jeremy.canfield@freekb.net   |