Bootstrap FreeKB - Ansible - Role Dependencies
Ansible - Role Dependencies

Updated:   |  Ansible articles

Let's say you have two roles, foo and bar, like this.

/etc/ansible/roles/foo
/etc/ansible/roles/bar

 

Of course, your playbook could invoke both the foo and the bar roles.

---
- hosts: all
  roles:
    - foo
    - bar

 

Or in roles/foo/meta/main.yml, you could have the following.

---
dependencies:
  - bar

 

Or like this.

---
dependencies:
  - role: bar

 

Then you would only have to run the foo role, and the bar role would also be run.

---
- hosts: all
  roles:
    - foo

 

Let's say the foo role contains this task.

- name: foo task
  debug:
    msg: "foo task"

 

Let's say the bar role contains this task.

- name: bar task
  debug:
    msg: "bar task"

 

Variables can also be defined, like this.

---
dependencies:
  - role: bar
    vars: 
      barvar: "my bar var"

 

Now, when using the foo role, the bar tasks will also be invoked.

TASK [bar : bar task] 
ok: [server1.example.com] => {
    "msg": "bar task"
}

TASK [bar : foo task] 
ok: [server1.example.com] => {
    "msg": "foo task"
}

 




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 21cc2e in the box below so that we can be sure you are a human.