def parse_commands(module, warnings): commands = transform_commands(module) if module.check_mode: for item in list(commands): if not item["command"].startswith("show"): warnings.append( "Only show commands are supported when using check mode, not executing %s" % item["command"]) commands.remove(item) return commands
def main(): argument_spec = dict( commands=dict(type='list', required=True), wait_for=dict(type='list', aliases=['waitfor']), 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) warnings = list() result = {'changed': False, 'warnings': warnings} commands = transform_commands(module) 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_text(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': list(to_lines(responses)) }) module.exit_json(**result)
def parse_commands(module, warnings): commands = transform_commands(module) for item in list(commands): if module.check_mode: if item['command'].startswith('conf'): warnings.append( 'only non-config commands are supported when using check mode, not ' 'executing %s' % item['command']) commands.remove(item) return commands
def parse_commands(module, warnings): commands = transform_commands(module) if module.check_mode: for item in list(commands): if not item['command'].startswith('show'): warnings.append( 'Only show commands are supported when using check mode, not ' 'executing %s' % item['command']) commands.remove(item) return commands
def test_transform_commands(): module = MagicMock() module.params = {"commands": ["show interfaces"]} transformed = utils.transform_commands(module) assert transformed == [ { "answer": None, "check_all": False, "command": "show interfaces", "newline": True, "output": None, "prompt": None, "sendonly": False, } ] module.params = {"commands": ["show version", "show memory"]} transformed = utils.transform_commands(module) assert transformed == [ { "answer": None, "check_all": False, "command": "show version", "newline": True, "output": None, "prompt": None, "sendonly": False, }, { "answer": None, "check_all": False, "command": "show memory", "newline": True, "output": None, "prompt": None, "sendonly": False, }, ] module.params = { "commands": [ {"command": "show version", "output": "json"}, "show memory", ] } transformed = utils.transform_commands(module) assert transformed == [ { "answer": None, "check_all": False, "command": "show version", "newline": True, "output": "json", "prompt": None, "sendonly": False, }, { "answer": None, "check_all": False, "command": "show memory", "newline": True, "output": None, "prompt": None, "sendonly": False, }, ]