def exec_module(self): if is_cli(self.module) and HAS_CLI_TRANSPORT: manager = self.get_manager('v1') else: manager = self.get_manager('v2') result = manager.exec_module() return result
def parse_commands(self, warnings): results = [] commands = list(deque(set(self.want.commands))) spec = dict( command=dict(key=True), output=dict(default='text', choices=['text', 'one-line']), ) transform = ComplexList(spec, self.module) commands = transform(commands) for index, item in enumerate(commands): if not self._is_valid_mode(item['command']) and is_cli( self.module): warnings.append( 'Using "write" commands is not idempotent. You should use ' 'a module that is specifically made for that. If such a ' 'module does not exist, then please file a bug. The command ' 'in question is "%s..."' % item['command'][0:40]) # This needs to be removed so that the ComplexList used in to_commands # will work correctly. output = item.pop('output', None) if output == 'one-line' and 'one-line' not in item['command']: item['command'] += ' one-line' elif output == 'text' and 'one-line' in item['command']: item['command'] = item['command'].replace('one-line', '') results.append(item) return results
def commands(self): commands = self._listify(self._values['commands']) commands = deque(commands) if not is_cli(self.module): commands.appendleft('tmsh modify cli preference pager disabled') commands = map(self._ensure_tmsh_prefix, list(commands)) return list(commands)
def exec_module(self): if is_cli(self.module) and HAS_CLI_TRANSPORT: manager = self.get_manager('v1') else: manager = self.get_manager('v2') result = manager.exec_module() return result
def main(): spec = ArgumentSpec() module = AnsibleModule(argument_spec=spec.argument_spec, supports_check_mode=spec.supports_check_mode) client = F5RestClient(**module.params) try: mm = ModuleManager(module=module, client=client) results = mm.exec_module() if not is_cli(module): cleanup_tokens(client) module.exit_json(**results) except F5ModuleError as e: if not is_cli(module): cleanup_tokens(client) module.fail_json(msg=str(e))
def _is_valid_mode(self, cmd): valid_configs = [ 'list', 'show', 'modify cli preference pager disabled' ] if not is_cli(self.module): valid_configs = list( map(self.want._ensure_tmsh_prefix, valid_configs)) if any(cmd.startswith(x) for x in valid_configs): return True return False
def main(): spec = ArgumentSpec() module = AnsibleModule(argument_spec=spec.argument_spec, supports_check_mode=spec.supports_check_mode) if is_cli(module) and not HAS_F5SDK: module.fail_json( msg="The python f5-sdk module is required to use the REST api") try: client = F5Client(**module.params) mm = ModuleManager(module=module, client=client) results = mm.exec_module() if not is_cli(module): cleanup_tokens(client) module.exit_json(**results) except F5ModuleError as e: if not is_cli(module): cleanup_tokens(client) module.fail_json(msg=str(e))
def main(): spec = ArgumentSpec() module = AnsibleModule( argument_spec=spec.argument_spec, supports_check_mode=spec.supports_check_mode ) if is_cli(module) and not HAS_F5SDK: module.fail_json(msg="The python f5-sdk module is required to use the REST api") try: client = F5Client(**module.params) mm = ModuleManager(module=module, client=client) results = mm.exec_module() if not is_cli(module): cleanup_tokens(client) module.exit_json(**results) except F5ModuleError as e: if not is_cli(module): cleanup_tokens(client) module.fail_json(msg=str(e))
def exec_module(self): if not is_cli(self.module): raise F5ModuleError('Module can only be run via SSH, set the transport property to CLI') result = dict() changed = self.present() reportable = ReportableChanges(params=self.changes.to_return()) changes = reportable.to_return() result.update(**changes) result.update(dict(changed=changed)) return result
def execute(self): warnings = list() changed = ('tmsh modify', 'tmsh create', 'tmsh delete') commands = self.parse_commands(warnings) wait_for = self.want.wait_for or list() retries = self.want.retries conditionals = [Conditional(c) for c in wait_for] if self.module.check_mode: return while retries > 0: if is_cli(self.module) and HAS_CLI_TRANSPORT: if self.is_tmsh(): for command in commands: command['command'] = command['command'][4:].strip() responses = self._run_commands(self.module, commands) else: responses = self.execute_on_device(commands) for item in list(conditionals): if item(responses): if self.want.match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(self.want.interval) retries -= 1 else: failed_conditions = [item.raw for item in conditionals] errmsg = 'One or more conditional statements have not been satisfied' raise FailedConditionsError(errmsg, failed_conditions) changes = { 'stdout': responses, 'stdout_lines': self._to_lines(responses) } if self.want.warn: changes['warnings'] = warnings self.changes = Parameters(params=changes, module=self.module) if any(x for x in self.want.user_commands if x.startswith(changed)): return True return False