Roles and playbooks in subdirectories

Dear list,

one thing that is really nice about Ansible is how the playbook
becomes the centre of execution, and if you run

  ansible-playbook /path/to/my/playbook

then that directory is where Ansible will also look for templates
required in the tasks of this playbook. Apparently, you can also now
define group_vars/host_vars next to the playbook, so it's a big step
towards self-containment.

Enter roles, reusable stuff to be used from playbooks. Roles are
looked for in the subdirectory roles/ in the parent directory of the
playbook, or you can specify a relative path to the directory of
another playbook, or you can specify an absolute path to a role
directory per se.

Now, what is the best approach if I want to use the same role from
both, playbook1/foo.yml and playbook2/bar.yml?

I'd like to avoid hard-coding absolute paths, as people clone from
Git to their own locations;

I don't even want to assume that playbook1 and playbook2 are in the
same place on the filesystem;

And I'd rather not put one copy of a role into playbook1/ and
another copy into playbook2/.

Is there another way?

If not, I could imagine two solutions:

  1. if a role is not found in the usual location, see if a roles/
     directory next to the inventory has it;

  2. provide a configuration option and maybe environment variable
     with which to specify a roles search path. This would be
     especially useful if distros started providing roles somewhere
     in /usr/share, encouraging people to use them.

The two options can also obviously be combined.

What do you think?

Hi,

Another way:

  • create symlinks (either for whole roles directory or just specific roles inside it) (it is not nice, but it could get your job done for now)

A configuration option and environment variable t specify a list of paths where to search for roles will probably be needed someday.

I am wondering if it is possible to create a generic path lookup function that could be used all over the place and always behave in the expected way/order. A function that would use the same approach everywhere:

  • locate the role: first check if absolute path, than check in “roles” dir relative to playbook (also recurse through all parents), check in “roles” dir relative to inventory file, check in “roles” dir relative to all paths in configuration option or environment variable, as last resort check in “roles” dir relative to current dir, than repeat everything without “roles” dir

  • same could be used for locating the module library and similar (replace “roles” with “library” in above)

  • same could also be used for locating group_vars/host_vars, although only relative to main playbook or inventory file should be encouraged

  • locate templates in template module, files in file module or file lookup plugin: first check if absolute path, than if lookup plugin used inside a template check relative to the template (?), than if inside a role check relative to its “templates”/“files” folder, than check in “templates”/“files” folder next to playbooks (recurse parents), check in “templates”/“files” folder next to the inventory file, check in “templates”/“files” relative to all paths in configuration option or environment variable, as last resort check in “templates”/“files” dir relative to current dir

To me it seems that using the same approach everywhere would be possible, or not?

Greetings,
gw

just path the role where it lies.

also sprach Michael DeHaan <michael@ansibleworks.com> [2013.06.22.1928 +0200]:

just path the role where it lies.

If you are suggesting absolute paths, then how do you propose to do
team maintenance, where user 'bob' checks out to /home/bob/ansible
and user 'alice' has her stuff in
/srv/users/a/alice/sysadmin/ansible/site1 ?

I am not suggesting absolute paths.