some guidance with roles

Hi,

Im using Ansible 1.2 latest dev release and trying the new feature of roles in order to migrate all of our infrastructure code from a specific vendor cloud manager to Ansible. In our existing infrastructure we use “templates” that are a set of scripts and specific variable values in order to deploy a server (web/ app/ proxy etc…) .

After looking at the documentation and Ansible best practice the current directory tree we have at the moment is :

roles/
└── db

├── tsaks

│ ├── mysql.yml

└── proxy

└── web

└── common
├── handlers
│ ├── all.yml

├── templates

├── vars

├── tasks
│ ├── ssh_users.yml

│ ├── monitoring.yml

If my understanding is correct, the template concept we were using before in Ansbile are achieved with the “roles” feature. The common role should be applied to ALL servers and consist of configuration regarding installing ssh keys & the monitoring agent. For the DB tier we want all common tasks to be applied + all tasks for the db role right ? if that’s the case I’m having problems executing my tasks using the role feature :

test.yml

You will want to create a common/tasks/main.yml file which will be run when include the common role from your top-level playbook. From there you can include other playbooks if needed.

See https://github.com/ansible/ansible-examples/blob/master/language_features/roletest2.yml

Hi John, running ansible-playbook test.yml with the setup as bellow doesn’t work :

test.yml

try this:

path/to/roles/common/tasks/main.yml:—

  • include monitoring.yml

Thanks John, that seems to work. I was trying before with “include:” .

Regarding the vars and handlers files I have available in roles/common/vars/ roles/common/handlers/ needed by my tasks in roles/common/tasks/ , where is it best to define them? inside the master playbook (test.yml) or in the main.yml ? what’s the correct syntax ? do I need to use relative or absolute path ?

Regards,
N.

Please take a look here for some examples:

https://github.com/ansible/ansible-examples/blob/master/lamp_haproxy/site.yml

Note not all examples use roles yet, some just have a directory called “roles” – we need to fix this :slight_smile:

Thank you Michael, the examples you paste are very helpful but I can’t find where are you using the vars ? Why do you have a group_vars and don’t have defined a vars directory for every role ?

Group vars are inventory variables, as described in http://ansible.cc/docs/patterns.html#table-of-contents

Variable directories for roles are optional, so it’s just an organizational choice.