def commit_label(self): """commit label""" commands = list() cmd1 = {'output': None, 'command': 'system-view'} commands.append(cmd1) cmd2 = {'output': None, 'command': ''} cmd2['command'] = "commit label %s" % self.label commands.append(cmd2) self.updates_cmd.append("commit label %s" % self.label) run_commands(self.module, commands) self.changed = True
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), ) argument_spec.update(ce_argument_spec) mutually_exclusive = [('lines', '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, ['save']) result['changed'] = True module.exit_json(**result)
def commit_label(self): """commit label""" commands = list() cmd1 = {'output': None, 'command': 'system-view'} commands.append(cmd1) cmd2 = {'output': None, 'command': ''} cmd2['command'] = "commit label %s" % self.label commands.append(cmd2) self.updates_cmd.append( "commit label %s" % self.label) run_commands(self.module, commands) self.changed = True
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: exc = get_exception() module.fail_json(msg=str(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 be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({'stdout': responses, 'stdout_lines': to_lines(responses)}) module.exit_json(**result)
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 enough_space(self): """Whether device has enough space""" commands = list() cmd = 'dir %s' % self.file_system commands.append(cmd) output = run_commands(self.module, commands) if not output: return True match = re.search(r'\((.*) KB free\)', output[0]) kbytes_free = match.group(1) kbytes_free = kbytes_free.replace(',', '') file_size = os.path.getsize(self.local_file) if int(kbytes_free) * 1024 > file_size: return True return False
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 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 be satisfied' module.fail_json(msg=msg, failed_conditions=failed_conditions) result.update({ 'stdout': responses, 'stdout_lines': to_lines(responses) }) module.exit_json(**result)