def operational(module, signature, result): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) mandatory_attributes_in_playbook = copy.deepcopy( MANDATORY_ATTRIBUTES.keys()) component_path = module.params[mandatory_attributes_in_playbook[0]] mandatory_attributes_in_playbook.pop(0) for mandatory_attribute_in_playbook in mandatory_attributes_in_playbook: component_path = component_path + '+' + module.params[ mandatory_attribute_in_playbook] if component_path: result["oper"] = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL + '/' + str(component_path) + '/oper', method='GET', body='', signature=signature) else: result["oper"] = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL + '/oper', method='GET', body='', signature=signature) return result
def operational(module, signature, result): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] vlan_num = module.params['vlan_num'] if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) if vlan_num: result["oper"] = axapi_call_v3(module, axapi_base_url + 'network/vlan/' + vlan_num + '/oper', method='GET', body='', signature=signature) else: result["oper"] = axapi_call_v3(module, axapi_base_url + 'network/vlan/oper', method='GET', body='', signature=signature) return result
def write_memory(module, signature): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature) if axapi_failure(result): axapi_close_session(module, signature) module.fail_json(msg="Failed to write config: %s" % result['response']['err']['msg'])
def axapi_close_session(module, signature): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] if axapi_version == '3': axapi_logoff_url = 'https://{}/axapi/v3/logoff/'.format(host) result = axapi_call_v3(module, axapi_logoff_url, method='POST', body='', signature=signature) elif axapi_version == '2.1': axapi_logoff_url = signature + '&method=session.close' result = axapi_call(module, axapi_logoff_url) if axapi_failure(result): module.fail_json(msg="Failed to close aXAPI session: %s" % result['response']['err']['msg'])
def change_partition(module, signature): host = module.params['a10_host'] partition = module.params['partition'] axapi_version = module.params['axapi_version'] if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + partition, method='POST', body='', signature=signature) if axapi_failure(result): axapi_close_session(module, signature) module.fail_json(msg="Failed to change partition: %s" % result['response']['err']['msg'])
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), server_name=dict(type='str', aliases=['server'], required=True), server_ip=dict(type='str', aliases=['ip', 'address'], required=True), server_status=dict(type='str', default='enable', aliases=['action'], choices=['enable', 'disable']), server_ports=dict(type='list', aliases=['port'], default=[]), ) ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=False ) host = module.params['host'] username = module.params['username'] password = module.params['password'] operation = module.params['operation'] write_config = module.params['write_config'] slb_server = module.params['server_name'] slb_server_ip = module.params['server_ip'] slb_server_status = module.params['server_status'] slb_server_ports = module.params['server_ports'] axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # validate the ports data structure validate_ports(module, slb_server_ports) json_post = { "server-list": [ { "name": slb_server, "host": slb_server_ip } ] } # add optional module parameters if slb_server_ports: json_post['server-list'][0]['port-list'] = slb_server_ports if slb_server_status: json_post['server-list'][0]['action'] = slb_server_status slb_server_data = axapi_call_v3(module, axapi_base_url+'slb/server/', method='GET', body='', signature=signature) # for empty slb server list if axapi_failure(slb_server_data): slb_server_exists = False else: slb_server_list = [server['name'] for server in slb_server_data['server-list']] if slb_server in slb_server_list: slb_server_exists = True else: slb_server_exists = False changed = False if operation == 'create': if slb_server_exists is False: result = axapi_call_v3(module, axapi_base_url+'slb/server/', method='POST', body=json.dumps(json_post), signature=signature) if axapi_failure(result): module.fail_json(msg="failed to create the server: %s" % result['response']['err']['msg']) changed = True else: module.fail_json(msg="server already exists, use state='update' instead") changed = False # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='GET', body='', signature=signature) else: result = slb_server_data elif operation == 'delete': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='DELETE', body='', signature=signature) if axapi_failure(result): module.fail_json(msg="failed to delete server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present") elif operation == 'update': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): module.fail_json(msg="failed to update server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present") # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url+'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)
def absent(module, signature, result): differences, config_before, json_post = diff_config(module, signature, result, status='absent') result['original_message'] = differences if differences == 1: result['msg'] = "Playbook's root element is not in the current config." elif differences == 2: result[ 'msg'] = "Playbook's config is entirely included in the current config." elif differences == 3: result[ 'msg'] = "Playbook's config is partially different from current config." elif differences == 4: result[ 'msg'] = "All playbook's config attributes are not in the current config." elif differences == 5: result[ 'msg'] = "Playbook indicates only the root element in the current config." result['post_config'] = json_post host = module.params['a10_host'] axapi_version = module.params['axapi_version'] vlan_num = module.params['vlan_num'] if axapi_version == '3': if differences == 2 or differences == 3: axapi_base_url = 'https://{}/axapi/v3/'.format(host) result_list = axapi_call_v3(module, axapi_base_url + 'network/vlan/' + str(vlan_num), method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to delete elemetns of VLAN: %s." % result_list) else: result["changed"] = True elif differences == 5: axapi_base_url = 'https://{}/axapi/v3/'.format(host) result_list = axapi_call_v3(module, axapi_base_url + 'network/vlan/' + str(vlan_num), method='DELETE', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to delete VLAN: %s." % result_list) else: result["changed"] = True else: result_list = copy.deepcopy(config_before) result['diff']['before'] = config_before result['diff']['after'] = result_list return result
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), service_group=dict(type='str', aliases=['service', 'pool', 'group'], required=True), service_group_protocol=dict(type='str', default='tcp', aliases=['proto', 'protocol'], choices=['tcp', 'udp']), service_group_lb_method=dict( type='str', required=False, aliases=['lb-method'], choices=[ 'dst-ip-hash', 'dst-ip-only-hash', 'fastest-response', 'least-request', 'src-ip-hash', 'src-ip-only-hash', 'weighted-rr', 'round-robin', 'round-robin-strict' ]), service_group_lc_method=dict( type='str', required=False, aliases=['lc-method'], choices=[ 'least-connection', 'service-least-connection', 'weighted-least-connection', 'service-weighted-least-connection' ]), servers=dict(type='list', aliases=['server', 'member'], default=[]), partition=dict(type='str', required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] partition = module.params['partition'] operation = module.params['operation'] write_config = module.params['write_config'] slb_service_group = module.params['service_group'] slb_service_group_proto = module.params['service_group_protocol'] slb_service_group_lb_method = module.params['service_group_lb_method'] slb_service_group_lc_method = module.params['service_group_lc_method'] slb_servers = module.params['servers'] # validate if service group name exists if slb_service_group is None: module.fail_json(msg='service_group is required') # validate the server list with using validate_servers validate_servers(module, slb_servers) # validate if there is both lb-method and lc-method if slb_service_group_lb_method and slb_service_group_lc_method: module.fail_json( msg= 'service_group_lb_method and service_group_lc_method are mutually exclusive' ) # Initialize JSON to be POST json_post = { "service-group": { "name": slb_service_group, "protocol": slb_service_group_proto, } } json_post_create = {"service-group-list": [{}]} # add optional module parameters to JSON if slb_servers: json_post['service-group']['member-list'] = slb_servers if slb_service_group_lb_method: json_post['service-group']['lb-method'] = slb_service_group_lb_method if slb_service_group_lc_method: json_post['service-group']['lc-method'] = slb_service_group_lc_method json_post_create['service-group-list'][0] = json_post['service-group'] # login A10 device and own signature axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # GET existing partition list and check if the partition indicated in the playbook exists if partition: partition_list = axapi_call_v3(module, axapi_base_url + 'partition/', method='GET', body='', signature=signature) if axapi_failure(partition_list): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="There is no partition on the device: %s" % (host)) else: partition_list = [ partition_attr['partition-name'] for partition_attr in partition_list['partition-list'] ] if partition in partition_list: result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + partition, method='POST', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="failed to create the service group: %s" % result['response']['err']['msg']) else: axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="The partition does not exist: %s" % (partition)) # GET existing service groups and check if the service group already exists slb_service_group_data = axapi_call_v3(module, axapi_base_url + 'slb/service-group/', method='GET', body='', signature=signature) if axapi_failure(slb_service_group_data): slb_service_group_exists = False else: slb_service_group_list = [ service_group['name'] for service_group in slb_service_group_data['service-group-list'] ] if slb_service_group in slb_service_group_list: slb_service_group_exists = True else: slb_service_group_exists = False # POST configuration changed = False if operation == 'create': if slb_service_group_exists is False: result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/', method='POST', body=json.dumps(json_post_create), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to create the service group: %s" % result['response']['err']['msg']) changed = True else: changed = False axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="service group already exists, use state='update' instead") # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method='GET', body='', signature=signature) else: result = slb_service_group_data elif operation == 'delete': if slb_service_group_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method='DELETE', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to delete service group: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the service group was not present") elif operation == 'update': if slb_service_group_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to update service group: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the service group was not present") # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)
def absent(module, signature, result): differences, config_before, json_post = diff_config(module, signature, result, status='absent') result['original_message'] = differences if differences == 1: result['msg'] = "Playbook's root element is not in the current config." elif differences == 2: result[ 'msg'] = "Playbook's config is entirely included in the current config." elif differences == 3: result[ 'msg'] = "Playbook's config is partially different from current config." elif differences == 4: result[ 'msg'] = "All playbook's config attributes are not in the current config." elif differences == 5: result[ 'msg'] = "Playbook indicates only the root element in the current config." result['post_config'] = json_post host = module.params['a10_host'] axapi_version = module.params['axapi_version'] if axapi_version == '3': if differences == 2 or differences == 3: axapi_base_url = 'https://{}/axapi/v3/'.format(host) mandatory_attributes_in_playbook = copy.deepcopy( MANDATORY_ATTRIBUTES.keys()) component_path = module.params[mandatory_attributes_in_playbook[0]] mandatory_attributes_in_playbook.pop(0) for mandatory_attribute_in_playbook in mandatory_attributes_in_playbook: component_path = component_path + '+' + module.params[ mandatory_attribute_in_playbook] result_list = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL + '/' + str(component_path), method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json( msg="Failed to delete elemetns of %s %s: %s." % (FIRST_LEVEL, SECOND_LEVEL, result_list)) else: if config_before != result_list: result["changed"] = True else: result["changed"] = False elif differences == 5: axapi_base_url = 'https://{}/axapi/v3/'.format(host) mandatory_attributes_in_playbook = copy.deepcopy( MANDATORY_ATTRIBUTES.keys()) component_path = module.params[mandatory_attributes_in_playbook[0]] mandatory_attributes_in_playbook.pop(0) for mandatory_attribute_in_playbook in mandatory_attributes_in_playbook: component_path = component_path + '+' + module.params[ mandatory_attribute_in_playbook] result_list = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL + '/' + str(component_path), method='DELETE', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to delete %s %s: %s." % (FIRST_LEVEL, SECOND_LEVEL, result_list)) else: result["changed"] = True else: result_list = copy.deepcopy(config_before) result['diff']['before'] = config_before result['diff']['after'] = result_list return result
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), virtual_server=dict(type='str', required=True), ip_address=dict(type='str', required=False), ipv6_address=dict(type='str', required=False), use_if_ip=dict(type='str', required=False, choises=['yes', 'no']), acl_name=dict(type='str', required=False), ipv6_acl=dict(type='str',required=False), enable_disable_action=dict(type='str', required=False, choices=['enable', 'disable', 'disable-when-all-ports-down', 'disable-when-any-port-down']), template_policy=dict(type='str', required=False), template_virtual_server=dict(type='str', required=False), vrid=dict(type='str', required=False), description=dict(type='str', required=False), port_list=dict(type='list', default=[]), partition=dict(type='str', required=False), ) ) module = AnsibleModule( argument_spec=argument_spec, supports_check_mode=False ) host = module.params['host'] username = module.params['username'] password = module.params['password'] partition = module.params['partition'] operation = module.params['operation'] write_config = module.params['write_config'] slb_virtual_server = module.params['virtual_server'] slb_virtual_server_ip_address = module.params['ip_address'] slb_virtual_server_ipv6_address = module.params['ipv6_address'] slb_virtual_server_use_if_ip = module.params['use_if_ip'] slb_virtual_server_acl_name = module.params['acl_name'] slb_virtual_server_ipv6_acl = module.params['ipv6_acl'] slb_virtual_server_enable_disable_action = module.params['enable_disable_action'] slb_virtual_server_template_policy = module.params['template_policy'] slb_virtual_server_template_virtual_server = module.params['template_virtual_server'] slb_virtual_server_vrid = module.params['vrid'] slb_virtual_server_description = module.params['description'] slb_virtual_server_port_list = module.params['port_list'] # validate if virtual-server name exists if slb_virtual_server is None: module.fail_json(msg="virtual-server name is required") # validate the server list with using validate_servers validate_port_list(module, slb_virtual_server_port_list) # validate if ip_address and ipv6_address and use_if_ip are exclusive if slb_virtual_server_ip_address: if slb_virtual_server_ipv6_address or slb_virtual_server_use_if_ip: module.fail_json(msg="ip_address and ipv6_address and use_if_ip are exclusive") elif slb_virtual_server_ipv6_address: if slb_virtual_server_use_if_ip: module.fail_json(msg="ip_address and ipv6_address and use_if_ip are exclusive") elif not slb_virtual_server_use_if_ip: module.fail_json(msg='One of ip_address or ipv6_address or use_if_ip should be defined') # Initialize JSON to be POST json_post = { "virtual-server": { "name": slb_virtual_server, } } json_post_create = { "virtual-server-list": [ { } ] } # add optional module parameters to JSON if slb_virtual_server_port_list: json_post['virtual-server']['port-list'] = slb_virtual_server_port_list if slb_virtual_server_ip_address: json_post['virtual-server']['ip-address'] = slb_virtual_server_ip_address if slb_virtual_server_ipv6_address: json_post['virtual-server']['ipv6-address'] = slb_virtual_server_ipv6_address if slb_virtual_server_use_if_ip: json_post['virtual-server']['use-if-ip'] = slb_virtual_server_use_if_ip if slb_virtual_server_acl_name: json_post['virtual-server']['acl-name'] = slb_virtual_server_acl_name if slb_virtual_server_ipv6_acl: json_post['virtual-server']['ipv6-acl'] = slb_virtual_server_ipv6_acl if slb_virtual_server_enable_disable_action: json_post['virtual-server']['enable-disable-action'] = slb_virtual_server_enable_disable_action if slb_virtual_server_template_policy: json_post['virtual-server']['template-policy'] = slb_virtual_server_template_policy if slb_virtual_server_template_virtual_server: json_post['virtual-server']['template-virtual-server'] = slb_virtual_server_template_virtual_server if slb_virtual_server_vrid: json_post['virtual-server']['vrid'] = slb_virtual_server_vrid if slb_virtual_server_description: json_post['virtual-server']['description'] = slb_virtual_server_description json_post_create['virtual-server-list'][0] = json_post['virtual-server'] # login A10 device and own signature axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # GET existing partition list and check if the partition indicated in the playbook exists if partition: partition_list = axapi_call_v3(module, axapi_base_url+'partition/', method='GET', body='', signature=signature) if axapi_failure(partition_list): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="There is no partition on the device: %s" % (host)) else: partition_list = [partition_attr['partition-name'] for partition_attr in partition_list['partition-list']] if partition in partition_list: result = axapi_call_v3(module, axapi_base_url+'active-partition/'+partition, method='POST', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to create the virtual server: %s" % result['response']['err']['msg']) else: axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="The partition does not exist: %s" % (partition)) # GET existing service groups and check if the service group already exists slb_virtual_server_data = axapi_call_v3(module, axapi_base_url+'slb/virtual-server/', method='GET', body='', signature=signature) if axapi_failure(slb_virtual_server_data): slb_virtual_server_exists = False else: slb_virtual_server_list = [virtual_server['name'] for virtual_server in slb_virtual_server_data['virtual-server-list']] if slb_virtual_server in slb_virtual_server_list: slb_virtual_server_exists = True else: slb_virtual_server_exists = False # POST configuration changed = False if operation == 'create': if slb_virtual_server_exists is False: result = axapi_call_v3(module, axapi_base_url+'slb/virtual-server/', method='POST', body=json.dumps(json_post_create), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to create the virtual server: %s" % result['response']['err']['msg']) changed = True else: changed = False axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="The virtual server already exists, use state='update' instead") # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual_server, method='GET', body='', signature=signature) else: result = slb_virtual_server_data elif operation == 'delete': if slb_virtual_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual_server, method='DELETE', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to delete the virtual server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="The virtual server was not present") elif operation == 'update': if slb_virtual_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual_server, method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to update the virtual server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="The virtual server was not present") # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url+'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict(partition=dict(type='str', required=False), operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), server_name=dict(type='str', aliases=['server'], required=True), server_ip=dict(type='str', aliases=['ip', 'address'], required=True), server_status=dict(type='str', default='enable', aliases=['action'], choices=['enable', 'disable']), template_server=dict(type='str', required=False), server_health_check_disable=dict(type='str', required=False, choises=['yes', 'no']), server_conn_limit=dict(type='str', required=False), server_weight=dict(type='str', required=False), server_slow_start=dict(type='str', required=False, choises=['yes', 'no']), server_ipv6_addr=dict(type='str', required=False), server_ports=dict(type='list', aliases=['port'], default=[]))) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] partition = module.params['partition'] operation = module.params['operation'] write_config = module.params['write_config'] slb_server = module.params['server_name'] slb_server_ip = module.params['server_ip'] slb_server_status = module.params['server_status'] slb_server_template_server = module.params['template_server'] slb_server_health_check_disable = module.params[ 'server_health_check_disable'] slb_server_conn_limit = module.params['server_conn_limit'] slb_server_weight = module.params['server_weight'] slb_server_slow_start = module.params['server_slow_start'] slb_server_ipv6_addr = module.params['server_ipv6_addr'] slb_server_ports = module.params['server_ports'] # validate the ports data structure validate_ports(module, slb_server_ports) # Initialize JSON to be POST json_post = {"server": {"name": slb_server, "host": slb_server_ip}} json_post_create = {"server-list": [{}]} # add optional module parameters if slb_server_status: json_post['server']['action'] = slb_server_status if slb_server_template_server: json_post['server']['template-server'] = slb_server_template_server if slb_server_health_check_disable: json_post['server'][ 'health-check-disable'] = slb_server_health_check_disable if slb_server_health_check_disable == 'True': json_post['server']['health-check-disable'] = 1 elif slb_server_health_check_disable == 'False': json_post['server']['health-check-disable'] = 0 else: module.fail_json( msg="Server health_check_disable shold be 'yes' or 'no'") if slb_server_conn_limit: json_post['server']['conn-limit'] = slb_server_conn_limit if slb_server_weight: json_post['server']['weight'] = slb_server_weight if slb_server_slow_start: json_post['server']['slow-start'] = slb_server_slow_start if slb_server_slow_start == 'True': json_post['server']['slow-start'] = 1 elif slb_server_slow_start == 'False': json_post['server']['slow-start'] = 0 else: module.fail_json(msg="Server slow_start shold be 'yes' or 'no'") if slb_server_ipv6_addr: json_post['server']['server-ipv6-addr'] = slb_server_ipv6_addr if slb_server_ports: json_post['server']['port-list'] = slb_server_ports json_post_create['server-list'][0] = json_post['server'] # login A10 device and own signature axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # GET existing partition list and check if the partition indicated in the playbook exists if partition: partition_list = axapi_call_v3(module, axapi_base_url + 'partition/', method='GET', body='', signature=signature) if axapi_failure(partition_list): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="There is no partition on the device: %s" % (host)) else: partition_list = [ partition_attr['partition-name'] for partition_attr in partition_list['partition-list'] ] if partition in partition_list: result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + partition, method='POST', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="failed to create the service group: %s" % result['response']['err']['msg']) else: axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="The partition does not exist: %s" % (partition)) # GET existing servers and check if the server already exits slb_server_data = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='GET', body='', signature=signature) if axapi_failure(slb_server_data): slb_server_exists = False else: slb_server_list = [ server['name'] for server in slb_server_data['server-list'] ] if slb_server in slb_server_list: slb_server_exists = True else: slb_server_exists = False # POST configuration changed = False if operation == 'create': if slb_server_exists is False: result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='POST', body=json.dumps(json_post_create), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to create the server: %s" % result['response']['err']['msg']) changed = True else: changed = False axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="server %s already exists, use state='update' instead" % (slb_server)) # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='GET', body='', signature=signature) else: result = slb_server_data elif operation == 'delete': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='DELETE', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to delete server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present: %s" % (slb_server)) elif operation == 'update': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to update server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present: %s" % (slb_server)) # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( state=dict(type='str', default='present', choices=['present', 'absent']), partition=dict(type='str', aliases=['partition', 'part'], required=False), file_name=dict(type='str', aliases=['filename'], required=False), cert_key_name=dict(type='str', required=False), file_type=dict(type='str', choices=['key', 'certificate', 'certificate/key'], required=False, default='certificate'), pfx_passwd=dict(type='str', required=False), method=dict(type='str', choices=['upload', 'download'], required=False), overwrite=dict(type='bool', default=False, required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] part = module.params['partition'] state = module.params['state'] file_name = module.params['file_name'] cert_key_name = module.params['cert_key_name'] file_type = module.params['file_type'] pfx_passwd = module.params['pfx_passwd'] method = module.params['method'] overwrite = module.params['overwrite'] if method and method != 'upload' and method != 'download': module.fail_json(msg="method must be one of 'upload' or 'download'") # authenticate axapi_base_url = 'http://%s/axapi/v3/' % host signature = axapi_authenticate_v3(module, axapi_base_url + 'auth', username, password) json_body = {} if file_type == "certificate": ext_url = 'ssl-cert' json_body = { "ssl-cert": { "certificate-type": "pem", "action": "import", "file": file_name, "file-handle": file_name } } elif file_type == "key": ext_url = 'ssl-key' json_body = { "ssl-key": { "action": "import", "file": file_name, "file-handle": file_name, "dst-file": file_name } } elif file_type == "certificate/key": ext_url = 'ssl-cert-key' # NOT WORKING YET # need to upload a .tar.gz file containing the key and cert json_body_search = { "ssl-cert-key": { "action": "check", "file": cert_key_name, "file-handle": cert_key_name } } if method == "upload": json_body = { "ssl-cert-key": { "action": "import", "file": file_name, "file-handle": file_name } } elif method == "download": json_body = { "ssl-cert-key": { "action": "export", "file": file_name, "file-handle": file_name, "dst-file": "blahdoo" } } # change partitions if we need to if part: part_change_result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + part, method="POST", signature=signature, body="") if (part_change_result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=part_change_result['response']['err']['msg']) # clear the 'changed' flag changed = False msg = "" # does the cert exist on the device already cert_found_on_device = False # check if the SSL cert/key exists on the device # for a cert/key we need to issue a POST and not a GET if file_type == "certificate/key": # result = axapi_call_v3(module, axapi_base_url + 'file/' + ext_url, method="POST", body=json_body_search, signature=signature) # there is no way to check for key/cert together so we must make two separate calls to validate result_cert_lookup = axapi_call_v3(module, axapi_base_url + 'file/ssl-cert/' + cert_key_name, method="GET", signature=signature) result_key_lookup = axapi_call_v3(module, axapi_base_url + 'file/ssl-key/' + cert_key_name, method="GET", signature=signature) if ('response' in result_cert_lookup and result_cert_lookup['response']['status'] == 'fail' and (result_cert_lookup['response']['code'] == 404 or result_cert_lookup['response']['code'] == 400) or \ 'response' in result_key_lookup and result_key_lookup['response']['status'] == 'fail' and (result_key_lookup['response']['code'] == 404 or result_key_lookup['response']['code'] == 400)): cert_found_on_device = False elif 'response' in result_cert_lookup and result_cert_lookup[ 'response']['status'] == 'fail' and result_cert_lookup[ 'response']['code'] != 404 and result_cert_lookup[ 'response']['code'] != 400: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) elif 'response' in result_cert_lookup and result_cert_lookup[ 'response']['status'] == 'fail' and result_key_lookup[ 'response']['code'] != 404 and result_key_lookup[ 'response']['code'] != 400: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) else: cert_found_on_device = True # file_name = "bundle.tar.gz" else: result = axapi_call_v3(module, axapi_base_url + 'file/' + ext_url + '/' + file_name, method="GET", signature=signature) if ('response' in result and result['response']['status'] == 'fail'): if (result['response']['code'] == 404 or result['response']['code'] == 400): cert_found_on_device = False else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) else: cert_content = result['response']['data'] cert_found_on_device = True if state == 'present': # adding the SSL cert if (method == "upload" and cert_found_on_device and overwrite) or \ (method == "upload" and not cert_found_on_device): # make sure the file being uploaded exists locally if os.path.isfile(file_name) is False: # log out of the session nicely and exit with an error logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg='File not found ' + file_name) else: try: result = uploadSSL(axapi_base_url + 'file/' + ext_url, 'upload', file_type, pfx_passwd, file_name, json.dumps(json_body), signature=signature) except Exception, e: # log out of the session nicely and exit with an error #err_result = e['changed'] logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=e) if axapi_failure(result): # log out of the session nicely and exit with an error logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg="failed to upload the cert: %s" % result['response']['err']['msg']) changed = True elif method == "download" and (cert_found_on_device or file_type == "certificate/key"): saveFile(file_name, cert_content) elif method == "download" and not cert_found_on_device: module.fail_json(msg="cert not found") elif cert_found_on_device and not overwrite: msg = "certificate/key found on device and not overwritten"
elif method == "download" and not cert_found_on_device: module.fail_json(msg="cert not found") elif cert_found_on_device and not overwrite: msg = "certificate/key found on device and not overwritten" elif state == 'absent': # removal of SSL cert if cert_found_on_device: if file_type == 'certificate': result = axapi_call_v3( module, 'https://%s/axapi/v3/pki/delete' % host, method="POST", signature=signature, body={"delete": { "cert-name": file_name }}) elif file_type == 'key': result = axapi_call_v3( module, 'https://%s/axapi/v3/pki/delete' % host, method="POST", signature=signature, body={"delete": { "private-key": file_name }}) else: # two calls have to be made to delete the private key and the public key result = axapi_call_v3(
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( state=dict(type='str', default='present', choices=['present', 'absent']), partition=dict(type='str', aliases=['partition', 'part']), service_group=dict(type='str', aliases=['service', 'pool', 'group'], required=True), service_group_protocol=dict(type='str', default='tcp', aliases=['proto', 'protocol'], choices=['tcp', 'udp']), service_group_method=dict( type='str', default='round-robin', aliases=['method'], choices=[ 'round-robin', 'weighted-rr', 'least-connection', 'weighted-least-connection', 'service-least-connection', 'service-weighted-least-connection', 'fastest-response', 'least-request', 'round-robin-strict', 'src-ip-only-hash', 'src-ip-hash' ]), servers=dict(type='list', aliases=['server', 'member'], default=[]), health_monitor=dict(type='str', aliases=['hm']), reset_on_server_selection_fail=dict(type='bool', default=False), overwrite=dict(type='bool', default=False, required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] part = module.params['partition'] state = module.params['state'] write_config = module.params['write_config'] slb_service_group = module.params['service_group'] slb_service_group_proto = module.params['service_group_protocol'] slb_service_group_method = module.params['service_group_method'] slb_servers = module.params['servers'] slb_health_monitor = module.params['health_monitor'] slb_reset_on_server_selection_fail = module.params[ 'reset_on_server_selection_fail'] overwrite = module.params['overwrite'] if slb_service_group is None: module.fail_json(msg='service_group is required') axapi_base_url = 'http://%s/axapi/v3/' % host # build the JSON message structure json_post = { 'service-group': { 'name': slb_service_group, 'protocol': slb_service_group_proto, 'lb-method': slb_service_group_method, 'reset-on-server-selection-fail': slb_reset_on_server_selection_fail, 'member-list': [] } } if slb_health_monitor: json_post['service_group']['health_monitor'] = slb_health_monitor # first we authenticate to get a session id signature = axapi_authenticate_v3(module, axapi_base_url + 'auth', username, password) # change partitions if we need to if part: result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + part, method="POST", signature=signature, body="") if (result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) # validate that if the health monitor has been passed in, it exists on the system already if slb_health_monitor: result_hm = axapi_call_v3( module, axapi_base_url + 'health/monitor/' + slb_health_monitor) if ('response' in result_hm and result_hm['response']['status'] == 'fail'): # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result_hm['response']['err']['msg']) # then we check to see if the specified service group exists result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method="GET", signature=signature) if ('response' in result and result['response']['status'] == 'fail'): if (result['response']['code'] == 404): slb_service_group_exist = False else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) else: # sg_content = result['response']['data'] slb_service_group_exist = True # clear the 'changed' flag changed = False msg = "" if state == 'present': # before creating/updating we need to validate that servers # defined in the servers list exist to prevent errors server_exist = True for server in slb_servers: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + server['name'], method="GET", signature=signature) # did the lookup of the server return some error if ('response' in result and result['response']['status'] == 'fail'): if (result['response']['code'] == 404): server_exist = False break else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) # add server to the member-list json_post['service-group']['member-list'].append(server) if not server_exist: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg="server %s does not exist" % server['name']) if slb_service_group_exist and not overwrite: # just exit gracefully with a message msg = 'service-group exists but not modified' elif slb_service_group_exist and overwrite: # overwrite the properties of the service group result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method="PUT", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "service group %s updated" % slb_service_group elif not slb_service_group_exist: # create a new server result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/', method="POST", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "service-group %s created" % slb_service_group elif state == 'absent': if slb_service_group_exist: result = axapi_call_v3(module, axapi_base_url + 'slb/service-group/' + slb_service_group, method="DELETE", signature=signature) changed = True else: msg = "the service group was not present" changed = False # if the config has changed, save the config unless otherwise requested if changed and write_config: result = axapi_call_v3(module, axapi_base_url + 'write/memory', method="POST", signature=signature) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) # log out of the session nicely and exit result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.exit_json(changed=changed, content=result, msg=msg)
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( state=dict(type='str', default='present', choices=['present', 'absent']), partition=dict(type='str', aliases=['partition', 'part']), virtual_server=dict(type='str', aliases=['vip', 'virtual'], required=True), virtual_server_ip=dict(type='str', aliases=['ip', 'address'], required=True), virtual_server_status=dict(type='str', default='enabled', aliases=['status'], choices=['enabled', 'disabled']), disable_vserver_on_condition=dict( type='str', choices=[ 'enable', 'disable', 'disable-when-all-ports-down', 'disable-when-any-port-down' ], required=False, default='enable'), redistribution_flagged=dict(type='int', choices=[0, 1, 2], required=False, default=0), acl_id=dict(type='str', required=False, default=None), acl_name=dict(type='str', required=False, default=None), virtual_server_ports=dict(type='list', required=True), overwrite=dict(type='bool', default=False, required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] part = module.params['partition'] state = module.params['state'] write_config = module.params['write_config'] slb_virtual = module.params['virtual_server'] slb_virtual_ip = module.params['virtual_server_ip'] slb_virtual_status = module.params['virtual_server_status'] slb_virtual_ports = module.params['virtual_server_ports'] redistribution_flagged = module.params['redistribution_flagged'] acl_id = module.params['acl_id'] acl_name = module.params['acl_name'] disable_vserver_on_condition = module.params[ 'disable_vserver_on_condition'] overwrite = module.params['overwrite'] # check mandatory fields if slb_virtual is None: module.fail_json(msg='virtual_server is required') axapi_base_url = 'http://%s/axapi/v3/' % host # first we authenticate to get a session id signature = axapi_authenticate_v3(module, axapi_base_url + 'auth', username, password) # change partitions if we need to if part: result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + part, method="POST", signature=signature, body="") if (result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) json_post = { 'virtual-server': { 'name': slb_virtual, 'enable-disable-action': axapi_enabled_disabled(slb_virtual_status), 'port-list': slb_virtual_ports, 'enable-disable-action': disable_vserver_on_condition, 'redistribution-flagged': redistribution_flagged, } } # is this an IPv4 or IPv6 VIP? if "::" in slb_virtual_ip or len(slb_virtual_ip) > 16: ip_address_field = "ipv6-address" acl_name_field = "ipv6-acl" else: ip_address_field = "ip-address" acl_name_field = "acl-name" # if acl id or acl name was passed in bind it to the vip, otherwise assign the ip address passed in if acl_id or acl_name: if acl_id: json_post['virtual-server']['acl-id'] = acl_id else: json_post['virtual-server'][acl_name_field] = acl_name # else: json_post['virtual-server'][ip_address_field] = slb_virtual_ip result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual, method="GET", signature=signature) if ('response' in result and result['response']['status'] == 'fail'): if (result['response']['code'] == 404): slb_virtual_exists = False else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) else: slb_virtual_exists = True # clear the 'changed' flag changed = False msg = "" if state == 'present': if slb_virtual_exists and not overwrite: msg = 'virtual server exists but not modified' elif slb_virtual_exists and overwrite: # overwrite the properties of the virtual server result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual, method="PUT", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "virtual server %s updated" % slb_virtual elif not slb_virtual_exists: # create a new server result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/', method="POST", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "virtual server %s created" % slb_virtual elif state == 'absent': if slb_virtual_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/virtual-server/' + slb_virtual, method="DELETE", signature=signature) changed = True else: changed = False msg = "the virtual server was not present" # if the config has changed, save the config unless otherwise requested if changed and write_config: result = axapi_call_v3(module, axapi_base_url + 'write/memory', method="POST", signature=signature) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) # log out of the session nicely and exit result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.exit_json(changed=changed, content=result, msg=msg)
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( state=dict(type='str', default='present', choices=['present', 'absent']), partition=dict(type='str', aliases=['partition', 'part'], required=False), server_name=dict(type='str', aliases=['server'], required=True), server_ip=dict(type='str', aliases=['ip', 'address']), server_action=dict(type='str', default='enable', aliases=['status'], choices=['enable', 'disable']), server_ports=dict(type='list', aliases=['port'], default=[]), server_hm=dict(type='str', aliases=['health_monitor']), overwrite=dict(type='bool', default=False, required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) host = module.params['host'] username = module.params['username'] password = module.params['password'] part = module.params['partition'] state = module.params['state'] write_config = module.params['write_config'] slb_server = module.params['server_name'] slb_server_ip = module.params['server_ip'] slb_server_action = module.params['server_action'] slb_server_ports = module.params['server_ports'] slb_server_hm = module.params['server_hm'] overwrite = module.params['overwrite'] if slb_server is None: module.fail_json(msg='server_name is required') axapi_base_url = 'http://%s/axapi/v3/' % host signature = axapi_authenticate_v3(module, axapi_base_url + 'auth', username, password) # change partitions if we need to if part: part_change_result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + part, method="POST", signature=signature, body="") if (part_change_result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=part_change_result['response']['err']['msg']) # is this server an ipv4 or ipv6 server if "::" in slb_server_ip or len(slb_server_ip) > 16: ip_address_field = "server-ipv6-addr" else: ip_address_field = "host" # create the JSON object containing the parameters json_post = { 'server': { 'name': slb_server, } } # add optional module parameters if slb_server_ip: json_post['server'][ip_address_field] = slb_server_ip if slb_server_ports: json_post['server']['port-list'] = slb_server_ports if slb_server_hm: json_post['server']['health-check'] = slb_server_hm if slb_server_action: json_post['server']['action'] = slb_server_action # rsp, info = fetch_url(module, axapi_base_url + 'slb/server/' + slb_server, method='GET', data=json.dumps(None), headers={'content-type': 'application/json', 'Authorization': 'A10 %s' % signature}) slb_server_data = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method="GET", signature=signature) if ('response' in slb_server_data and slb_server_data['response']['status'] == 'fail'): if (slb_server_data['response']['code'] == 404): server_exists = False else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=slb_server_data['response']['err']['msg']) else: #server_content = slb_server_data['response']['data'] server_content = slb_server_data server_exists = True changed = False msg = "" # server is being added/modified if state == 'present': if server_exists and not overwrite: # just exit gracefully with a message msg = 'server exists but not modified' elif server_exists and overwrite: # overwrite the properties of the server result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method="PUT", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "server %s updated" % slb_server elif not server_exists: # create a new server result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method="POST", signature=signature, body=json_post) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) changed = True msg = "server %s created" % slb_server elif state == 'absent': if server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method="DELETE", signature=signature) changed = True else: changed = False msg = "the server was not present" # if the config has changed, save the config unless otherwise requested if changed and write_config: result = axapi_call_v3(module, axapi_base_url + 'write/memory', method="POST", signature=signature) if ('response' in result and 'err' in result['response']): logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=result['response']['err']['msg']) # log out of the session nicely and exit result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.exit_json(changed=changed, content=result, msg=msg)
def diff_config(module, signature, result, status): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] # Initialize return values differences = 0 config_before = {} if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) result_list = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL, method='GET', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to obtain current %s setup %s." % (FIRST_LEVEEL, result_list)) else: component_list = [] if result_list[FIRST_LEVEL].has_key(SECOND_LEVEL_LIST): result_list = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL, method='GET', body='', signature=signature) for config_list in result_list[SECOND_LEVEL_LIST]: mandatory_attributes_in_config = [] for mandatory_attribute_in_config in MANDATORY_ATTRIBUTES.values( ): mandatory_attributes_in_config.append( config_list[mandatory_attribute_in_config]) component_list.append(mandatory_attributes_in_config) else: result_list = {SECOND_LEVEL: {}} config_before = copy.deepcopy(result_list) mandatory_attributes_in_playbook = [] for mandatory_attribute_in_playbook in MANDATORY_ATTRIBUTES.keys(): if not (module.params[mandatory_attribute_in_playbook] is None): mandatory_attributes_in_playbook.append( module.params[mandatory_attribute_in_playbook]) if mandatory_attributes_in_playbook in component_list: component_path = mandatory_attributes_in_playbook[0] mandatory_attributes_in_playbook.pop(0) for mandatory_attribute_in_playbook in mandatory_attributes_in_playbook: component_path = component_path + '+' + mandatory_attribute_in_playbook result_list = axapi_call_v3(module, axapi_base_url + FIRST_LEVEL + '/' + SECOND_LEVEL + '/' + str(component_path), method='GET', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json( msg="Failed to obtain %s %s %s information." % (FIRST_LEVEL, SECOND_LEVEL, component_path)) else: config_before = copy.deepcopy(result_list) json_post = copy.deepcopy(result_list) diff_sw = False same_sw = False absent_sw = False for playbook_attribute in COMPONENT_ATTRIBUTES.keys(): if not (module.params[playbook_attribute] is None): if result_list[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES[playbook_attribute]): if result_list[SECOND_LEVEL][COMPONENT_ATTRIBUTES[ playbook_attribute]] != module.params[ playbook_attribute]: diff_sw = True else: same_sw = True if status == 'absent': json_post[SECOND_LEVEL].pop( COMPONENT_ATTRIBUTES[ playbook_attribute]) else: absent_sw = True if status == 'present': for mutually_exclusive_list in MUTUALLY_EXCLUSIVE_ATTRIBUTES_SET: if playbook_attribute in mutually_exclusive_list: mutually_exclusive_list.remove( playbook_attribute) for current_attribute_removed in mutually_exclusive_list: if json_post[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ): json_post[SECOND_LEVEL].pop( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ) json_post[SECOND_LEVEL][COMPONENT_ATTRIBUTES[ playbook_attribute]] = module.params[ playbook_attribute] for playbook_attribute in COMPONENT_ATTRIBUTES_BOOLEAN.keys( ): if not (module.params[playbook_attribute] is None): if result_list[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES_BOOLEAN[ playbook_attribute]): if result_list[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_BOOLEAN[ playbook_attribute]] != module.params[ playbook_attribute]: diff_sw = True else: same_sw = True if status == 'absent': json_post[SECOND_LEVEL].pop( COMPONENT_ATTRIBUTES_BOOLEAN[ playbook_attribute]) else: absent_sw = True if status == 'present': for mutually_exclusive_list in MUTUALLY_EXCLUSIVE_ATTRIBUTES_SET: if playbook_attribute in mutually_exclusive_list: mutually_exclusive_list.remove( playbook_attribute) for current_attribute_removed in mutually_exclusive_list: if json_post[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ): json_post[SECOND_LEVEL].pop( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ) json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_BOOLEAN[ playbook_attribute]] = module.params[ playbook_attribute] for playbook_attribute in COMPONENT_ATTRIBUTES_LIST.keys(): if not (module.params[playbook_attribute] is None): if result_list[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]): json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]] = [] current_lists = copy.deepcopy( result_list[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]]) playbook_lists = copy.deepcopy( module.params[playbook_attribute]) current_lists_rest = copy.deepcopy( current_lists) playbook_lists_rest = copy.deepcopy( playbook_lists) for playbook_list in playbook_lists: for current_list in current_lists: current_list_mandatory_values = [] current_list_options = copy.deepcopy( current_list) playbook_list_mandatory_values = [] playbook_list_options = copy.deepcopy( playbook_list) json_post_list = copy.deepcopy( current_list) if COMPONENT_ATTRIBUTES_LIST_MANDATORIES.has_key( playbook_attribute): for list_mandatory_key in COMPONENT_ATTRIBUTES_LIST_MANDATORIES[ playbook_attribute]: current_list_mandatory_values.append( current_list[ list_mandatory_key]) current_list_options.pop( list_mandatory_key) playbook_list_mandatory_values.append( playbook_list[ list_mandatory_key]) playbook_list_options.pop( list_mandatory_key) if set( current_list_mandatory_values ) == set(playbook_list_mandatory_values ): if playbook_list_options != {}: playbook_options_included = True if list( set(playbook_list_options ) - set(current_list_options )) != []: playbook_options_included = False for playbook_list_option_key in playbook_list_options.keys( ): if current_list_options.has_key( playbook_list_option_key ): if playbook_list_options[ playbook_list_option_key] != current_list_options[ playbook_list_option_key]: playbook_options_included = False if playbook_options_included: same_sw = True else: diff_sw = True if status == 'present': for playbook_list_key in playbook_list.keys( ): json_post_list[ playbook_list_key] = playbook_list[ playbook_list_key] if status == 'absent': if playbook_list_options != []: for playbook_list_key in playbook_list.keys( ): if (json_post_list[ playbook_list_key] == playbook_list[ playbook_list_key] ) and not ( playbook_list_key in COMPONENT_ATTRIBUTES_LIST_MANDATORIES[ playbook_attribute] ): json_post_list.pop( playbook_list_key ) json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]].append( json_post_list) else: if status == 'absent': diff_sw = True if status == 'present': json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]].append( current_list) current_lists_rest.remove( current_list) playbook_lists_rest.remove( playbook_list) if current_lists_rest != []: for current_list in current_lists_rest: json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]].append( current_list) if playbook_lists_rest != []: absent_sw = True if status == 'present': for playbook_list in playbook_lists_rest: json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]].append( playbook_list) else: absent_sw = True if status == 'present': for mutually_exclusive_list in MUTUALLY_EXCLUSIVE_ATTRIBUTES_SET: if playbook_attribute in mutually_exclusive_list: mutually_exclusive_list.remove( playbook_attribute) for current_attribute_removed in mutually_exclusive_list: if json_post[SECOND_LEVEL].has_key( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ): json_post[SECOND_LEVEL].pop( COMPONENT_ATTRIBUTES_ALL[ current_attribute_removed] ) json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]] = module.params[ playbook_attribute] if absent_sw and not (diff_sw) and not (same_sw): differences = 4 elif not (absent_sw) and not (diff_sw) and not (same_sw): differences = 5 elif not (absent_sw) and not (diff_sw) and same_sw: differences = 2 else: differences = 3 else: #there is no existing SECOND_LEVEL component in the current config differences = 1 if status == 'present': json_post = {SECOND_LEVEL: {}} for playbook_attribute in MANDATORY_ATTRIBUTES.keys(): if not (module.params[playbook_attribute] is None): json_post[SECOND_LEVEL][MANDATORY_ATTRIBUTES[ playbook_attribute]] = module.params[ playbook_attribute] for playbook_attribute in COMPONENT_ATTRIBUTES.keys(): if not (module.params[playbook_attribute] is None): json_post[SECOND_LEVEL][COMPONENT_ATTRIBUTES[ playbook_attribute]] = module.params[ playbook_attribute] for playbook_attribute in COMPONENT_ATTRIBUTES_BOOLEAN.keys( ): if not (module.params[playbook_attribute] is None): json_post[SECOND_LEVEL][ COMPONENT_ATTRIBUTES_BOOLEAN[ playbook_attribute]] = module.params[ playbook_attribute] for playbook_attribute in COMPONENT_ATTRIBUTES_LIST.keys(): if not (module.params[playbook_attribute] is None): json_post[SECOND_LEVEL][COMPONENT_ATTRIBUTES_LIST[ playbook_attribute]] = module.params[ playbook_attribute] elif status == 'absent': json_post = {} return differences, config_before, json_post
elif method == "download" and not aflex_exists: result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg="aflex cannot be found the device") elif state == 'absent': # does the aflex exist on the load balancer if aflex_exists: result = axapi_call_v3( module, axapi_base_url + 'file/aflex', method="POST", signature=signature, body={"aflex": { "file": file_name, "action": "delete" }}) if ('response' in result and result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") #module.fail_json(msg=result['response']['err']['msg']) module.fail_json(msg=result['response'])
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( state=dict(type='str', default='present', choices=['present', 'absent']), partition=dict(type='str', aliases=['partition', 'part'], required=False), file_name=dict(type='str', aliases=['filename'], required=False), method=dict(type='str', choices=['upload', 'download'], required=False), overwrite=dict(type='bool', default=False, required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] part = module.params['partition'] state = module.params['state'] file_name = module.params['file_name'] method = module.params['method'] overwrite = module.params['overwrite'] if method and method != 'upload' and method != 'download': module.fail_json(msg="method must be one of 'upload' or 'download'") # authenticate axapi_base_url = 'https://%s/axapi/v3/' % host signature = axapi_authenticate_v3(module, axapi_base_url + 'auth', username, password) # change partitions if we need to if part: part_change_result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + part, method="POST", signature=signature, body="") if (part_change_result['response']['status'] == 'fail'): # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=part_change_result['response']['err']['msg']) # look for the aflex script on the device aflex_data = axapi_call_v3(module, axapi_base_url + 'file/aflex/' + file_name, method="GET", signature=signature) aflex_content = "" if ('response' in aflex_data and aflex_data['response']['status'] == 'fail'): if (aflex_data['response']['code'] == 404): aflex_exists = False else: logoff_result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=aflex_data['response']['err']['msg']) else: aflex_content = aflex_data['response']['data'] aflex_exists = True changed = False if state == 'present': if (method == "upload" and aflex_exists and overwrite) or (method == "upload" and not aflex_exists): if os.path.isfile(file_name) is False: # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg='File does not exist ' + file_name) else: try: result = uploadAflex(axapi_base_url + 'file/aflex', file_name, file_name, signature=signature) except Exception, e: # log out of the session nicely and exit with an error #err_result = e['changed'] result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg=e) if axapi_failure(result): # log out of the session nicely and exit with an error result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg="failed to upload the aflex: %s" % result['response']['err']['msg']) changed = True elif method == "download" and aflex_exists: saveFile(file_name, aflex_content) elif method == "download" and not aflex_exists: result = axapi_call_v3(module, axapi_base_url + 'logoff', method="POST", signature=signature, body="") module.fail_json(msg="aflex cannot be found the device")
def diff_config(module, signature, result, status): host = module.params['a10_host'] axapi_version = module.params['axapi_version'] vlan_num = module.params['vlan_num'] name = module.params['name'] user_tag = module.params['user_tag'] shared_vlan = module.params['shared_vlan'] ve = module.params['ve'] tagged_eth_list = module.params['tagged_eth_list'] tagged_trunk_list = module.params['tagged_trunk_list'] untagged_eth_list = module.params['untagged_eth_list'] untagged_trunk_list = module.params['untagged_trunk_list'] untagged_lif = module.params['untagged_lif'] # Initialize return values differences = 0 config_before = {} if axapi_version == '3': axapi_base_url = 'https://{}/axapi/v3/'.format(host) result_list = axapi_call_v3(module, axapi_base_url + 'network', method='GET', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to obtain current network setup %s." % result_list) else: if result_list['network'].has_key('vlan-list'): result_list = axapi_call_v3(module, axapi_base_url + 'network/vlan', method='GET', body='', signature=signature) vlan = [vlan['vlan-num'] for vlan in result_list['vlan-list']] else: result_list = {"vlan": {}} vlan = [] config_before = copy.deepcopy(result_list) if vlan_num in vlan: result_list = axapi_call_v3(module, axapi_base_url + 'network/vlan/' + str(vlan_num), method='GET', body='', signature=signature) if axapi_failure(result_list): axapi_close_session(module, signature) module.fail_json(msg="Failed to obtain vlan information.") else: config_before = copy.deepcopy(result_list) json_post = copy.deepcopy(result_list) # json_post['vlan'].pop('uuid') # json_post['vlan'].pop('a10-url') diff_sw = 0 same_sw = 0 absent_sw = 0 if name: if result_list['vlan'].has_key('name'): if result_list['vlan']['name'] != name: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('name') else: absent_sw = 1 if status == 'present': json_post['vlan']['name'] = name if user_tag: if result_list['vlan'].has_key('user-tag'): if result_list['vlan']['user-tag'] != user_tag: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('user-tag') else: absent_sw = 1 if status == 'present': json_post['vlan']['user-tag'] = user_tag if int(shared_vlan): if result_list['vlan'].has_key('shared-vlan'): if result_list['vlan'][ 'shared-vlan'] != shared_vlan: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('shared-vlan') else: absent_sw = 1 if status == 'present': json_post['vlan']['shared-vlan'] = shared_vlan if ve: if result_list['vlan'].has_key('ve'): if result_list['vlan']['ve'] != ve: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('ve') else: absent_sw = 1 if status == 'present': json_post['vlan']['ve'] = ve if tagged_eth_list: if result_list['vlan'].has_key('tagged-eth-list'): if result_list['vlan'][ 'tagged-eth-list'] != tagged_eth_list: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('tagged-eth-list') else: absent_sw = 1 if status == 'present': json_post['vlan'][ 'tagged-eth-list'] = tagged_eth_list if tagged_trunk_list: if result_list['vlan'].has_key('tagged-trunk-list'): if result_list['vlan'][ 'tagged-trunk-list'] != tagged_trunk_list: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('tagged-trunk-list') else: absent_sw = 1 if status == 'present': json_post['vlan'][ 'tagged-trunk-list'] = tagged_trunk_list if untagged_eth_list: if result_list['vlan'].has_key('untagged-eth-list'): if result_list['vlan'][ 'untagged-eth-list'] != untagged_eth_list: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('untagged-eth-list') else: absent_sw = 1 if status == 'present': json_post['vlan'][ 'untagged-eth-list'] = untagged_eth_list if untagged_trunk_list: if result_list['vlan'].has_key('untagged-trunk-list'): if result_list['vlan'][ 'untagged-trunk-list'] != untagged_trunk_list: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop( 'untagged-trunk-list') else: absent_sw = 1 if status == 'present': json_post['vlan'][ 'untagged-trunk-list'] = untagged_trunk_list if untagged_lif: if result_list['vlan'].has_key('untagged-lif'): if result_list['vlan'][ 'untagged-lif'] != untagged_lif: diff_sw = 1 else: same_sw = 1 if status == 'absent': json_post['vlan'].pop('untagged-lif') else: absent_sw = 1 if status == 'present': json_post['vlan']['untagged-lif'] = untagged_lif if absent_sw and not (diff_sw) and not (same_sw): differences = 4 elif diff_sw: differences = 3 elif same_sw and not (diff_sw): differences = 2 else: differences = 5 else: #there is no existing vlan whose number is vlan-num differences = 1 if status == 'present': json_post = {"vlan": {"vlan-num": vlan_num}} if name: json_post['vlan']['name'] = name if user_tag: json_post['vlan']['user-tag'] = user_tag if int(shared_vlan): json_post['vlan']['shared-vlan'] = shared_vlan if ve: json_post['vlan']['ve'] = ve if tagged_eth_list: json_post['vlan']['tagged-eth-list'] = tagged_eth_list if tagged_trunk_list: json_post['vlan'][ 'tagged-trunk-list'] = tagged_trunk_list if untagged_eth_list: json_post['vlan'][ 'untagged-eth-list'] = untagged_eth_list if untagged_trunk_list: json_post['vlan'][ 'untagged-trunk-list'] = untagged_trunk_list if untagged_lif: json_post['vlan']['untagged-lif'] = untagged_lif elif status == 'absent': json_post = {} return differences, config_before, json_post
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), server_name=dict(type='str', aliases=['server'], required=True), server_ip=dict(type='str', aliases=['ip', 'address'], required=True), server_status=dict(type='str', default='enable', aliases=['action'], choices=['enable', 'disable']), server_ports=dict(type='list', aliases=['port'], default=[]), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] operation = module.params['operation'] write_config = module.params['write_config'] slb_server = module.params['server_name'] slb_server_ip = module.params['server_ip'] slb_server_status = module.params['server_status'] slb_server_ports = module.params['server_ports'] axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # validate the ports data structure validate_ports(module, slb_server_ports) json_post = {"server-list": [{"name": slb_server, "host": slb_server_ip}]} # add optional module parameters if slb_server_ports: json_post['server-list'][0]['port-list'] = slb_server_ports if slb_server_status: json_post['server-list'][0]['action'] = slb_server_status slb_server_data = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='GET', body='', signature=signature) # for empty slb server list if axapi_failure(slb_server_data): slb_server_exists = False else: slb_server_list = [ server['name'] for server in slb_server_data['server-list'] ] if slb_server in slb_server_list: slb_server_exists = True else: slb_server_exists = False changed = False if operation == 'create': if slb_server_exists == False: result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='POST', body=json.dumps(json_post), signature=signature) if axapi_failure(result): module.fail_json(msg="failed to create the server: %s" % result['response']['err']['msg']) changed = True else: module.fail_json( msg="server already exists, use state='update' instead") changed = False # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='GET', body='', signature=signature) else: result = slb_server_data elif operation == 'delete': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/' + slb_server, method='DELETE', body='', signature=signature) if axapi_failure(result): module.fail_json(msg="failed to delete server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present") elif operation == 'update': if slb_server_exists: result = axapi_call_v3(module, axapi_base_url + 'slb/server/', method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): module.fail_json(msg="failed to update server: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the server was not present") # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)
def main(): argument_spec = a10_argument_spec() argument_spec.update(url_argument_spec()) argument_spec.update( dict( partition=dict(type='str', required=False), operation=dict(type='str', default='create', choices=['create', 'update', 'delete']), pool_name=dict(type='str', required=True), start_address=dict(type='str', required=True), end_address=dict(type='str', required=True), netmask=dict(type='str', required=True), gateway=dict(type='str', required=False), )) module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=False) host = module.params['host'] username = module.params['username'] password = module.params['password'] partition = module.params['partition'] operation = module.params['operation'] write_config = module.params['write_config'] ip_nat_pool_name = module.params['pool_name'] ip_nat_pool_start_address = module.params['start_address'] ip_nat_pool_end_address = module.params['end_address'] ip_nat_pool_netmask = module.params['netmask'] ip_nat_pool_gateway = module.params['gateway'] # Initialize JSON to be POST json_post = { "pool": { "pool-name": ip_nat_pool_name, "start-address": ip_nat_pool_start_address, "end-address": ip_nat_pool_end_address, "netmask": ip_nat_pool_netmask } } json_post_create = {"pool-list": [{}]} if ip_nat_pool_gateway: json_post['pool']['gateway'] = ip_nat_pool_gateway json_post_create['pool-list'][0] = json_post['pool'] # module.fail_json(msg="JSON file is %s" % (json_post)) # login A10 device and own signature axapi_base_url = 'https://{}/axapi/v3/'.format(host) axapi_auth_url = axapi_base_url + 'auth/' signature = axapi_authenticate_v3(module, axapi_auth_url, username, password) # GET existing partition list and check if the partition indicated in the playbook exists if partition: partition_list = axapi_call_v3(module, axapi_base_url + 'partition/', method='GET', body='', signature=signature) if axapi_failure(partition_list): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="There is no partition on the device: %s" % (host)) else: partition_list = [ partition_attr['partition-name'] for partition_attr in partition_list['partition-list'] ] if partition in partition_list: result = axapi_call_v3(module, axapi_base_url + 'active-partition/' + partition, method='POST', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="failed to create the service group: %s" % result['response']['err']['msg']) else: axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="The partition does not exist: %s" % (partition)) # GET existing servers and check if the server already exits ip_nat_pool_data = axapi_call_v3(module, axapi_base_url + 'ip/nat/pool', method='GET', body='', signature=signature) if axapi_failure(ip_nat_pool_data): ip_nat_pool_exists = False else: ip_nat_pool_list = [ ip_nat_pool['pool-name'] for ip_nat_pool in ip_nat_pool_data['pool-list'] ] if ip_nat_pool_name in ip_nat_pool_list: ip_nat_pool_exists = True else: ip_nat_pool_exists = False # POST configuration changed = False if operation == 'create': if ip_nat_pool_exists is False: result = axapi_call_v3(module, axapi_base_url + 'ip/nat/pool', method='POST', body=json.dumps(json_post), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to create the NAT Pool: %s" % result['response']['err']['msg']) changed = True else: changed = False axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json( msg="NAT pool %s already exists, use 'update' instead" % (ip_nat_pool_name)) # if we changed things, get the full info regarding result if changed: result = axapi_call_v3(module, axapi_base_url + 'ip/nat/pool/' + ip_nat_pool_name, method='GET', body='', signature=signature) else: result = ip_nat_pool_data elif operation == 'delete': if ip_nat_pool_exists: result = axapi_call_v3(module, axapi_base_url + 'ip/nat/pool/' + ip_nat_pool_name, method='DELETE', body='', signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to delete NAT Pool: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the NAT Pool was not present: %s" % (ip_nat_pool_name)) elif operation == 'update': if ip_nat_pool_exists: result = axapi_call_v3(module, axapi_base_url + 'ip/nat/pool/' + ip_nat_pool_name, method='PUT', body=json.dumps(json_post), signature=signature) if axapi_failure(result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to update NAT Pool: %s" % result['response']['err']['msg']) changed = True else: result = dict(msg="the NAT Pool was not present: %s" % (ip_nat_pool_name)) # if the config has changed, save the config unless otherwise requested if changed and write_config: write_result = axapi_call_v3(module, axapi_base_url + 'write/memory/', method='POST', body='', signature=signature) if axapi_failure(write_result): axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg']) # log out gracefully and exit axapi_call_v3(module, axapi_base_url + 'logoff/', method='POST', body='', signature=signature) module.exit_json(changed=changed, content=result)