def main(): spec = dict( # { command: <str>, prompt: <str>, response: <str> } 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') ) spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) check_args(module) result = {'changed': False} wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] commands = module.params['commands'] 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, command): command_string = command command = {'command': command} resp = run_commands(self.module, [command]) try: return resp[0] except IndexError: self.warnings.append( 'command %s failed, facts will not be populated' % command_string) return None
def run(module, result): match = module.params['match'] replace = module.params['replace'] path = module.params['parents'] candidate = get_candidate(module) if match != 'none': contents = module.params['config'] if not contents: contents = get_config(module) config = NetworkConfig(indent=1, contents=contents) configobjs = candidate.difference(config, path=path, match=match, replace=replace) else: configobjs = candidate.items if configobjs: commands = dumps(configobjs, 'commands').split('\n') if module.params['lines']: if module.params['before']: commands[:0] = module.params['before'] if module.params['after']: commands.extend(module.params['after']) result['updates'] = commands # send the configuration commands to the device and merge # them with the current running config if not module.check_mode: load_config(module, commands) result['changed'] = True if module.params['save']: if not module.check_mode: run_commands(module, 'write mem') result['changed'] = True
def main(): spec = dict( # { command: <str>, prompt: <str>, response: <str> } inside=dict(type='str', default=''), outside=dict(type='str', default=''), 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')) spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) check_args(module) result = {'changed': False} wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] inside = module.params['inside'] outside = module.params['outside'] retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] nats = [] while retries > 0: if inside: inside_nat_command = ['show nat ' + inside] inside_response = run_commands(module, inside_nat_command) responses = inside_response[0].splitlines() for p in responses: if 'No matching NAT policy found' in p: module.exit_json(msg=p) # Split the response to get the zone, and object names if 'Policies' in p or 'translate_hits' in p or len(p) < 6: #module.fail_json(msg=p) continue if p.split()[5] == 'dynamic': zone = p.split()[1][1:-1] source_object = p.split()[6] outside_object = p.split()[8] type = 'dynamic' elif p.split()[5] == 'static': zone = p.split()[1][1:-1] source_object = p.split()[6] outside_object = p.split()[7] type = 'static' # check if outside object is actually an IP address from section 2 try: IPAddress(outside_object).is_unicast() outside_ip = outside_object except core.AddrFormatError: outside_object_command = [ 'show run object id ' + outside_object ] outside_object_response = run_commands( module, outside_object_command) outside_ip = outside_object_response[0].splitlines( )[1].strip() if source_object == 'any': inside_ip = source_object else: inside_object_command = [ 'show run object id ' + source_object ] inside_object_response = run_commands( module, inside_object_command) inside_ip = inside_object_response[0].splitlines( )[1].strip() nats.append({ 'type': type, 'zone': zone, 'source_object': source_object, 'outside_object': outside_object, 'outside_ip': outside_ip, 'inside_ip': inside_ip }) for item in list(conditionals): if item(inside_response): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) retries -= 1 if outside: outside_nat_command = ['show nat translated ' + outside] outside_response = run_commands(module, outside_nat_command) responses = outside_response[0].splitlines() for p in responses: if 'No matching NAT policy found' in p: module.exit_json(msg=p) # Split the response to get the zone, and object names if 'Policies' in p or 'translate_hits' in p or len(p) < 6: #module.fail_json(msg=p) continue if p.split()[5] == 'dynamic': zone = p.split()[1][1:-1] source_object = p.split()[6] outside_object = p.split()[8] type = 'dynamic' elif p.split()[5] == 'static': zone = p.split()[1][1:-1] source_object = p.split()[6] outside_object = p.split()[7] type = 'static' # check if outside object is actually an IP address from section 2 try: IPAddress(outside_object).is_unicast() outside_ip = outside_object except core.AddrFormatError: outside_object_command = [ 'show run object id ' + outside_object ] outside_object_response = run_commands( module, outside_object_command) outside_ip = outside_object_response[0].splitlines( )[1].strip() if source_object == 'any': inside_ip = source_object else: inside_object_command = [ 'show run object id ' + source_object ] inside_object_response = run_commands( module, inside_object_command) inside_ip = inside_object_response[0].splitlines( )[1].strip() nats.append({ 'type': type, 'zone': zone, 'source_object': source_object, 'outside_object': outside_object, 'outside_ip': outside_ip, 'inside_ip': inside_ip }) for item in list(conditionals): if item(outside_response): 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, 'results': nats}) module.exit_json(**result)
def main(): spec = dict( # { command: <str>, prompt: <str>, response: <str> } count=dict(type="int"), size=dict(type="size"), dest=dict(type="str", required=True), interface=dict(type="str"), data=dict(type="str"), timeout=dict(type="str"), validate=dict(type="bool"), state=dict(type="str", choices=["absent", "present"], default="present"), 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')) spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) check_args(module) result = {'changed': False} wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] count = module.params['count'] dest = module.params['dest'] interface = module.params['interface'] data = module.params['data'] size = module.params['size'] validate = module.params['validate'] timeout = module.params['timeout'] retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] while retries > 0: cmds = [] try: dest_ping = IPSet([IPNetwork(dest)]) for dst in dest_ping: cmds.append( build_ping(str(dst), count, interface, data, size, timeout, validate)) except core.AddrFormatError: cmds.append( build_ping(dest, count, interface, data, size, timeout, validate)) result["commands"] = cmds ping_results = run_commands(module, commands=result["commands"]) result["results"] = [] for ping_result in ping_results: destination_result = {} ping_results_list = ping_result.splitlines() stats = "" for line in ping_results_list: if line.startswith('Success'): stats = line elif line.startswith('Sending'): destination_result['destination'] = line.split( ',')[1].split('to')[1] if stats: success, rx, tx, rtt = parse_ping(stats) loss = abs(100 - int(success)) destination_result["packet_loss"] = str(loss) + "%" destination_result["packets_rx"] = int(rx) destination_result["packets_tx"] = int(tx) # Convert rtt values to int for k, v in rtt.items(): if rtt[k] is not None: rtt[k] = int(v) destination_result["rtt"] = rtt destination_result["msg"] = validate_results( module, loss, result) result["results"].append(destination_result) for item in list(conditionals): if item(inside_response): 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) module.exit_json(**result)
def main(): spec = dict( # { command: <str>, prompt: <str>, response: <str> } zone=dict(type='str'), ip=dict(type='str'), mac=dict(type='str'), 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')) spec.update(asa_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) check_args(module) result = {'changed': False} wait_for = module.params['wait_for'] or list() conditionals = [Conditional(c) for c in wait_for] zone = module.params['zone'] ip = module.params['ip'] mac = module.params['mac'] retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] arps = {} zones = [] while retries > 0: command = build_cmd(zone, ip, mac) response = run_commands(module, command) responses = response[0].splitlines() for p in responses: # Split the response to get the zone, ip, and mac address z = p.split()[0] ip = p.split()[1] mac = p.split()[2] if z in zones: arps[z].append({'ip': ip, 'mac_address': mac}) else: zones.append(z) newzone = {z: [{'ip': ip, 'mac_address': mac}]} arps.update(newzone) for item in list(conditionals): if item(inside_response): 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, 'results': arps}) module.exit_json(**result)