From 632232259aef6b492829122e1ef1071b858a70ea Mon Sep 17 00:00:00 2001 From: Thomas Omans Date: Fri, 27 Sep 2013 10:03:28 -0700 Subject: [PATCH] Adding config flag role_path for common/global roles Using ANSIBLE_ROLE_PATH environment variable or role_path in ansible.cfg can configure paths where roles will be searched for extra paths will only be used as a backup once regular locations are exhausted --- examples/ansible.cfg | 1 + lib/ansible/constants.py | 1 + lib/ansible/playbook/play.py | 30 +++++++++++++++++++++--------- 3 files changed, 23 insertions(+), 9 deletions(-) diff --git a/examples/ansible.cfg b/examples/ansible.cfg index 25b58dc1da..acb8e52e90 100644 --- a/examples/ansible.cfg +++ b/examples/ansible.cfg @@ -12,6 +12,7 @@ hostfile = /etc/ansible/hosts library = /usr/share/ansible +#roles_path = /etc/ansible/roles remote_tmp = $HOME/.ansible/tmp pattern = * forks = 5 diff --git a/lib/ansible/constants.py b/lib/ansible/constants.py index b84bb0777b..a6b77050ca 100644 --- a/lib/ansible/constants.py +++ b/lib/ansible/constants.py @@ -95,6 +95,7 @@ DEFAULTS='defaults' # configurable things DEFAULT_HOST_LIST = shell_expand_path(get_config(p, DEFAULTS, 'hostfile', 'ANSIBLE_HOSTS', '/etc/ansible/hosts')) DEFAULT_MODULE_PATH = get_config(p, DEFAULTS, 'library', 'ANSIBLE_LIBRARY', DIST_MODULE_PATH) +DEFAULT_ROLES_PATH = get_config(p, DEFAULTS, 'roles_path', 'ANSIBLE_ROLES_PATH', None) DEFAULT_REMOTE_TMP = shell_expand_path(get_config(p, DEFAULTS, 'remote_tmp', 'ANSIBLE_REMOTE_TEMP', '$HOME/.ansible/tmp')) DEFAULT_MODULE_NAME = get_config(p, DEFAULTS, 'module_name', None, 'command') DEFAULT_PATTERN = get_config(p, DEFAULTS, 'pattern', None, '*') diff --git a/lib/ansible/playbook/play.py b/lib/ansible/playbook/play.py index fb5338bc5d..f965770c90 100644 --- a/lib/ansible/playbook/play.py +++ b/lib/ansible/playbook/play.py @@ -21,6 +21,7 @@ from ansible.utils.template import template from ansible import utils from ansible import errors from ansible.playbook.task import Task +import ansible.constants as C import pipes import shlex import os @@ -148,16 +149,27 @@ class Play(object): role_vars = orig_path orig_path = role_name - path = utils.path_dwim(self.basedir, os.path.join('roles', orig_path)) - if not os.path.isdir(path) and not orig_path.startswith(".") and not orig_path.startswith("/"): - path2 = utils.path_dwim(self.basedir, orig_path) - if not os.path.isdir(path2): - raise errors.AnsibleError("cannot find role in %s or %s" % (path, path2)) - path = path2 - elif not os.path.isdir(path): - raise errors.AnsibleError("cannot find role in %s" % (path)) + role_path = None - return (path, role_vars) + possible_paths = [ + utils.path_dwim(self.basedir, os.path.join('roles', orig_path)), + utils.path_dwim(self.basedir, orig_path) + ] + + if C.DEFAULT_ROLES_PATH: + search_locations = C.DEFAULT_ROLES_PATH.split(os.pathsep) + for loc in search_locations: + possible_paths.append(utils.path_dwim(loc, orig_path)) + + for path_option in possible_paths: + if os.path.isdir(path_option): + role_path = path_option + break + + if role_path is None: + raise errors.AnsibleError("cannot find role in %s" % " or ".join(possible_paths)) + + return (role_path, role_vars) def _build_role_dependencies(self, roles, dep_stack, passed_vars={}, level=0): # this number is arbitrary, but it seems sane