Playbook relative role_paths

Given an ansible structure below

├── ansible.cfg
├── deploy.yml
├── files/
├── foundation_roles/
│ ├── cleanup/
│ ├── common/
│ ├── deploy/
│ ├── retire/
│ └── setup/
├── group_vars/
├── roles/
│ └── service/

I would like to be able to refer to the roles in “foundation_roles” without having to use relative references everywhere. In particular, the roles in “foundation_roles” need to reference each other.

I believe one elegant solution would be to use the role_paths configuration setting - IF you could specify a path that was relative to the playbook_dir. However, as far as I can tell, these paths need to be absolute, or they are relative to the directory from which ansible was invoked.

Is this possible, and I’m just missing it?

We use relative paths:

roles_path = ./roles

They are relative to the directory in which you execute ansible-playbook.

The issue is that the relative path is relative to where the playbook was invoked, not relative to the playbook. That means that every invocation must be from the same relative depth - which is not necessarily the case when the roles are being shared by lots of people and automation. For example, we have build automation that tends to nest the ansible playbooks quite deeply.

For example the relative path in the roles_path directory would cause at least one of the following cases to fail:
1. Ansible-playbook /a/b/c/pb.yml
2. Ansible-playbook /x/pb.yml

where the actual directory names are irrelevant, only the depth matters.

I think the real question is in what cases would current ansible code break if the relative role_paths were defined to be relative to the playbook_dir?

The path should be relative to the config file (it has not always
been, but fixed in 2.4 to ensure this).

IF you want it otherwise you can use the {{CWD}} macro to change that behaviour.

roles/ is already looked at relative to the playbook path, but not
ROLES_PATH as that is a global setting.

Thanks, Brian. Let me make sure I understand.

“The path should be relative to the config file (it has not always been, but fixed in 2.4 to ensure this).”

Which path? And do you mean “ansible.cfg”. I’m currently running ansible 2.3.1, so that may be the source of my problems.

If, as of 2.4, the path from which relative paths in ROLES_PATH are calculated is the directory containing ansible.cfg, that will resolve my issue nicely.

any path defined inside the ansible.cfg shoudl be relative to the file.

I don’t see that paths inside ansible.cfg are relative to the config file itself.

The structure of my ansible files in /a/b/c/d/e/ansible is:

ansible.cfg
setup.yml
roles
foundation_roles
    setup
        main.yml

The “roles_path” is set to “./foundation_roles in ansible.cfg

Executing “ansible-playbook -vvv ansible/setup.yml” from the “/a/b/c/d/e” directory produces:

ansible-playbook 2.4.0.0
   config file = /a/b/c/d/e/ansible/ansible.cfg
   python version = 2.7.13 (default, Feb 2 2017, 13:41:18) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.42.1)]
Using /a/b/c/d/e/ansible/ansible.cfg as config file

ERROR! the role 'setup' was not found in /a/b/c/d/e/ansible/roles:/a/b/c/d/e/foundation_roles:/a/b/c/d/e/ansible

It appears that the paths are relative to the directory from which ansible was invoked. What am I missing?

You are correct, my bad, thought i had fixed that, in any case, this
PR should https://github.com/ansible/ansible/issues/31533