def main(): module_specific_arguments = dict( name=dict(type='str'), targetlbvserver=dict(type='str'), targetvserverexpr=dict(type='str'), comment=dict(type='str'), ) argument_spec = dict() argument_spec.update(netscaler_common_arguments) argument_spec.update(module_specific_arguments) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) module_result = dict( changed=False, failed=False, loglines=loglines ) # Fail the module if imports failed if not PYTHON_SDK_IMPORTED: module.fail_json(msg='Could not load nitro python sdk') # Fallthrough to rest of execution client = get_nitro_client(module) try: client.login() except nitro_exception as e: msg = "nitro exception during login. errorcode=%s, message=%s" % (str(e.errorcode), e.message) module.fail_json(msg=msg) except Exception as e: if str(type(e)) == "<class 'requests.exceptions.ConnectionError'>": module.fail_json(msg='Connection error %s' % str(e)) elif str(type(e)) == "<class 'requests.exceptions.SSLError'>": module.fail_json(msg='SSL Error %s' % str(e)) else: module.fail_json(msg='Unexpected error during login %s' % str(e)) readwrite_attrs = [ 'name', 'targetlbvserver', 'targetvserverexpr', 'comment', ] readonly_attrs = [ 'hits', 'referencecount', 'undefhits', 'builtin', ] immutable_attrs = [ 'name', 'targetvserverexpr', ] transforms = { } # Instantiate config proxy csaction_proxy = ConfigProxy( actual=csaction(), client=client, attribute_values_dict=module.params, readwrite_attrs=readwrite_attrs, readonly_attrs=readonly_attrs, immutable_attrs=immutable_attrs, transforms=transforms, ) try: ensure_feature_is_enabled(client, 'CS') # Apply appropriate state if module.params['state'] == 'present': log('Applying actions for state present') if not action_exists(client, module): if not module.check_mode: csaction_proxy.add() if module.params['save_config']: client.save_config() module_result['changed'] = True elif not action_identical(client, module, csaction_proxy): # Check if we try to change value of immutable attributes immutables_changed = get_immutables_intersection(csaction_proxy, diff_list(client, module, csaction_proxy).keys()) if immutables_changed != []: module.fail_json( msg='Cannot update immutable attributes %s' % (immutables_changed,), diff=diff_list(client, module, csaction_proxy), **module_result ) if not module.check_mode: csaction_proxy.update() if module.params['save_config']: client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for state log('Sanity checks for state present') if not module.check_mode: if not action_exists(client, module): module.fail_json(msg='Content switching action does not exist', **module_result) if not action_identical(client, module, csaction_proxy): module.fail_json( msg='Content switching action differs from configured', diff=diff_list(client, module, csaction_proxy), **module_result ) elif module.params['state'] == 'absent': log('Applying actions for state absent') if action_exists(client, module): if not module.check_mode: csaction_proxy.delete() if module.params['save_config']: client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for state if not module.check_mode: log('Sanity checks for state absent') if action_exists(client, module): module.fail_json(msg='Content switching action still exists', **module_result) except nitro_exception as e: msg = "nitro exception errorcode=%s, message=%s" % (str(e.errorcode), e.message) module.fail_json(msg=msg, **module_result) client.logout() module.exit_json(**module_result)
def main(): module_specific_arguments = dict( name=dict(type='str'), targetlbvserver=dict(type='str'), targetvserverexpr=dict(type='str'), comment=dict(type='str'), ) argument_spec = dict() argument_spec.update(netscaler_common_arguments) argument_spec.update(module_specific_arguments) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) module_result = dict( changed=False, failed=False, loglines=loglines ) # Fail the module if imports failed if not PYTHON_SDK_IMPORTED: module.fail_json(msg='Could not load nitro python sdk') # Fallthrough to rest of execution client = get_nitro_client(module) try: client.login() except nitro_exception as e: msg = "nitro exception during login. errorcode=%s, message=%s" % (str(e.errorcode), e.message) module.fail_json(msg=msg) except Exception as e: if str(type(e)) == "<class 'requests.exceptions.ConnectionError'>": module.fail_json(msg='Connection error %s' % str(e)) elif str(type(e)) == "<class 'requests.exceptions.SSLError'>": module.fail_json(msg='SSL Error %s' % str(e)) else: module.fail_json(msg='Unexpected error during login %s' % str(e)) readwrite_attrs = [ 'name', 'targetlbvserver', 'targetvserverexpr', 'comment', ] readonly_attrs = [ 'hits', 'referencecount', 'undefhits', 'builtin', ] immutable_attrs = [ 'name', 'targetvserverexpr', ] transforms = { } json_encodes = ['targetvserverexpr'] # Instantiate config proxy csaction_proxy = ConfigProxy( actual=csaction(), client=client, attribute_values_dict=module.params, readwrite_attrs=readwrite_attrs, readonly_attrs=readonly_attrs, immutable_attrs=immutable_attrs, transforms=transforms, json_encodes=json_encodes, ) try: ensure_feature_is_enabled(client, 'CS') # Apply appropriate state if module.params['state'] == 'present': log('Applying actions for state present') if not action_exists(client, module): if not module.check_mode: csaction_proxy.add() if module.params['save_config']: client.save_config() module_result['changed'] = True elif not action_identical(client, module, csaction_proxy): # Check if we try to change value of immutable attributes immutables_changed = get_immutables_intersection(csaction_proxy, diff_list(client, module, csaction_proxy).keys()) if immutables_changed != []: module.fail_json( msg='Cannot update immutable attributes %s' % (immutables_changed,), diff=diff_list(client, module, csaction_proxy), **module_result ) if not module.check_mode: csaction_proxy.update() if module.params['save_config']: client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for state log('Sanity checks for state present') if not module.check_mode: if not action_exists(client, module): module.fail_json(msg='Content switching action does not exist', **module_result) if not action_identical(client, module, csaction_proxy): module.fail_json( msg='Content switching action differs from configured', diff=diff_list(client, module, csaction_proxy), **module_result ) elif module.params['state'] == 'absent': log('Applying actions for state absent') if action_exists(client, module): if not module.check_mode: csaction_proxy.delete() if module.params['save_config']: client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for state if not module.check_mode: log('Sanity checks for state absent') if action_exists(client, module): module.fail_json(msg='Content switching action still exists', **module_result) except nitro_exception as e: msg = "nitro exception errorcode=%s, message=%s" % (str(e.errorcode), e.message) module.fail_json(msg=msg, **module_result) client.logout() module.exit_json(**module_result)
def main(): from ansible.module_utils.netscaler import ConfigProxy, get_nitro_client, netscaler_common_arguments, log, loglines, ensure_feature_is_enabled try: from nssrc.com.citrix.netscaler.nitro.resource.config.cs.csaction import csaction from nssrc.com.citrix.netscaler.nitro.exception.nitro_exception import nitro_exception python_sdk_imported = True except ImportError as e: python_sdk_imported = False module_specific_arguments = dict( name=dict(type='str'), targetlbvserver=dict(type='str'), targetvserverexpr=dict(type='str'), comment=dict(type='str'), ) argument_spec = dict() argument_spec.update(netscaler_common_arguments) argument_spec.update(module_specific_arguments) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=True, ) module_result = dict(changed=False, failed=False, loglines=loglines) # Fail the module if imports failed if not python_sdk_imported: module.fail_json(msg='Could not load nitro python sdk') # Fallthrough to rest of execution client = get_nitro_client(module) client.login() # Instantiate Service Config object readwrite_attrs = [ 'name', 'targetlbvserver', 'targetvserverexpr', 'comment', ] readonly_attrs = [ 'hits', 'referencecount', 'undefhits', 'builtin', ] ''' if 'targetvserverexpr' in module.params and module.params['targetvserverexpr'] is not None: module.params['targetvserverexpr'] = json.dumps(module.params['targetvserverexpr']) ''' csaction_proxy = ConfigProxy(actual=csaction(), client=client, attribute_values_dict=module.params, readwrite_attrs=readwrite_attrs, readonly_attrs=readonly_attrs, json_encodes=['targetvserverexpr']) def action_exists(): if csaction.count_filtered(client, 'name:%s' % module.params['name']) > 0: return True else: return False def action_identical(): if len(diff_list()) == 0: return True else: return False def diff_list(): action_list = csaction.get_filtered(client, 'name:%s' % module.params['name']) diff_list = csaction_proxy.diff_object(action_list[0]) if False and 'targetvserverexpr' in diff_list: json_value = json.loads(action_list[0].targetvserverexpr) if json_value == module.params['targetvserverexpr']: del diff_list['targetvserverexpr'] return diff_list try: ensure_feature_is_enabled(client, 'CS') # Apply appropriate operation if module.params['operation'] == 'present': if not action_exists(): if not module.check_mode: csaction_proxy.add() client.save_config() module_result['changed'] = True elif not action_identical(): if not module.check_mode: csaction_proxy.update() client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for operation if not action_exists(): module.fail_json(msg='Content switching action does not exist', **module_result) if not action_identical(): module.fail_json( msg='Content switching action differs from configured', diff=diff_list(), **module_result) elif module.params['operation'] == 'absent': if action_exists(): if not module.check_mode: csaction_proxy.delete() client.save_config() module_result['changed'] = True else: module_result['changed'] = False # Sanity check for operation if action_exists(): module.fail_json(msg='Service still exists', **module_result) except nitro_exception as e: msg = "nitro exception errorcode=" + str( e.errorcode) + ",message=" + e.message module.fail_json(msg=msg, **module_result) client.logout() module.exit_json(**module_result)