def do_lines_template(self, template, join=False, when=None, required=False): templated_lines = list() _processed = list() if when is not None: if not self._check_conditional(when, self.ds): display.vvv("skipping due to conditional failure") return templated_lines for line in to_list(template): res = self.template(line, self.ds) if res: _processed.append(res) elif not res and join: break if required and not _processed: raise AnsibleError('unabled to templated required line') elif _processed and join: templated_lines.append(' '.join(_processed)) elif _processed: templated_lines.extend(_processed) return templated_lines
def do_context(self, block): results = list() for entry in block: task = entry.copy() name = task.pop('name', None) register = task.pop('register', None) when = task.pop('when', None) if when is not None: if not self._check_conditional(when, self.ds): display.vvv( 'skipping context due to conditional check failure') continue loop = task.pop('loop', None) if loop: loop = self.template(loop, self.ds) if 'context' in task: res = self.do_context(task['context']) if res: results.extend(res) elif isinstance(loop, collections.Mapping): loop_result = list() for loop_key, loop_value in iteritems(loop): self.ds['item'] = {'key': loop_key, 'value': loop_value} loop_result.extend(to_list(self._process_directive(task))) results.extend(loop_result) elif isinstance(loop, collections.Iterable) and not isinstance( loop, string_types): loop_result = list() for loop_item in loop: self.ds['item'] = loop_item loop_result.extend(to_list(self._process_directive(task))) results.extend(loop_result) else: res = self._process_directive(task) if res: results.extend(to_list(res)) return results
def run(self, terms, variables, **kwargs): self.ds = variables.copy() config_lines = list() for term in to_list(terms[0]): display.debug("File lookup term: %s" % term) lookupfile = self.find_file_in_search_path(variables, 'templates', term) display.vvvv("File lookup using %s as file" % lookupfile) if lookupfile: with open(to_bytes(lookupfile, errors='surrogate_or_strict'), 'rb'): tasks = self._loader.load_from_file(lookupfile) for task in tasks: task.pop('name', None) register = task.pop('register', None) when = task.pop('when', None) if when is not None: if not self._check_conditional(when, self.ds): display.vvv('skipping task due to conditional check failure') continue loop = task.pop('loop', None) if loop: loop = self.template(loop, self.ds) loop_result = list() if isinstance(loop, Mapping): for loop_key, loop_value in iteritems(loop): self.ds['item'] = {'key': loop_key, 'value': loop_value} res = self._process_directive(task) if res: loop_result.extend(to_list(res)) elif isinstance(loop, collections.Iterable) and not isinstance(loop, string_types): for loop_item in loop: self.ds['item'] = loop_item res = self._process_directive(task) if res: loop_result.extend(to_list(res)) config_lines.extend(loop_result) if register: self.ds[register] = loop_result else: res = self._process_directive(task) if res: config_lines.extend(to_list(res)) if register: self.ds[register] = res else: raise AnsibleError("the template file %s could not be found for the lookup" % term) return [to_text('\n'.join(config_lines)).strip()]