def _load_tasks(self, tasks, vars={}, default_vars={}, sudo_vars={}, additional_conditions=[], original_file=None): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] for x in tasks: if not isinstance(x, dict): 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 x['meta'] == 'flush_handlers': results.append(Task(self,x)) continue task_vars = self.vars.copy() task_vars.update(vars) if original_file: task_vars['_original_file'] = original_file if 'include' in x: tokens = shlex.split(str(x['include'])) items = [''] included_additional_conditions = list(additional_conditions) include_vars = {} for k in x: if k.startswith("with_"): plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = template(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.insert(0, utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k == 'when': included_additional_conditions.insert(0, utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) elif k in ("include", "vars", "default_vars", "only_if", "sudo", "sudo_user"): continue else: include_vars[k] = x[k]
def _load_tasks(self, tasks, vars={}, additional_conditions=[]): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] for x in tasks: task_vars = self.vars.copy() task_vars.update(vars) if 'include' in x: tokens = shlex.split(x['include']) items = [''] included_additional_conditions = list(additional_conditions) for k in x: if k.startswith("with_"): plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = utils.template_ds(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.append(utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k in ("include", "vars", "only_if"): pass else: raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k) if 'vars' in x: task_vars.update(x['vars']) if 'only_if' in x: included_additional_conditions.append(x['only_if']) for item in items: mv = task_vars.copy() mv['item'] = item for t in tokens[1:]: (k,v) = t.split("=", 1) mv[k] = utils.template_ds(self.basedir, v, mv) include_file = utils.template(self.basedir, tokens[0], mv) data = utils.parse_yaml_from_file(utils.path_dwim(self.basedir, include_file)) results += self._load_tasks(data, mv, included_additional_conditions) elif type(x) == dict: results.append(Task(self,x,module_vars=task_vars, additional_conditions=additional_conditions)) else: raise Exception("unexpected task type") for x in results: if self.tags is not None: x.tags.extend(self.tags) return results
def _load_tasks(self, tasks, vars=None, default_vars=None, sudo_vars=None, additional_conditions=None, original_file=None, role_name=None): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] if additional_conditions is None: additional_conditions = [] if vars is None: vars = {} if default_vars is None: default_vars = {} if sudo_vars is None: sudo_vars = {} old_conditions = list(additional_conditions) for x in tasks: # prevent assigning the same conditions to each task on an include included_additional_conditions = list(old_conditions) if not isinstance(x, dict): 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 x['meta'] == 'flush_handlers': results.append(Task(self,x)) continue task_vars = self.vars.copy() task_vars.update(vars) if original_file: task_vars['_original_file'] = original_file if 'include' in x: tokens = shlex.split(str(x['include'])) items = [''] included_additional_conditions = list(additional_conditions) include_vars = {} for k in x: if k.startswith("with_"): utils.deprecated("include + with_items is an unsupported feature and has been undocumented for many releases because of this", "1.5") plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = template(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.insert(0, utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k == 'when': if type(x[k]) is str: included_additional_conditions.insert(0, utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) elif type(x[k]) is list: for i in x[k]: included_additional_conditions.insert(0, utils.compile_when_to_only_if("jinja2_compare %s" % i)) elif k in ("include", "vars", "default_vars", "only_if", "sudo", "sudo_user", "role_name"): continue else: include_vars[k] = x[k] default_vars = x.get('default_vars', {}) if not default_vars: default_vars = self.default_vars else: default_vars = utils.combine_vars(self.default_vars, default_vars) # append the vars defined with the include (from above) # as well as the old-style 'vars' element. The old-style # vars are given higher precedence here (just in case) task_vars = utils.combine_vars(task_vars, include_vars) if 'vars' in x: task_vars = utils.combine_vars(task_vars, x['vars']) if 'only_if' in x: included_additional_conditions.append(x['only_if']) new_role = None if 'role_name' in x: new_role = x['role_name'] for item in items: mv = task_vars.copy() mv['item'] = item for t in tokens[1:]: (k,v) = t.split("=", 1) mv[k] = template(self.basedir, v, mv) dirname = self.basedir if original_file: dirname = os.path.dirname(original_file) include_file = template(dirname, tokens[0], mv) include_filename = utils.path_dwim(dirname, include_file) data = utils.parse_yaml_from_file(include_filename) if 'role_name' in x and data is not None: for x in data: if 'include' in x: x['role_name'] = new_role loaded = self._load_tasks(data, mv, default_vars, included_sudo_vars, list(included_additional_conditions), original_file=include_filename, role_name=new_role) results += loaded elif type(x) == dict: task = Task(self,x,module_vars=task_vars,default_vars=default_vars,additional_conditions=list(additional_conditions),role_name=role_name) results.append(task) else: raise Exception("unexpected task type") for x in results: if self.tags is not None: x.tags.extend(self.tags) return results
def _load_tasks(self, tasks, vars={}, additional_conditions=[], original_file=None): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] for x in tasks: if not isinstance(x, dict): raise errors.AnsibleError("expecting dict; got: %s" % x) if 'meta' in x: if x['meta'] == 'flush_handlers': results.append(Task(self, x)) continue task_vars = self.vars.copy() task_vars.update(vars) if original_file: task_vars['_original_file'] = original_file if 'include' in x: tokens = shlex.split(str(x['include'])) items = [''] included_additional_conditions = list(additional_conditions) for k in x: if k.startswith("with_"): plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError( "cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = template(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get( plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.append( utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k == 'when': included_additional_conditions.append( utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) elif k in ("include", "vars", "only_if"): pass else: raise errors.AnsibleError( "parse error: task includes cannot be used with other directives: %s" % k) if 'vars' in x: task_vars.update(x['vars']) if 'only_if' in x: included_additional_conditions.append(x['only_if']) for item in items: mv = task_vars.copy() mv['item'] = item for t in tokens[1:]: (k, v) = t.split("=", 1) mv[k] = template(self.basedir, v, mv) dirname = self.basedir if original_file: dirname = os.path.dirname(original_file) include_file = template(dirname, tokens[0], mv) include_filename = utils.path_dwim(dirname, include_file) data = utils.parse_yaml_from_file(include_filename) results += self._load_tasks(data, mv, included_additional_conditions, original_file=include_filename) elif type(x) == dict: results.append( Task(self, x, module_vars=task_vars, additional_conditions=additional_conditions)) else: raise Exception("unexpected task type") for x in results: if self.tags is not None: x.tags.extend(self.tags) return results
def __init__(self, play, ds, module_vars=None, additional_conditions=None): """ constructor loads from a task or handler datastructure """ for x in ds.keys(): # code to allow for saying "modulename: args" versus "action: modulename args" if x in utils.plugins.module_finder: if "action" in ds: raise errors.AnsibleError("multiple actions specified in task %s" % (ds.get("name", ds["action"]))) ds["action"] = x + " " + ds[x] ds.pop(x) # code to allow "with_glob" and to reference a lookup plugin named glob elif x.startswith("with_"): plugin_name = x.replace("with_", "") if plugin_name in utils.plugins.lookup_loader: ds["items_lookup_plugin"] = plugin_name ds["items_lookup_terms"] = ds[x] ds.pop(x) else: raise errors.AnsibleError( "cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name) ) elif x.startswith("when_"): when_name = x.replace("when_", "") ds["when"] = "%s %s" % (when_name, ds[x]) ds.pop(x) elif not x in Task.VALID_KEYS: raise errors.AnsibleError("%s is not a legal parameter in an Ansible task or handler" % x) self.module_vars = module_vars self.play = play # load various attributes self.name = ds.get("name", None) self.tags = ["all"] self.register = ds.get("register", None) self.sudo = utils.boolean(ds.get("sudo", play.sudo)) if self.sudo: self.sudo_user = ds.get("sudo_user", play.sudo_user) self.sudo_pass = ds.get("sudo_pass", play.playbook.sudo_pass) else: self.sudo_user = None self.sudo_pass = None # Both are defined if ("action" in ds) and ("local_action" in ds): raise errors.AnsibleError("the 'action' and 'local_action' attributes can not be used together") # Both are NOT defined elif (not "action" in ds) and (not "local_action" in ds): raise errors.AnsibleError( "'action' or 'local_action' attribute missing in task \"%s\"" % ds.get("name", "<Unnamed>") ) # Only one of them is defined elif "local_action" in ds: self.action = ds.get("local_action", "") self.delegate_to = "127.0.0.1" else: self.action = ds.get("action", "") self.delegate_to = ds.get("delegate_to", None) self.transport = ds.get("connection", ds.get("transport", play.transport)) # delegate_to can use variables if not (self.delegate_to is None): self.delegate_to = utils.template(None, self.delegate_to, self.module_vars) # delegate_to: localhost should use local transport if self.delegate_to in ["127.0.0.1", "localhost"]: self.transport = "local" # notified by is used by Playbook code to flag which hosts # need to run a notifier self.notified_by = [] # if no name is specified, use the action line as the name if self.name is None: self.name = self.action # load various attributes self.only_if = ds.get("only_if", "True") self.when = ds.get("when", None) self.async_seconds = int(ds.get("async", 0)) # not async by default self.async_poll_interval = int(ds.get("poll", 10)) # default poll = 10 seconds self.notify = ds.get("notify", []) self.first_available_file = ds.get("first_available_file", None) self.items_lookup_plugin = ds.get("items_lookup_plugin", None) self.items_lookup_terms = ds.get("items_lookup_terms", None) self.ignore_errors = ds.get("ignore_errors", False) # action should be a string if not isinstance(self.action, basestring): raise errors.AnsibleError( "action is of type '%s' and not a string in task. name: %s" % (type(self.action).__name__, self.name) ) # notify can be a string or a list, store as a list if isinstance(self.notify, basestring): self.notify = [self.notify] # split the action line into a module name + arguments tokens = self.action.split(None, 1) if len(tokens) < 1: raise errors.AnsibleError("invalid/missing action in task. name: %s" % self.name) self.module_name = tokens[0] self.module_args = "" if len(tokens) > 1: self.module_args = tokens[1] import_tags = self.module_vars.get("tags", []) if type(import_tags) in [str, unicode]: # allow the user to list comma delimited tags import_tags = import_tags.split(",") # handle mutually incompatible options incompatibles = [x for x in [self.first_available_file, self.items_lookup_plugin] if x is not None] if len(incompatibles) > 1: raise errors.AnsibleError( "with_(plugin), and first_available_file are mutually incompatible in a single task" ) # make first_available_file accessable to Runner code if self.first_available_file: self.module_vars["first_available_file"] = self.first_available_file if self.items_lookup_plugin is not None: self.module_vars["items_lookup_plugin"] = self.items_lookup_plugin self.module_vars["items_lookup_terms"] = self.items_lookup_terms # allow runner to see delegate_to option self.module_vars["delegate_to"] = self.delegate_to # make ignore_errors accessable to Runner code self.module_vars["ignore_errors"] = self.ignore_errors # tags allow certain parts of a playbook to be run without running the whole playbook apply_tags = ds.get("tags", None) if apply_tags is not None: if type(apply_tags) in [str, unicode]: self.tags.append(apply_tags) elif type(apply_tags) == list: self.tags.extend(apply_tags) self.tags.extend(import_tags) if self.when is not None: if self.only_if != "True": raise errors.AnsibleError("when obsoletes only_if, only use one or the other") self.only_if = utils.compile_when_to_only_if(self.when) if additional_conditions: self.only_if = "(" + self.only_if + ") and (" + " ) and (".join(additional_conditions) + ")"
def __init__(self, play, ds, module_vars=None, additional_conditions=None): ''' constructor loads from a task or handler datastructure ''' # meta directives are used to tell things like ansible/playbook to run # operations like handler execution. Meta tasks are not executed # normally. if 'meta' in ds: self.meta = ds['meta'] self.tags = [] return else: self.meta = None library = os.path.join(play.basedir, 'library') if os.path.exists(library): utils.plugins.module_finder.add_directory(library) for x in ds.keys(): # code to allow for saying "modulename: args" versus "action: modulename args" if x in utils.plugins.module_finder: if 'action' in ds: raise errors.AnsibleError( "multiple actions specified in task %s" % (ds.get('name', ds['action']))) if isinstance(ds[x], dict): if 'args' in ds: raise errors.AnsibleError( "can't combine args: and a dict for %s: in task %s" % (x, ds.get('name', "%s: %s" % (x, ds[x])))) ds['args'] = ds[x] ds[x] = '' elif ds[x] is None: ds[x] = '' if not isinstance(ds[x], basestring): raise errors.AnsibleError( "action specified for task %s has invalid type %s" % (ds.get('name', "%s: %s" % (x, ds[x])), type(ds[x]))) ds['action'] = x + " " + ds[x] ds.pop(x) # code to allow "with_glob" and to reference a lookup plugin named glob elif x.startswith("with_"): plugin_name = x.replace("with_", "") if plugin_name in utils.plugins.lookup_loader: ds['items_lookup_plugin'] = plugin_name ds['items_lookup_terms'] = ds[x] ds.pop(x) else: raise errors.AnsibleError( "cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) elif x == 'when': ds['when'] = "jinja2_compare %s" % (ds[x]) elif x.startswith("when_"): if 'when' in ds: raise errors.AnsibleError( "multiple when_* statements specified in task %s" % (ds.get('name', ds['action']))) when_name = x.replace("when_", "") ds['when'] = "%s %s" % (when_name, ds[x]) ds.pop(x) elif not x in Task.VALID_KEYS: raise errors.AnsibleError( "%s is not a legal parameter in an Ansible task or handler" % x) self.module_vars = module_vars self.play = play # load various attributes self.name = ds.get('name', None) self.tags = ['all'] self.register = ds.get('register', None) self.sudo = utils.boolean(ds.get('sudo', play.sudo)) self.environment = ds.get('environment', {}) # rather than simple key=value args on the options line, these represent structured data and the values # can be hashes and lists, not just scalars self.args = ds.get('args', {}) if self.sudo: self.sudo_user = ds.get('sudo_user', play.sudo_user) self.sudo_pass = ds.get('sudo_pass', play.playbook.sudo_pass) else: self.sudo_user = None self.sudo_pass = None # Both are defined if ('action' in ds) and ('local_action' in ds): raise errors.AnsibleError( "the 'action' and 'local_action' attributes can not be used together" ) # Both are NOT defined elif (not 'action' in ds) and (not 'local_action' in ds): raise errors.AnsibleError( "'action' or 'local_action' attribute missing in task \"%s\"" % ds.get('name', '<Unnamed>')) # Only one of them is defined elif 'local_action' in ds: self.action = ds.get('local_action', '') self.delegate_to = '127.0.0.1' else: self.action = ds.get('action', '') self.delegate_to = ds.get('delegate_to', None) self.transport = ds.get('connection', ds.get('transport', play.transport)) if isinstance(self.action, dict): if 'module' not in self.action: raise errors.AnsibleError( "'module' attribute missing from action in task \"%s\"" % ds.get('name', '%s' % self.action)) if self.args: raise errors.AnsibleError( "'args' cannot be combined with dict 'action' in task \"%s\"" % ds.get('name', '%s' % self.action)) self.args = self.action self.action = self.args.pop('module') # delegate_to can use variables if not (self.delegate_to is None): # delegate_to: localhost should use local transport if self.delegate_to in ['127.0.0.1', 'localhost']: self.transport = 'local' # notified by is used by Playbook code to flag which hosts # need to run a notifier self.notified_by = [] # if no name is specified, use the action line as the name if self.name is None: self.name = self.action # load various attributes self.only_if = ds.get('only_if', 'True') self.when = ds.get('when', None) self.async_seconds = int(ds.get('async', 0)) # not async by default self.async_poll_interval = int(ds.get('poll', 10)) # default poll = 10 seconds self.notify = ds.get('notify', []) self.first_available_file = ds.get('first_available_file', None) self.items_lookup_plugin = ds.get('items_lookup_plugin', None) self.items_lookup_terms = ds.get('items_lookup_terms', None) self.ignore_errors = ds.get('ignore_errors', False) self.any_errors_fatal = ds.get('any_errors_fatal', play.any_errors_fatal) # action should be a string if not isinstance(self.action, basestring): raise errors.AnsibleError( "action is of type '%s' and not a string in task. name: %s" % (type(self.action).__name__, self.name)) # notify can be a string or a list, store as a list if isinstance(self.notify, basestring): self.notify = [self.notify] # split the action line into a module name + arguments tokens = self.action.split(None, 1) if len(tokens) < 1: raise errors.AnsibleError( "invalid/missing action in task. name: %s" % self.name) self.module_name = tokens[0] self.module_args = '' if len(tokens) > 1: self.module_args = tokens[1] import_tags = self.module_vars.get('tags', []) if type(import_tags) in [str, unicode]: # allow the user to list comma delimited tags import_tags = import_tags.split(",") # handle mutually incompatible options incompatibles = [ x for x in [self.first_available_file, self.items_lookup_plugin] if x is not None ] if len(incompatibles) > 1: raise errors.AnsibleError( "with_(plugin), and first_available_file are mutually incompatible in a single task" ) # make first_available_file accessable to Runner code if self.first_available_file: self.module_vars[ 'first_available_file'] = self.first_available_file if self.items_lookup_plugin is not None: self.module_vars['items_lookup_plugin'] = self.items_lookup_plugin self.module_vars['items_lookup_terms'] = self.items_lookup_terms # allow runner to see delegate_to option self.module_vars['delegate_to'] = self.delegate_to # make ignore_errors accessable to Runner code self.module_vars['ignore_errors'] = self.ignore_errors # tags allow certain parts of a playbook to be run without running the whole playbook apply_tags = ds.get('tags', None) if apply_tags is not None: if type(apply_tags) in [str, unicode]: self.tags.append(apply_tags) elif type(apply_tags) == list: self.tags.extend(apply_tags) self.tags.extend(import_tags) if self.when is not None: if self.only_if != 'True': raise errors.AnsibleError( 'when obsoletes only_if, only use one or the other') self.only_if = utils.compile_when_to_only_if(self.when) if additional_conditions: self.only_if = [self.only_if] self.only_if.extend(additional_conditions)
def _load_tasks(self, tasks, vars={}, default_vars={}, sudo_vars={}, additional_conditions=[], original_file=None): ''' handle task and handler include statements ''' results = [] if tasks is None: # support empty handler files, and the like. tasks = [] for x in tasks: if not isinstance(x, dict): 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 x['meta'] == 'flush_handlers': results.append(Task(self,x)) continue task_vars = self.vars.copy() task_vars.update(vars) if original_file: task_vars['_original_file'] = original_file if 'include' in x: tokens = shlex.split(str(x['include'])) items = [''] included_additional_conditions = list(additional_conditions) for k in x: if k.startswith("with_"): plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = template(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.insert(0, utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k == 'when': included_additional_conditions.insert(0, utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) elif k in ("include", "vars", "default_vars", "only_if", "sudo", "sudo_user"): pass else: raise errors.AnsibleError("parse error: task includes cannot be used with other directives: %s" % k) default_vars = utils.combine_vars(self.default_vars, x.get('default_vars', {})) if 'vars' in x: task_vars = utils.combine_vars(task_vars, x['vars']) if 'only_if' in x: included_additional_conditions.append(x['only_if']) for item in items: mv = task_vars.copy() mv['item'] = item for t in tokens[1:]: (k,v) = t.split("=", 1) mv[k] = template(self.basedir, v, mv) dirname = self.basedir if original_file: dirname = os.path.dirname(original_file) include_file = template(dirname, tokens[0], mv) include_filename = utils.path_dwim(dirname, include_file) data = utils.parse_yaml_from_file(include_filename) results += self._load_tasks(data, mv, default_vars, included_sudo_vars, included_additional_conditions, original_file=include_filename) elif type(x) == dict: results.append(Task(self,x,module_vars=task_vars,default_vars=default_vars,additional_conditions=additional_conditions)) else: raise Exception("unexpected task type") for x in results: if self.tags is not None: x.tags.extend(self.tags) return results
def __init__(self, play, ds, module_vars=None, additional_conditions=None): ''' constructor loads from a task or handler datastructure ''' # meta directives are used to tell things like ansible/playbook to run # operations like handler execution. Meta tasks are not executed # normally. if 'meta' in ds: self.meta = ds['meta'] self.tags = [] return else: self.meta = None library = os.path.join(play.basedir, 'library') if os.path.exists(library): utils.plugins.module_finder.add_directory(library) for x in ds.keys(): # code to allow for saying "modulename: args" versus "action: modulename args" if x in utils.plugins.module_finder: if 'action' in ds: raise errors.AnsibleError("multiple actions specified in task %s" % (ds.get('name', ds['action']))) if isinstance(ds[x], dict): if 'args' in ds: raise errors.AnsibleError("can't combine args: and a dict for %s: in task %s" % (x, ds.get('name', "%s: %s" % (x, ds[x])))) ds['args'] = ds[x] ds[x] = '' elif ds[x] is None: ds[x] = '' if not isinstance(ds[x], basestring): raise errors.AnsibleError("action specified for task %s has invalid type %s" % (ds.get('name', "%s: %s" % (x, ds[x])), type(ds[x]))) ds['action'] = x + " " + ds[x] ds.pop(x) # code to allow "with_glob" and to reference a lookup plugin named glob elif x.startswith("with_"): plugin_name = x.replace("with_","") if plugin_name in utils.plugins.lookup_loader: ds['items_lookup_plugin'] = plugin_name ds['items_lookup_terms'] = ds[x] ds.pop(x) else: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) elif x in [ 'changed_when', 'when']: ds[x] = "jinja2_compare %s" % (ds[x]) elif x.startswith("when_"): if 'when' in ds: raise errors.AnsibleError("multiple when_* statements specified in task %s" % (ds.get('name', ds['action']))) when_name = x.replace("when_","") ds['when'] = "%s %s" % (when_name, ds[x]) ds.pop(x) elif not x in Task.VALID_KEYS: raise errors.AnsibleError("%s is not a legal parameter in an Ansible task or handler" % x) self.module_vars = module_vars self.play = play # load various attributes self.name = ds.get('name', None) self.tags = [ 'all' ] self.register = ds.get('register', None) self.sudo = utils.boolean(ds.get('sudo', play.sudo)) self.environment = ds.get('environment', {}) # rather than simple key=value args on the options line, these represent structured data and the values # can be hashes and lists, not just scalars self.args = ds.get('args', {}) if self.sudo: self.sudo_user = ds.get('sudo_user', play.sudo_user) self.sudo_pass = ds.get('sudo_pass', play.playbook.sudo_pass) else: self.sudo_user = None self.sudo_pass = None # Both are defined if ('action' in ds) and ('local_action' in ds): raise errors.AnsibleError("the 'action' and 'local_action' attributes can not be used together") # Both are NOT defined elif (not 'action' in ds) and (not 'local_action' in ds): raise errors.AnsibleError("'action' or 'local_action' attribute missing in task \"%s\"" % ds.get('name', '<Unnamed>')) # Only one of them is defined elif 'local_action' in ds: self.action = ds.get('local_action', '') self.delegate_to = '127.0.0.1' else: self.action = ds.get('action', '') self.delegate_to = ds.get('delegate_to', None) self.transport = ds.get('connection', ds.get('transport', play.transport)) if isinstance(self.action, dict): if 'module' not in self.action: raise errors.AnsibleError("'module' attribute missing from action in task \"%s\"" % ds.get('name', '%s' % self.action)) if self.args: raise errors.AnsibleError("'args' cannot be combined with dict 'action' in task \"%s\"" % ds.get('name', '%s' % self.action)) self.args = self.action self.action = self.args.pop('module') # delegate_to can use variables if not (self.delegate_to is None): # delegate_to: localhost should use local transport if self.delegate_to in ['127.0.0.1', 'localhost']: self.transport = 'local' # notified by is used by Playbook code to flag which hosts # need to run a notifier self.notified_by = [] # if no name is specified, use the action line as the name if self.name is None: self.name = self.action # load various attributes self.only_if = ds.get('only_if', 'True') self.when = ds.get('when', None) self.changed_when = ds.get('changed_when', None) if self.changed_when is not None: self.changed_when = utils.compile_when_to_only_if(self.changed_when) self.async_seconds = int(ds.get('async', 0)) # not async by default self.async_poll_interval = int(ds.get('poll', 10)) # default poll = 10 seconds self.notify = ds.get('notify', []) self.first_available_file = ds.get('first_available_file', None) self.items_lookup_plugin = ds.get('items_lookup_plugin', None) self.items_lookup_terms = ds.get('items_lookup_terms', None) self.ignore_errors = ds.get('ignore_errors', False) self.any_errors_fatal = ds.get('any_errors_fatal', play.any_errors_fatal) self.always_run = ds.get('always_run', False) # action should be a string if not isinstance(self.action, basestring): raise errors.AnsibleError("action is of type '%s' and not a string in task. name: %s" % (type(self.action).__name__, self.name)) # notify can be a string or a list, store as a list if isinstance(self.notify, basestring): self.notify = [ self.notify ] # split the action line into a module name + arguments tokens = self.action.split(None, 1) if len(tokens) < 1: raise errors.AnsibleError("invalid/missing action in task. name: %s" % self.name) self.module_name = tokens[0] self.module_args = '' if len(tokens) > 1: self.module_args = tokens[1] import_tags = self.module_vars.get('tags',[]) if type(import_tags) in [str,unicode]: # allow the user to list comma delimited tags import_tags = import_tags.split(",") # handle mutually incompatible options incompatibles = [ x for x in [ self.first_available_file, self.items_lookup_plugin ] if x is not None ] if len(incompatibles) > 1: raise errors.AnsibleError("with_(plugin), and first_available_file are mutually incompatible in a single task") # make first_available_file accessable to Runner code if self.first_available_file: self.module_vars['first_available_file'] = self.first_available_file if self.items_lookup_plugin is not None: self.module_vars['items_lookup_plugin'] = self.items_lookup_plugin self.module_vars['items_lookup_terms'] = self.items_lookup_terms # allow runner to see delegate_to option self.module_vars['delegate_to'] = self.delegate_to # make some task attributes accessible to Runner code self.module_vars['ignore_errors'] = self.ignore_errors self.module_vars['register'] = self.register self.module_vars['changed_when'] = self.changed_when self.module_vars['always_run'] = self.always_run # tags allow certain parts of a playbook to be run without running the whole playbook apply_tags = ds.get('tags', None) if apply_tags is not None: if type(apply_tags) in [ str, unicode ]: self.tags.append(apply_tags) elif type(apply_tags) == list: self.tags.extend(apply_tags) self.tags.extend(import_tags) if self.when is not None: if self.only_if != 'True': raise errors.AnsibleError('when obsoletes only_if, only use one or the other') self.only_if = utils.compile_when_to_only_if(self.when) if additional_conditions: self.only_if = [ self.only_if ] self.only_if.extend(additional_conditions)
======= >>>>>>> LOCAL # append the vars defined with the include (from above) # as well as the old-style 'vars' element. The old-style # vars are given higher precedence here (just in case) task_vars = utils.combine_vars(task_vars, include_vars) for k in x: if k.startswith("with_"): plugin_name = k[5:] if plugin_name not in utils.plugins.lookup_loader: raise errors.AnsibleError("cannot find lookup plugin named %s for usage in with_%s" % (plugin_name, plugin_name)) terms = template(self.basedir, x[k], task_vars) items = utils.plugins.lookup_loader.get(plugin_name, basedir=self.basedir, runner=None).run(terms, inject=task_vars) elif k.startswith("when_"): included_additional_conditions.insert(0, utils.compile_when_to_only_if("%s %s" % (k[5:], x[k]))) elif k == 'when': included_additional_conditions.insert(0, utils.compile_when_to_only_if("jinja2_compare %s" % x[k])) elif k in ("include", "vars", "default_vars", "only_if", "sudo", "sudo_user"): continue else: include_vars[k] = x[k] if 'vars' in x: task_vars = utils.combine_vars(task_vars, x['vars']) if 'only_if' in x: included_additional_conditions.append(x['only_if']) for item in items: mv = task_vars.copy()