2014-05-29 12:16:04 +02:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Ansible module to manage A10 Networks slb server objects
|
|
|
|
(c) 2014, Mischa Peters <mpeters@a10networks.com>
|
|
|
|
|
|
|
|
This file is part of Ansible
|
|
|
|
|
|
|
|
Ansible is free software: you can redistribute it and/or modify
|
|
|
|
it under the terms of the GNU General Public License as published by
|
|
|
|
the Free Software Foundation, either version 3 of the License, or
|
|
|
|
(at your option) any later version.
|
|
|
|
|
|
|
|
Ansible is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
|
|
|
along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
"""
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
---
|
|
|
|
module: a10_server
|
2014-08-29 03:23:48 +02:00
|
|
|
version_added: 1.8
|
2014-05-29 12:16:04 +02:00
|
|
|
short_description: Manage A10 Networks AX/SoftAX/Thunder/vThunder devices
|
|
|
|
description:
|
|
|
|
- Manage slb server objects on A10 Networks devices via aXAPI
|
2015-06-16 20:32:39 +02:00
|
|
|
author: "Mischa Peters (@mischapeters)"
|
2014-05-29 12:16:04 +02:00
|
|
|
notes:
|
|
|
|
- Requires A10 Networks aXAPI 2.1
|
|
|
|
options:
|
|
|
|
host:
|
|
|
|
description:
|
|
|
|
- hostname or ip of your A10 Networks device
|
|
|
|
required: true
|
|
|
|
username:
|
|
|
|
description:
|
|
|
|
- admin account of your A10 Networks device
|
|
|
|
required: true
|
|
|
|
aliases: ['user', 'admin']
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- admin password of your A10 Networks device
|
|
|
|
required: true
|
|
|
|
aliases: ['pass', 'pwd']
|
|
|
|
server_name:
|
|
|
|
description:
|
|
|
|
- slb server name
|
|
|
|
required: true
|
|
|
|
aliases: ['server']
|
|
|
|
server_ip:
|
|
|
|
description:
|
|
|
|
- slb server IP address
|
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
aliases: ['ip', 'address']
|
2014-08-29 03:23:48 +02:00
|
|
|
server_status:
|
2014-05-29 12:16:04 +02:00
|
|
|
description:
|
2014-08-29 03:23:48 +02:00
|
|
|
- slb virtual server status
|
2014-05-29 12:16:04 +02:00
|
|
|
required: false
|
2016-02-18 14:17:23 +01:00
|
|
|
default: enabled
|
2014-08-29 03:23:48 +02:00
|
|
|
aliases: ['status']
|
|
|
|
choices: ['enabled', 'disabled']
|
|
|
|
server_ports:
|
2014-05-29 12:16:04 +02:00
|
|
|
description:
|
2014-08-29 03:23:48 +02:00
|
|
|
- A list of ports to create for the server. Each list item should be a
|
|
|
|
dictionary which specifies the C(port:) and C(protocol:), but can also optionally
|
|
|
|
specify the C(status:). See the examples below for details. This parameter is
|
|
|
|
required when C(state) is C(present).
|
2014-05-29 12:16:04 +02:00
|
|
|
required: false
|
|
|
|
default: null
|
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- create, update or remove slb server
|
|
|
|
required: false
|
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
# Create a new server
|
2014-08-29 03:23:48 +02:00
|
|
|
- a10_server:
|
|
|
|
host: a10.mydomain.com
|
|
|
|
username: myadmin
|
|
|
|
password: mypassword
|
|
|
|
server: test
|
|
|
|
server_ip: 1.1.1.100
|
|
|
|
server_ports:
|
|
|
|
- port_num: 8080
|
|
|
|
protocol: tcp
|
|
|
|
- port_num: 8443
|
|
|
|
protocol: TCP
|
2014-05-29 12:16:04 +02:00
|
|
|
|
|
|
|
'''
|
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
VALID_PORT_FIELDS = ['port_num', 'protocol', 'status']
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
def validate_ports(module, ports):
|
|
|
|
for item in ports:
|
|
|
|
for key in item:
|
|
|
|
if key not in VALID_PORT_FIELDS:
|
|
|
|
module.fail_json(msg="invalid port field (%s), must be one of: %s" % (key, ','.join(VALID_PORT_FIELDS)))
|
2014-08-10 11:25:19 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
# validate the port number is present and an integer
|
|
|
|
if 'port_num' in item:
|
|
|
|
try:
|
|
|
|
item['port_num'] = int(item['port_num'])
|
|
|
|
except:
|
|
|
|
module.fail_json(msg="port_num entries in the port definitions must be integers")
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="port definitions must define the port_num field")
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
# validate the port protocol is present, and convert it to
|
|
|
|
# the internal API integer value (and validate it)
|
|
|
|
if 'protocol' in item:
|
|
|
|
protocol = axapi_get_port_protocol(item['protocol'])
|
|
|
|
if not protocol:
|
|
|
|
module.fail_json(msg="invalid port protocol, must be one of: %s" % ','.join(AXAPI_PORT_PROTOCOLS))
|
|
|
|
else:
|
|
|
|
item['protocol'] = protocol
|
|
|
|
else:
|
|
|
|
module.fail_json(msg="port definitions must define the port protocol (%s)" % ','.join(AXAPI_PORT_PROTOCOLS))
|
2014-08-10 11:25:19 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
# convert the status to the internal API integer value
|
|
|
|
if 'status' in item:
|
|
|
|
item['status'] = axapi_enabled_disabled(item['status'])
|
|
|
|
else:
|
|
|
|
item['status'] = 1
|
2014-08-10 11:25:19 +02:00
|
|
|
|
2014-05-29 12:16:04 +02:00
|
|
|
|
|
|
|
def main():
|
2014-08-29 03:23:48 +02:00
|
|
|
argument_spec = a10_argument_spec()
|
|
|
|
argument_spec.update(url_argument_spec())
|
|
|
|
argument_spec.update(
|
|
|
|
dict(
|
|
|
|
state=dict(type='str', default='present', choices=['present', 'absent']),
|
2014-08-10 11:25:19 +02:00
|
|
|
server_name=dict(type='str', aliases=['server'], required=True),
|
|
|
|
server_ip=dict(type='str', aliases=['ip', 'address']),
|
2014-08-29 03:23:48 +02:00
|
|
|
server_status=dict(type='str', default='enabled', aliases=['status'], choices=['enabled', 'disabled']),
|
|
|
|
server_ports=dict(type='list', aliases=['port'], default=[]),
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=argument_spec,
|
2014-05-29 12:16:04 +02:00
|
|
|
supports_check_mode=False
|
|
|
|
)
|
|
|
|
|
|
|
|
host = module.params['host']
|
2014-08-29 03:23:48 +02:00
|
|
|
username = module.params['username']
|
|
|
|
password = module.params['password']
|
|
|
|
state = module.params['state']
|
|
|
|
write_config = module.params['write_config']
|
2014-05-29 12:16:04 +02:00
|
|
|
slb_server = module.params['server_name']
|
|
|
|
slb_server_ip = module.params['server_ip']
|
|
|
|
slb_server_status = module.params['server_status']
|
2014-08-29 03:23:48 +02:00
|
|
|
slb_server_ports = module.params['server_ports']
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
if slb_server is None:
|
|
|
|
module.fail_json(msg='server_name is required')
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
axapi_base_url = 'https://%s/services/rest/V2.1/?format=json' % host
|
|
|
|
session_url = axapi_authenticate(module, axapi_base_url, username, password)
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
# validate the ports data structure
|
|
|
|
validate_ports(module, slb_server_ports)
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
json_post = {
|
|
|
|
'server': {
|
2014-11-25 21:43:47 +01:00
|
|
|
'name': slb_server,
|
2014-08-29 03:23:48 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-11-25 21:43:47 +01:00
|
|
|
# add optional module parameters
|
|
|
|
if slb_server_ip:
|
|
|
|
json_post['server']['host'] = slb_server_ip
|
|
|
|
|
|
|
|
if slb_server_ports:
|
|
|
|
json_post['server']['port_list'] = slb_server_ports
|
|
|
|
|
|
|
|
if slb_server_status:
|
|
|
|
json_post['server']['status'] = axapi_enabled_disabled(slb_server_status)
|
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
slb_server_data = axapi_call(module, session_url + '&method=slb.server.search', json.dumps({'name': slb_server}))
|
|
|
|
slb_server_exists = not axapi_failure(slb_server_data)
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
changed = False
|
|
|
|
if state == 'present':
|
|
|
|
if not slb_server_exists:
|
2014-11-25 21:43:47 +01:00
|
|
|
if not slb_server_ip:
|
|
|
|
module.fail_json(msg='you must specify an IP address when creating a server')
|
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
result = axapi_call(module, session_url + '&method=slb.server.create', json.dumps(json_post))
|
|
|
|
if axapi_failure(result):
|
|
|
|
module.fail_json(msg="failed to create the server: %s" % result['response']['err']['msg'])
|
|
|
|
changed = True
|
|
|
|
else:
|
2014-11-25 21:43:47 +01:00
|
|
|
def port_needs_update(src_ports, dst_ports):
|
2014-08-29 03:23:48 +02:00
|
|
|
'''
|
|
|
|
Checks to determine if the port definitions of the src_ports
|
|
|
|
array are in or different from those in dst_ports. If there is
|
|
|
|
a difference, this function returns true, otherwise false.
|
|
|
|
'''
|
|
|
|
for src_port in src_ports:
|
|
|
|
found = False
|
|
|
|
different = False
|
|
|
|
for dst_port in dst_ports:
|
|
|
|
if src_port['port_num'] == dst_port['port_num']:
|
|
|
|
found = True
|
|
|
|
for valid_field in VALID_PORT_FIELDS:
|
|
|
|
if src_port[valid_field] != dst_port[valid_field]:
|
|
|
|
different = True
|
|
|
|
break
|
|
|
|
if found or different:
|
|
|
|
break
|
|
|
|
if not found or different:
|
|
|
|
return True
|
|
|
|
# every port from the src exists in the dst, and none of them were different
|
|
|
|
return False
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-11-25 21:43:47 +01:00
|
|
|
def status_needs_update(current_status, new_status):
|
|
|
|
'''
|
|
|
|
Check to determine if we want to change the status of a server.
|
|
|
|
If there is a difference between the current status of the server and
|
|
|
|
the desired status, return true, otherwise false.
|
|
|
|
'''
|
|
|
|
if current_status != new_status:
|
|
|
|
return True
|
|
|
|
return False
|
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
defined_ports = slb_server_data.get('server', {}).get('port_list', [])
|
2014-11-25 21:43:47 +01:00
|
|
|
current_status = slb_server_data.get('server', {}).get('status')
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2014-11-25 21:43:47 +01:00
|
|
|
# we check for a needed update several ways
|
|
|
|
# - in case ports are missing from the ones specified by the user
|
|
|
|
# - in case ports are missing from those on the device
|
|
|
|
# - in case we are change the status of a server
|
2014-12-04 23:15:23 +01:00
|
|
|
if port_needs_update(defined_ports, slb_server_ports) or port_needs_update(slb_server_ports, defined_ports) or status_needs_update(current_status, axapi_enabled_disabled(slb_server_status)):
|
2014-08-29 03:23:48 +02:00
|
|
|
result = axapi_call(module, session_url + '&method=slb.server.update', json.dumps(json_post))
|
|
|
|
if axapi_failure(result):
|
|
|
|
module.fail_json(msg="failed to update the server: %s" % result['response']['err']['msg'])
|
|
|
|
changed = True
|
2014-08-10 11:25:19 +02:00
|
|
|
|
2014-08-29 03:23:48 +02:00
|
|
|
# if we changed things, get the full info regarding
|
|
|
|
# the service group for the return data below
|
|
|
|
if changed:
|
|
|
|
result = axapi_call(module, session_url + '&method=slb.server.search', json.dumps({'name': slb_server}))
|
|
|
|
else:
|
|
|
|
result = slb_server_data
|
|
|
|
elif state == 'absent':
|
|
|
|
if slb_server_exists:
|
|
|
|
result = axapi_call(module, session_url + '&method=slb.server.delete', json.dumps({'name': slb_server}))
|
|
|
|
changed = True
|
|
|
|
else:
|
2014-11-25 21:43:47 +01:00
|
|
|
result = dict(msg="the server was not present")
|
2014-05-29 12:16:04 +02:00
|
|
|
|
2016-08-06 12:48:26 +02:00
|
|
|
# if the config has changed, save the config unless otherwise requested
|
|
|
|
if changed and write_config:
|
2014-08-29 03:23:48 +02:00
|
|
|
write_result = axapi_call(module, session_url + '&method=system.action.write_memory')
|
|
|
|
if axapi_failure(write_result):
|
|
|
|
module.fail_json(msg="failed to save the configuration: %s" % write_result['response']['err']['msg'])
|
|
|
|
|
|
|
|
# log out of the session nicely and exit
|
|
|
|
axapi_call(module, session_url + '&method=session.close')
|
|
|
|
module.exit_json(changed=changed, content=result)
|
|
|
|
|
|
|
|
# standard ansible module imports
|
2014-05-29 12:16:04 +02:00
|
|
|
from ansible.module_utils.basic import *
|
2014-08-29 03:23:48 +02:00
|
|
|
from ansible.module_utils.urls import *
|
|
|
|
from ansible.module_utils.a10 import *
|
|
|
|
|
2014-05-29 12:16:04 +02:00
|
|
|
main()
|