DEFAULT_ROLES_PATH
is defined in Ansible's constants.py
file:
DEFAULT_ROLES_PATH = shell_expand_path(get_config(p, DEFAULTS, 'roles_path', 'ANSIBLE_ROLES_PATH', '/etc/ansible/roles'))
By importing it from ansible.constant import DEFAULT_ROLES_PATH
your module will follow any path changes that might occur in the Ansible project.
utils.py
will likely be the next file you should become familiar with when writing modules as it contains the path-lookup functions (see path_dwim
) and other Ansible-specific interaction functions.
If you want a plugin to work like the copy module, you can see the logic for the file pathing in the runner's __init__.py
:
if items_plugin is not None and items_plugin in utils.plugins.lookup_loader:
basedir = self.basedir
if '_original_file' in inject:
basedir = os.path.dirname(inject['_original_file'])
filesdir = os.path.join(basedir, '..', 'files')
if os.path.exists(filesdir):
basedir = filesdir
where self.basedir is defined as a utils.default(basedir, lambda: os.getcwd())
, where basedir is optionally the playbook directory.
By providing the proper argument spec to your module, you should be able to automagically take advantage of these underlying details.
At the bottom of your module, include the basic module_utils to hook into methods expected to exist ('fail_json', etc) and populate other defaults for you:
from ansible.module_utils.basic import *
I believe that with these portions in place, you'll trigger the file search path, but please tell me if I'm incorrect.