def main(): argument_spec = dict(vni=dict(required=True, type='str'), route_distinguisher=dict(required=False, type='str'), route_target_both=dict(required=False, type='list'), route_target_import=dict(required=False, type='list'), route_target_export=dict(required=False, type='list'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) state = module.params['state'] args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) commands = [] parents = [] proposed = {} for key, value in proposed_args.items(): if key != 'vni': if value == 'true': value = True elif value == 'false': value = False if existing.get(key) != value: proposed[key] = value if state == 'present': commands, parents = state_present(module, existing, proposed) elif state == 'absent' and existing: commands, parents = state_absent(module, existing, proposed) if commands: if (existing.get('route_distinguisher') and proposed.get('route_distinguisher')): if (existing['route_distinguisher'] != proposed['route_distinguisher'] and proposed['route_distinguisher'] != 'default'): warnings.append('EVPN RD {0} was automatically removed. ' 'It is highly recommended to use a task ' '(with default as value) to explicitly ' 'unconfigure it.'.format( existing['route_distinguisher'])) remove_commands = [ 'no rd {0}'.format(existing['route_distinguisher']) ] candidate = CustomNetworkConfig(indent=3) candidate.add(remove_commands, parents=parents) load_config(module, candidate) results['changed'] = True results['commands'] = candidate.items_text() time.sleep(30) else: candidate = CustomNetworkConfig(indent=3) candidate.add(commands, parents=parents) candidate = candidate.items_text() load_config(module, candidate) results['changed'] = True results['commands'] = candidate else: results['commands'] = [] module.exit_json(**results)
def main(): argument_spec = dict(vni=dict(required=True, type='str'), route_distinguisher=dict(required=False, type='str'), route_target_both=dict(required=False, type='list'), route_target_import=dict(required=False, type='list'), route_target_export=dict(required=False, type='list'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False)) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) state = module.params['state'] args = [ 'vni', 'route_distinguisher', 'route_target_both', 'route_target_import', 'route_target_export' ] existing = invoke('get_existing', module, args) end_state = existing proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) proposed = {} for key, value in proposed_args.items(): if key != 'vni': if value == 'true': value = True elif value == 'false': value = False if existing.get(key) or (not existing.get(key) and value): proposed[key] = value result = {} if state == 'present' or (state == 'absent' and existing): candidate = CustomNetworkConfig(indent=3) commands, parents = invoke('state_%s' % state, module, existing, proposed) if commands: if (existing.get('route_distinguisher') and proposed.get('route_distinguisher')): if (existing['route_distinguisher'] != proposed['route_distinguisher'] and proposed['route_distinguisher'] != 'default'): WARNINGS.append('EVPN RD {0} was automatically removed. ' 'It is highly recommended to use a task ' '(with default as value) to explicitly ' 'unconfigure it.'.format( existing['route_distinguisher'])) remove_commands = [ 'no rd {0}'.format(existing['route_distinguisher']) ] candidate.add(remove_commands, parents=parents) result = execute_config(module, candidate) time.sleep(30) candidate = CustomNetworkConfig(indent=3) candidate.add(commands, parents=parents) result = execute_config(module, candidate) else: result['updates'] = [] if module._verbosity > 0: end_state = invoke('get_existing', module, args) result['end_state'] = end_state result['existing'] = existing result['proposed'] = proposed_args if WARNINGS: result['warnings'] = WARNINGS module.exit_json(**result)
def main(): argument_spec = dict( vni=dict(required=True, type='str'), route_distinguisher=dict(required=False, type='str'), route_target_both=dict(required=False, type='list'), route_target_import=dict(required=False, type='list'), route_target_export=dict(required=False, type='list'), state=dict(choices=['present', 'absent'], default='present', required=False), include_defaults=dict(default=True), config=dict(), save=dict(type='bool', default=False) ) argument_spec.update(nxos_argument_spec) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) warnings = list() check_args(module, warnings) results = dict(changed=False, warnings=warnings) state = module.params['state'] args = PARAM_TO_COMMAND_KEYMAP.keys() existing = get_existing(module, args) proposed_args = dict((k, v) for k, v in module.params.items() if v is not None and k in args) commands = [] parents = [] proposed = {} for key, value in proposed_args.items(): if key != 'vni': if value == 'true': value = True elif value == 'false': value = False if existing.get(key) != value: proposed[key] = value if state == 'present': commands, parents = state_present(module, existing, proposed) elif state == 'absent' and existing: commands, parents = state_absent(module, existing, proposed) if commands: if (existing.get('route_distinguisher') and proposed.get('route_distinguisher')): if (existing['route_distinguisher'] != proposed['route_distinguisher'] and proposed['route_distinguisher'] != 'default'): warnings.append('EVPN RD {0} was automatically removed. ' 'It is highly recommended to use a task ' '(with default as value) to explicitly ' 'unconfigure it.'.format(existing['route_distinguisher'])) remove_commands = ['no rd {0}'.format(existing['route_distinguisher'])] candidate = CustomNetworkConfig(indent=3) candidate.add(remove_commands, parents=parents) load_config(module, candidate) results['changed'] = True results['commands'] = candidate.items_text() time.sleep(30) else: candidate = CustomNetworkConfig(indent=3) candidate.add(commands, parents=parents) candidate = candidate.items_text() load_config(module, candidate) results['changed'] = True results['commands'] = candidate else: results['commands'] = [] module.exit_json(**results)