def excute_command(self, commands): """ excute_command""" output = '' runner = CommandRunner(self.module) for cmd in commands: try: runner.add_command(**cmd) except AddCommandError: self.module.fail_json(msg='duplicate command detected: %s' % cmd) try: runner.run() except FailedConditionsError: exc = get_exception() self.module.fail_json(msg=get_cli_exception(exc), failed_conditions=exc.failed_conditions) except FailedConditionalError: exc = get_exception() self.module.fail_json(msg=get_cli_exception(exc), failed_conditional=exc.failed_conditional) except NetworkError: exc = get_exception() self.module.fail_json(msg=get_cli_exception(exc)) for cmd in commands: try: output = runner.get_command(cmd['command'], cmd.get('output')) except ValueError: self.module.fail_json( msg='command not executed due to check_mode, see warnings') return output
def excute_command(self, commands): """ excute_command""" runner = CommandRunner(self.module) for cmd in commands: try: runner.add_command(**cmd) except AddCommandError: exc = get_exception() self.module.fail_json(msg=get_cli_exception(exc)) try: runner.run() except NetworkError: err = get_cli_exception() self.module.fail_json(msg=err)
def cli_load_config(self, commands): """load config by cli""" if not self.module.check_mode: try: self.module.config.load_config(commands) except NetworkError: err = get_cli_exception() self.module.fail_json(msg=err)
def work(self): """ excute task """ if not HAS_SCP: self.module.fail_json( msg="'Error: No scp package, please install it.'") if self.local_file and len(self.local_file) > 4096: self.module.fail_json( msg="'Error: The maximum length of local_file is 4096.'") if self.remote_file and len(self.remote_file) > 4096: self.module.fail_json( msg="'Error: The maximum length of remote_file is 4096.'") retcode, cur_state = self.get_scp_enable() if retcode and cur_state == 'Disable': self.module.fail_json( msg="'Error: Please ensure SCP server is enabled.'") if not os.path.isfile(self.local_file): self.module.fail_json( msg="Local file {} not found".format(self.local_file)) dest = self.remote_file or ('/' + os.path.basename(self.local_file)) remote_exists, file_size = self.remote_file_exists( dest, file_system=self.file_system) if remote_exists and (os.path.getsize(self.local_file) != file_size): remote_exists = False if not remote_exists: self.changed = True file_exists = False else: file_exists = True self.transfer_result = 'The local file already exists on the device.' if not file_exists: try: self.transfer_file(dest) self.transfer_result = 'The local file has been successfully ' \ 'transferred to the device.' except ShellError: clie = get_exception() self.module.fail_json(msg=get_cli_exception(clie)) if self.remote_file is None: self.remote_file = '/' + os.path.basename(self.local_file) self.module.exit_json(changed=self.changed, transfer_result=self.transfer_result, local_file=self.local_file, remote_file=self.remote_file, file_system=self.file_system)
def main(): """ main entry point for module execution """ argument_spec = dict( src=dict(type='path'), lines=dict(aliases=['commands'], type='list'), parents=dict(type='list'), before=dict(type='list'), after=dict(type='list'), match=dict(default='line', choices=['line', 'strict', 'exact', 'none']), replace=dict(default='line', choices=['line', 'block']), config=dict(), defaults=dict(type='bool', default=False), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), ) mutually_exclusive = [('lines', 'src')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines'])] module = NetworkModule(argument_spec=argument_spec, connect_on_load=False, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) warnings = list() result = dict(changed=False, warnings=warnings) if module.params['backup']: result['__backup__'] = module.config.get_config() try: run(module, result) except NetworkError: exc = get_exception() config_fail(module, msg=get_cli_exception(exc), **exc.kwargs) config_exit(module, **result)
def excute_command(self, commands): """ Method to execute command """ runner = CommandRunner(self.module) for cmd in commands: try: runner.add_command(**cmd) except AddCommandError: exc = get_exception() self.module.fail_json(msg=str(exc)) try: runner.run() except NetworkError: err = get_cli_exception() self.module.fail_json(msg=err) output = [] for cmd in commands: tmp = runner.get_command(cmd['command'], cmd.get('output')) output.append(tmp) return output
def main(): """ Module main """ spec = dict(gather_subset=dict(default=['!config'], type='list')) module = NetworkModule(argument_spec=spec, supports_check_mode=True) gather_subset = module.params['gather_subset'] runable_subsets = set() exclude_subsets = set() for subset in gather_subset: if subset == 'all': runable_subsets.update(VALID_SUBSETS) continue if subset.startswith('!'): subset = subset[1:] if subset == 'all': exclude_subsets.update(VALID_SUBSETS) continue exclude = True else: exclude = False if subset not in VALID_SUBSETS: module.fail_json(msg='Bad subset') if exclude: exclude_subsets.add(subset) else: runable_subsets.add(subset) if not runable_subsets: runable_subsets.update(VALID_SUBSETS) runable_subsets.difference_update(exclude_subsets) facts = dict() facts['gather_subset'] = list(runable_subsets) runner = CommandRunner(module) instances = list() for key in runable_subsets: instances.append(FACT_SUBSETS[key](module, runner)) try: runner.run() except NetworkError: exc = get_exception() module.fail_json(msg=get_cli_exception(exc), **exc.kwargs) try: for inst in instances: inst.populate() facts.update(inst.facts) except Exception: raise ansible_facts = dict() for key, value in iteritems(facts): if key.startswith('_'): ansible_facts[key[1:]] = value else: ansible_facts[key] = value module.exit_json(ansible_facts=ansible_facts)
def main(): """ main """ spec = dict(commands=dict(type='list', required=True), wait_for=dict(type='list', aliases=['waitfor']), match=dict(default='all', choices=['any', 'all']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int')) module = NetworkModule(argument_spec=spec, supports_check_mode=True) commands = list(parse_commands(module)) conditionals = module.params['wait_for'] or list() warnings = list() runner = CommandRunner(module) for cmd in commands: if module.check_mode and not cmd['command'].startswith('dis'): warnings.append('only display commands are supported when using ' 'check mode, not executing `%s`' % cmd['command']) else: if cmd['command'].startswith('sys'): module.fail_json( msg='Error: ce_command does not support running ' 'config mode commands. Please use ' 'ce_config instead') try: runner.add_command(**cmd) except AddCommandError: exc = get_exception() warnings.append('duplicate command detected: %s' % cmd) try: for item in conditionals: runner.add_conditional(item) except AddConditionError: exc = get_exception() module.fail_json(msg=get_cli_exception(exc), condition=exc.condition) runner.retries = module.params['retries'] runner.interval = module.params['interval'] runner.match = module.params['match'] try: runner.run() except FailedConditionsError: exc = get_exception() module.fail_json(msg=get_cli_exception(exc), failed_conditions=exc.failed_conditions) except FailedConditionalError: exc = get_exception() module.fail_json(msg=get_cli_exception(exc), failed_conditional=exc.failed_conditional) except NetworkError: err = get_cli_exception() module.fail_json(msg=err) result = dict(changed=False) result['stdout'] = list() for cmd in commands: try: output = runner.get_command(cmd['command'], cmd.get('output')) except ValueError: output = 'command not executed due to check_mode, see warnings' result['stdout'].append(output) result['warnings'] = warnings result['stdout_lines'] = list(to_lines(result['stdout'])) module.exit_json(**result)