def save_config(module, result): result['changed'] = True if not module.check_mode: command = {"command": "save configuration", "prompt": "Do you want to save configuration", "answer": "y"} run_commands(module, command) else: module.warn('Skipping command `save configuration` ' 'due to check_mode. Configuration not copied to ' 'non-volatile storage')
def get_startup_config(module, flags=None): reply = run_commands(module, {'command': 'show switch', 'output': 'text'}) match = re.search(r'Config Selected: +(\S+)\.cfg', to_text(reply, errors='surrogate_or_strict').strip(), re.MULTILINE) if match: cfgname = match.group(1).strip() command = ' '.join(['debug cfgmgr show configuration file', cfgname]) if flags: command += ' '.join(to_list(flags)).strip() reply = run_commands(module, {'command': command, 'output': 'text'}) data = reply[0] else: data = '' return data
def main(): """main entry point for module execution """ argument_spec = dict(commands=dict(type='list', required=True), wait_for=dict(type='list'), match=dict(default='all', choices=['all', 'any']), retries=dict(default=10, type='int'), interval=dict(default=1, type='int')) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) result = {'changed': False} warnings = list() commands = parse_commands(module, warnings) result['warnings'] = warnings wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] 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({ 'changed': False, 'stdout': responses, 'stdout_lines': list(to_lines(responses)) }) module.exit_json(**result)
def run(self, cmd): return run_commands(self.module, cmd)
def populate(self): self.responses = run_commands(self.module, self.COMMANDS)