FreeKB - Ansible Getting Started with Roles
Ansible - Getting Started with Roles

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:

With the roles plugin, you will want to create the roles directories, subdirectories, and files.You can:

Here is an example of the directory structure used with roles.

roles/foo/defaults/main.yml
roles/foo/files/bar.txt
roles/foo/handlers/main.yml
roles/foo/meta/main.yml
roles/foo/tasks/main.yml
roles/foo/templates/main.yml
roles/foo/vars/main.yml

 


Where should the "roles" files and directories reside?

Let's say your playbook wants to use the "foo" role.

---
- hosts: all
  roles:
    - foo

 

Or like this.

---
- hosts: all
  roles:
    - role: foo

 

Ansible will search the following directories for the "foo" role:

  • The directory that contains your playbook (e.g. /home/john.doe/foo)
  • /etc/ansible/roles/foo

Let's say you define some other roles_path in ansible.cfg.

roles_path = /usr/local/ansible/roles

 

Now Ansible will search the following directories for the "foo" role:

  • The directory that contains your playbook (e.g. /home/john.doe/foo)
  • /usr/local/ansible/roles

 

It is noteworthy that you have a role that resides in some other directory, say /usr/local/testing/roles, the role can be used by including the absolute path to the role in your playbook.

---
- hosts: all
  roles:
    - /usr/local/testing/roles/foo

 


Variables

Each role can have it's own unique set of variables. Refer to Role Variables.

 


pre_tasks and post_tasks

When using the roles module, tasks will be performed in this order:

 


Perform tasks on the control node

Refer to run tasks on the control node if you want to run this module on your control node.



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