def main(): module = AnsibleModule(argument_spec=dict( tunnel=dict(required=True, type='str'), src=dict(required=False, type='str'), dest=dict(required=False, type='str'), global_src=dict(required=False, type='str'), state=dict(choices=['present', 'absent'], default='present'), port=dict(default=830, type='int'), hostname=dict(required=True), username=dict(required=True), password=dict(required=True), ), supports_check_mode=True) if not HAS_PYHP: module.fail_json(msg='There was a problem loading from the pyhpecw7 ' + 'module.', error=str(ie)) username = module.params['username'] password = module.params['password'] port = module.params['port'] hostname = socket.gethostbyname(module.params['hostname']) device_args = dict(host=hostname, username=username, password=password, port=port) device = HPCOM7(**device_args) tunnel = module.params['tunnel'] src = module.params['src'] dest = module.params['dest'] global_src = module.params['global_src'] state = module.params['state'] changed = False args = dict(src=src, dest=dest) proposed = dict((k, v) for k, v in args.iteritems() if v is not None) try: device.open() except ConnectionError as e: safe_fail(module, device, msg=str(e), descr='error opening device conn') try: l2vpn = L2VPN(device) is_l2vpn_enabled = l2vpn.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='L2VPN check failed') if is_l2vpn_enabled == 'disabled': safe_fail(module, device, msg='l2vpn needs to be enabled.') try: tun = Tunnel(device, tunnel) existing = tun.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='could not get tunnel config') if state == 'present': if existing.get('mode') and existing.get('mode') != 'vxlan': safe_fail(module, device, msg='tunnel interface exists but is not a ' + 'vxlan \ntunnel interface. remove and re-add.') delta = dict(set(proposed.iteritems()).difference(existing.iteritems())) try: existing_gsrc = tun.get_global_source() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='could not get existing global src') if global_src: if existing_gsrc != global_src: delta['global_src'] = global_src if state == 'present': if delta or not existing: tun.build(stage=True, **delta) elif state == 'absent': if existing: tun.remove(stage=True) commands = None end_state = existing if device.staged: commands = device.staged_to_string() if module.check_mode: device.close() safe_exit(module, device, changed=True, commands=commands) else: try: device.execute_staged() end_state = tun.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='error during execution') end_state['global_src'] = tun.get_global_source() changed = True proposed['global_src'] = global_src existing['global_src'] = existing_gsrc results = {} results['proposed'] = proposed results['existing'] = existing results['state'] = state results['commands'] = commands results['changed'] = changed results['end_state'] = end_state safe_exit(module, device, **results)
def main(): module = AnsibleModule(argument_spec=dict( vxlan=dict(required=True, type='str'), vsi=dict(required=True, type='str'), tunnels=dict(required=False, type='list'), descr=dict(required=False), state=dict(choices=['present', 'absent'], default='present'), port=dict(default=830, type='int'), hostname=dict(required=True), username=dict(required=True), password=dict(required=True), ), supports_check_mode=True) if not HAS_PYHP: module.fail_json(msg='There was a problem loading from the pyhpecw7 ' + 'module.', error=str(ie)) username = module.params['username'] password = module.params['password'] port = module.params['port'] hostname = socket.gethostbyname(module.params['hostname']) device_args = dict(host=hostname, username=username, password=password, port=port) device = HPCOM7(**device_args) vxlan = module.params['vxlan'] vsi = module.params['vsi'] descr = module.params['descr'] tunnels = normalize_to_list(module.params['tunnels']) state = module.params['state'] changed = False args = dict(vxlan=vxlan, vsi=vsi, descr=descr) proposed = dict((k, v) for k, v in args.iteritems() if v is not None) try: device.open() except ConnectionError as e: safe_fail(module, device, msg=str(e), descr='error opening device conn') try: l2vpn = L2VPN(device) is_l2vpn_enabled = l2vpn.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='L2VPN check failed') if is_l2vpn_enabled == 'disabled': safe_fail(module, device, msg='l2vpn needs to be enabled.') try: VXLAN = Vxlan(device, vxlan, vsi) existing = VXLAN.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='could not obtain existing') if state == 'present': checks(existing, proposed, module) if 'tunnels' in existing.keys(): existing_tunnels = existing.pop('tunnels') else: existing_tunnels = [] delta = dict(set(proposed.iteritems()).difference(existing.iteritems())) tunnels_to_add = list(set(tunnels).difference(existing_tunnels)) tunnels_to_remove = list(set(existing_tunnels).difference(tunnels)) if tunnels_to_add: delta['tunnels_to_add'] = tunnels_to_add for each in tunnels_to_add: tun = Tunnel(device, each) exists = tun.get_config() if not exists: safe_fail(module, device, msg='tunnel needs to exist first' + ' before \nbefore adding it to a vxlan', tunnel=each) if tunnels_to_remove: delta['tunnels_to_remove'] = tunnels_to_remove if state == 'present': if not existing.get('vxlan'): VXLAN.create(stage=True) if delta: VXLAN.build(stage=True, **delta) elif state == 'absent': if existing: # existing is based off the VXLAN ID # if it's not mapped to any VSI, it's not considered # existing although the VSI may exist if existing.get('vsi') != vsi: safe_fail(module, device, msg='vsi/vxlan mapping must exist' + ' on switch to remove it', current_vsi=existing.get('vsi')) else: VXLAN.remove_vsi(stage=True) commands = None end_state = existing if device.staged: commands = device.staged_to_string() if module.check_mode: device.close() safe_exit(module, device, changed=True, commands=commands) else: try: device.execute_staged() end_state = VXLAN.get_config() except PYHPError as e: safe_fail(module, device, msg=str(e), descr='failed during execution') changed = True if tunnels: proposed.update(tunnels=tunnels) if existing_tunnels: existing.update(tunnels=existing_tunnels) results = {} results['proposed'] = proposed results['existing'] = existing results['state'] = state results['commands'] = commands results['changed'] = changed results['end_state'] = end_state safe_exit(module, device, **results)
def setUp(self, mock_device): self.device = mock_device self.l2eth = L2EthService(self.device, INTERFACE, INSTANCE, VSI) self.vxlan = Vxlan(self.device, INSTANCE, vsi=VSI) self.tunnel = Tunnel(self.device, TUNNEL)