def main(): """ main entry point for module execution """ backup_spec = dict( filename=dict(), dir_path=dict(type='path') ) 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), backup_options=dict(type='dict', options=backup_spec), save=dict(type='bool', default=False), ) argument_spec.update(ce_argument_spec) mutually_exclusive = [('lines', 'src'), ('parents', 'src')] required_if = [('match', 'strict', ['lines']), ('match', 'exact', ['lines']), ('replace', 'block', ['lines'])] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, required_if=required_if, supports_check_mode=True) warnings = list() check_args(module, warnings) result = dict(changed=False, warnings=warnings) if module.params['backup']: result['__backup__'] = get_config(module) if any((module.params['src'], module.params['lines'])): run(module, result) if module.params['save']: if not module.check_mode: run_commands(module, ['return', 'mmi-mode enable', 'save']) result["changed"] = True run_commands(module, ['return', 'undo mmi-mode enable']) module.exit_json(**result)
def main(): """entry point for module execution """ argument_spec = dict( # { command: <str>, output: <str>, prompt: <str>, response: <str> } commands=dict(type='list', required=True), wait_for=dict(type='list'), match=dict(default='all', choices=['any', 'all']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int')) argument_spec.update(ce_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() check_args(module, warnings) commands = parse_commands(module, warnings) result['warnings'] = warnings wait_for = module.params['wait_for'] or list() try: conditionals = [Conditional(c) for c in wait_for] except AttributeError as exc: module.fail_json(msg=to_native(exc), exception=traceback.format_exc()) retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] while retries > 0: responses = run_commands(module, commands) for item in list(conditionals): if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) retries -= 1 if conditionals: failed_conditions = [item.raw for item in conditionals] msg = 'One or more conditional statements have not been satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({'stdout': responses, 'stdout_lines': to_lines(responses)}) module.exit_json(**result)
def get_running_config(module): contents = module.params['config'] if not contents: command = "display current-configuration " if module.params['defaults']: command += 'include-default' resp = run_commands(module, command) contents = resp[0] return NetworkConfig(indent=1, contents=contents)
def startup_next_cfg_file(self): """set next cfg file""" commands = list() cmd = {'output': None, 'command': ''} if self.slot: cmd['command'] = "startup saved-configuration %s slot %s" % ( self.cfg_file, self.slot) commands.append(cmd) self.updates_cmd.append("startup saved-configuration %s slot %s" % (self.cfg_file, self.slot)) run_commands(self.module, commands) self.changed = True else: cmd['command'] = "startup saved-configuration %s" % self.cfg_file commands.append(cmd) self.updates_cmd.append("startup saved-configuration %s" % self.cfg_file) run_commands(self.module, commands) self.changed = True
def startup_next_pat_file(self): """set next patch file""" commands = list() cmd = {'output': None, 'command': ''} if self.slot: if self.slot == "all": cmd['command'] = "startup patch %s %s" % (self.patch_file, self.slot) commands.append(cmd) self.updates_cmd.append("startup patch %s %s" % (self.patch_file, self.slot)) run_commands(self.module, commands) self.changed = True else: cmd['command'] = "startup patch %s slot %s" % (self.patch_file, self.slot) commands.append(cmd) self.updates_cmd.append("startup patch %s slot %s" % (self.patch_file, self.slot)) run_commands(self.module, commands) self.changed = True if not self.slot: cmd['command'] = "startup patch %s" % self.patch_file commands.append(cmd) self.updates_cmd.append("startup patch %s" % self.patch_file) run_commands(self.module, commands) self.changed = True
def startup_next_software_file(self): """set next software file""" commands = list() cmd = {'output': None, 'command': ''} if self.slot: if self.slot == "all" or self.slot == "slave-board": cmd['command'] = "startup system-software %s %s" % ( self.software_file, self.slot) commands.append(cmd) self.updates_cmd.append("startup system-software %s %s" % (self.software_file, self.slot)) run_commands(self.module, commands) self.changed = True else: cmd['command'] = "startup system-software %s slot %s" % ( self.software_file, self.slot) commands.append(cmd) self.updates_cmd.append("startup system-software %s slot %s" % (self.software_file, self.slot)) run_commands(self.module, commands) self.changed = True if not self.slot: cmd['command'] = "startup system-software %s" % self.software_file commands.append(cmd) self.updates_cmd.append("startup system-software %s" % self.software_file) run_commands(self.module, commands) self.changed = True
def __init__(self, **kwargs): """ Class init """ # module argument_spec = kwargs["argument_spec"] self.spec = argument_spec self.module = AnsibleModule(argument_spec=self.spec, required_together=[("interface_type", "interface_number")], supports_check_mode=True) # config self.cur_cfg = dict() self.cur_cfg["snmp-agent trap"] = [] self.cur_cfg["undo snmp-agent trap"] = [] # module args self.state = self.module.params['state'] self.feature_name = self.module.params['feature_name'] self.trap_name = self.module.params['trap_name'] self.interface_type = self.module.params['interface_type'] self.interface_number = self.module.params['interface_number'] self.port_number = self.module.params['port_number'] # state self.changed = False self.updates_cmd = list() self.results = dict() self.proposed = dict() self.existing = dict() self.existing["snmp-agent trap"] = [] self.existing["undo snmp-agent trap"] = [] self.end_state = dict() self.end_state["snmp-agent trap"] = [] self.end_state["undo snmp-agent trap"] = [] commands = list() cmd1 = 'display interface brief' commands.append(cmd1) self.interface = run_commands(self.module, commands)
def populate(self): self.responses = run_commands(self.module, list(self.COMMANDS))
def cli_load_config(self, commands): """load config by cli""" if not self.module.check_mode: run_commands(self.module, commands)