1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

Implement sudo/sudo_user at the include and role levels

This commit is contained in:
jeromew 2013-07-18 22:32:56 +00:00
parent c3a8b6ff07
commit de0bc96b70

View file

@ -159,8 +159,12 @@ class Play(object):
has_dict = orig_path has_dict = orig_path
orig_path = role_name orig_path = role_name
with_items = has_dict.get('with_items', None) # special vars must be extracted from the dict to the included tasks
when = has_dict.get('when', None) special_keys = [ "sudo", "sudo_user", "when", "with_items" ]
special_vars = {}
for k in special_keys:
if k in has_dict:
special_vars[k] = has_dict[k]
path = utils.path_dwim(self.basedir, os.path.join('roles', orig_path)) 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("/"): if not os.path.isdir(path) and not orig_path.startswith(".") and not orig_path.startswith("/"):
@ -181,17 +185,15 @@ class Play(object):
raise errors.AnsibleError("found role at %s, but cannot find %s or %s or %s or %s" % (path, task, handler, vars_file, library)) raise errors.AnsibleError("found role at %s, but cannot find %s or %s or %s or %s" % (path, task, handler, vars_file, library))
if os.path.isfile(task): if os.path.isfile(task):
nt = dict(include=task, vars=has_dict) nt = dict(include=task, vars=has_dict)
if when: for k in special_keys:
nt['when'] = when if k in special_vars:
if with_items: nt[k] = special_vars[k]
nt['with_items'] = with_items
new_tasks.append(nt) new_tasks.append(nt)
if os.path.isfile(handler): if os.path.isfile(handler):
nt = dict(include=handler, vars=has_dict) nt = dict(include=handler, vars=has_dict)
if when: for k in special_keys:
nt['when'] = when if k in special_vars:
if with_items: nt[k] = special_vars[k]
nt['with_items'] = with_items
new_handlers.append(nt) new_handlers.append(nt)
if os.path.isfile(vars_file): if os.path.isfile(vars_file):
new_vars_files.append(vars_file) new_vars_files.append(vars_file)
@ -247,7 +249,7 @@ class Play(object):
# ************************************************* # *************************************************
def _load_tasks(self, tasks, vars={}, additional_conditions=[], original_file=None): def _load_tasks(self, tasks, vars={}, sudo_vars={}, additional_conditions=[], original_file=None):
''' handle task and handler include statements ''' ''' handle task and handler include statements '''
results = [] results = []
@ -259,6 +261,15 @@ class Play(object):
if not isinstance(x, dict): if not isinstance(x, dict):
raise errors.AnsibleError("expecting dict; got: %s" % x) raise errors.AnsibleError("expecting dict; got: %s" % x)
# evaluate sudo vars for current and child tasks
included_sudo_vars = {}
for k in ["sudo", "sudo_user"]:
if k in x:
included_sudo_vars[k] = x[k]
elif k in sudo_vars:
included_sudo_vars[k] = sudo_vars[k]
x[k] = sudo_vars[k]
if 'meta' in x: if 'meta' in x:
if x['meta'] == 'flush_handlers': if x['meta'] == 'flush_handlers':
results.append(Task(self,x)) results.append(Task(self,x))
@ -284,7 +295,7 @@ class Play(object):
included_additional_conditions.append(utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) included_additional_conditions.append(utils.compile_when_to_only_if("%s %s" % (k[5:], x[k])))
elif k == 'when': elif k == 'when':
included_additional_conditions.append(utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) included_additional_conditions.append(utils.compile_when_to_only_if("jinja2_compare %s" % x[k]))
elif k in ("include", "vars", "only_if"): elif k in ("include", "vars", "only_if", "sudo", "sudo_user"):
pass pass
else: else:
raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k) raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k)
@ -306,7 +317,7 @@ class Play(object):
include_file = template(dirname, tokens[0], mv) include_file = template(dirname, tokens[0], mv)
include_filename = utils.path_dwim(dirname, include_file) include_filename = utils.path_dwim(dirname, include_file)
data = utils.parse_yaml_from_file(include_filename) data = utils.parse_yaml_from_file(include_filename)
results += self._load_tasks(data, mv, included_additional_conditions, original_file=include_filename) results += self._load_tasks(data, mv, included_sudo_vars, included_additional_conditions, original_file=include_filename)
elif type(x) == dict: elif type(x) == dict:
results.append(Task(self,x,module_vars=task_vars, additional_conditions=additional_conditions)) results.append(Task(self,x,module_vars=task_vars, additional_conditions=additional_conditions))
else: else: