def main(): argument_spec = dict( src=dict(type='path'), lines=dict(type='list'), comment=dict(default=DEFAULT_COMMENT), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), ) argument_spec.update(vyos_argument_spec) mutually_exclusive = [('lines', 'src')] module = AnsibleModule(argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True) warnings = list() result = dict(changed=False, warnings=warnings) if module.params['backup']: result['__backup__'] = get_config(module=module) if any((module.params['src'], module.params['lines'])): run(module, result) if module.params['save']: diff = run_commands(module, commands=['configure', 'compare saved'])[1] if diff != '[edit]': run_commands(module, commands=['save']) result['changed'] = True run_commands(module, commands=['exit']) module.exit_json(**result)
def map_config_to_obj(module): objs = [] interfaces = list() output = run_commands(module, 'show interfaces') lines = output[0].strip().splitlines()[3:] for l in lines: splitted_line = re.split(r'\s{2,}', l.strip()) obj = {} eth = splitted_line[0].strip("'") if eth.startswith('eth'): obj['interfaces'] = [] if '.' in eth: interface = eth.split('.')[0] obj['interfaces'].append(interface) obj['vlan_id'] = eth.split('.')[-1] else: obj['interfaces'].append(eth) obj['vlan_id'] = None if splitted_line[1].strip("'") != '-': obj['address'] = splitted_line[1].strip("'") if len(splitted_line) > 3: obj['name'] = splitted_line[3].strip("'") obj['state'] = 'present' objs.append(obj) return objs
def map_config_to_obj(module): obj = [] output = run_commands(module, ['show interfaces bonding slaves']) lines = output[0].splitlines() if len(lines) > 1: for line in lines[1:]: splitted_line = line.split() name = splitted_line[0] mode = splitted_line[1] state = splitted_line[2] if len(splitted_line) > 4: members = splitted_line[4:] else: members = [] obj.append({ 'name': name, 'mode': mode, 'members': members, 'state': state }) return obj
def main(): command_spec = dict(command=dict(key=True), prompt=dict(), answer=dict()) spec = dict(commands=dict(type='list', elements='dict', options=command_spec, 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(vyos_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) warnings = list() commands = parse_commands(module, 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'] for _ in range(retries): responses = run_commands(module, commands) for item in conditionals: if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) 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, falied_conditions=failed_conditions) result = { 'changed': False, 'stdout': responses, 'warnings': warnings, 'stdout_lines': list(to_lines(responses)), } module.exit_json(**result)
def map_config_to_obj(module): obj = [] output = run_commands(module, ['show interfaces']) lines = re.split(r'\n[e|l]', output[0])[1:] if len(lines) > 0: for line in lines: splitted_line = line.split() if len(splitted_line) > 0: ipv4 = [] ipv6 = [] if splitted_line[0].lower().startswith('th'): name = 'e' + splitted_line[0].lower() elif splitted_line[0].lower().startswith('o'): name = 'l' + splitted_line[0].lower() for i in splitted_line[1:]: if (('.' in i or ':' in i) and '/' in i): value = i.split(r'\n')[0] if is_ipv4(value): ipv4.append(value) elif is_ipv6(value): ipv6.append(value) obj.append({'name': name, 'ipv4': ipv4, 'ipv6': ipv6}) return obj
def main(): """ main entry point for module execution """ argument_spec = dict( count=dict(type="int", default=5), dest=dict(type="str", required=True), source=dict(type="str"), ttl=dict(type='int'), size=dict(type='int'), interval=dict(type='int'), state=dict(type="str", choices=["absent", "present"], default="present"), ) argument_spec.update(vyos_argument_spec) module = AnsibleModule(argument_spec=argument_spec) count = module.params["count"] dest = module.params["dest"] source = module.params["source"] size = module.params["size"] ttl = module.params["ttl"] interval = module.params["interval"] warnings = list() results = {} if warnings: results["warnings"] = warnings results["commands"] = [build_ping(dest, count, size, interval, source, ttl)] ping_results = run_commands(module, commands=results["commands"]) ping_results_list = ping_results[0].split("\n") rtt_info, rate_info = None, None for line in ping_results_list: if line.startswith('rtt'): rtt_info = line if line.startswith('%s packets transmitted' % count): rate_info = line if rtt_info: rtt = parse_rtt(rtt_info) for k, v in rtt.items(): if rtt[k] is not None: rtt[k] = int(v) results["rtt"] = rtt pkt_loss, rx, tx = parse_rate(rate_info) results["packet_loss"] = str(pkt_loss) + "%" results["packets_rx"] = int(rx) results["packets_tx"] = int(tx) validate_results(module, pkt_loss, results) module.exit_json(**results)
def main(): 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') ) spec.update(vyos_argument_spec) module = AnsibleModule(argument_spec=spec, supports_check_mode=True) warnings = list() commands = parse_commands(module, 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)) retries = module.params['retries'] interval = module.params['interval'] match = module.params['match'] for _ in range(retries): responses = run_commands(module, commands) for item in conditionals: if item(responses): if match == 'any': conditionals = list() break conditionals.remove(item) if not conditionals: break time.sleep(interval) 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, falied_conditions=failed_conditions) result = { 'changed': False, 'stdout': responses, 'warnings': warnings, 'stdout_lines': list(to_lines(responses)), } module.exit_json(**result)
def main(): argument_spec = dict( src=dict(type='path'), lines=dict(type='list'), match=dict(default='line', choices=['line', 'none']), comment=dict(default=DEFAULT_COMMENT), config=dict(), backup=dict(type='bool', default=False), save=dict(type='bool', default=False), ) argument_spec.update(vyos_argument_spec) mutually_exclusive = [('lines', 'src')] module = AnsibleModule( argument_spec=argument_spec, mutually_exclusive=mutually_exclusive, supports_check_mode=True ) warnings = list() result = dict(changed=False, warnings=warnings) if module.params['backup']: result['__backup__'] = get_config(module=module) if any((module.params['src'], module.params['lines'])): run(module, result) if module.params['save']: diff = run_commands(module, commands=['configure', 'compare saved'])[1] if diff != '[edit]': run_commands(module, commands=['save']) result['changed'] = True run_commands(module, commands=['exit']) module.exit_json(**result)
def map_config_to_obj(module): obj = [] output = run_commands(module, ['show interfaces']) lines = output[0].splitlines() if len(lines) > 3: for line in lines[3:]: splitted_line = line.split() if len(splitted_line) > 1: name = splitted_line[0] address = splitted_line[1] if address == '-': address = None if address is not None and ':' not in address: obj.append({'name': name, 'ipv4': address, 'ipv6': None}) else: obj.append({'name': name, 'ipv6': address, 'ipv4': None}) else: obj[-1]['ipv6'] = splitted_line[0] return obj
def map_config_to_obj(module): obj = [] output = run_commands(module, ['show interfaces bonding slaves']) lines = output[0].splitlines() if len(lines) > 1: for line in lines[1:]: splitted_line = line.split() name = splitted_line[0] mode = splitted_line[1] state = splitted_line[2] if len(splitted_line) > 4: members = splitted_line[4:] else: members = [] obj.append({'name': name, 'mode': mode, 'members': members, 'state': state}) return obj
def populate(self): self.responses = run_commands(self.module, list(self.COMMANDS))