FreeKB - Ansible Role Dependencies
Ansible - Role Dependencies

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"
}

 



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 e7200 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   |