|
|
|
@ -130,31 +130,51 @@ ansible host -m a10_virtual -a "host=a10adc.example.com username=axapiuser passw
|
|
|
|
|
|
|
|
|
|
import urllib2
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def axapi_call(url, post=None):
|
|
|
|
|
result = urllib2.urlopen(url, post).read()
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def axapi_authenticate(base_url, user, pwd):
|
|
|
|
|
url = base_url + '&method=authenticate&username=' + user + '&password=' + pwd
|
|
|
|
|
sessid = json.loads(axapi_call(url))['session_id']
|
|
|
|
|
result = base_url + '&session_id=' + sessid
|
|
|
|
|
return result
|
|
|
|
|
url = base_url + '&method=authenticate&username=' + user + \
|
|
|
|
|
'&password=' + pwd
|
|
|
|
|
result = json.loads(axapi_call(url))
|
|
|
|
|
if 'response' in result:
|
|
|
|
|
return module.fail_json(msg=result['response']['err']['msg'])
|
|
|
|
|
sessid = result['session_id']
|
|
|
|
|
return base_url + '&session_id=' + sessid
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
global module
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
|
argument_spec=dict(
|
|
|
|
|
host=dict(type='str', required=True),
|
|
|
|
|
username = dict(type='str', aliases=['user', 'admin'], required=True),
|
|
|
|
|
password = dict(type='str', aliases=['pass', 'pwd'], required=True),
|
|
|
|
|
virtual_server = dict(type='str', aliases=['vip', 'virtual'], required=True),
|
|
|
|
|
virtual_server_ip = dict(type='str', aliases=['ip', 'address']),
|
|
|
|
|
virtual_server_status = dict(type='str', default='enabled', aliases=['status'], choices=['enabled', 'disabled']),
|
|
|
|
|
virtual_server_port = dict(type='int', aliases=['port', 'vport']),
|
|
|
|
|
virtual_server_port_type = dict(type='str', aliases=['proto', 'protocol'], choices=['tcp', 'udp', 'fast-http', 'http', 'https']),
|
|
|
|
|
virtual_server_port_status = dict(type='str', default='enabled', aliases=['portstatus', 'port_status'], choices=['enabled', 'disabled']),
|
|
|
|
|
username=dict(type='str', aliases=['user', 'admin'],
|
|
|
|
|
required=True),
|
|
|
|
|
password=dict(type='str', aliases=['pass', 'pwd'],
|
|
|
|
|
required=True),
|
|
|
|
|
virtual_server=dict(type='str', aliases=['vip', 'virtual'],
|
|
|
|
|
required=True),
|
|
|
|
|
virtual_server_ip=dict(type='str',
|
|
|
|
|
aliases=['ip', 'address']),
|
|
|
|
|
virtual_server_status=dict(type='str', default='enabled',
|
|
|
|
|
aliases=['status'],
|
|
|
|
|
choices=['enabled', 'disabled']),
|
|
|
|
|
virtual_server_port=dict(type='int',
|
|
|
|
|
aliases=['port', 'vport']),
|
|
|
|
|
virtual_server_port_type=dict(type='str',
|
|
|
|
|
aliases=['proto', 'protocol'],
|
|
|
|
|
choices=['tcp', 'udp', 'fast-http',
|
|
|
|
|
'http', 'https']),
|
|
|
|
|
virtual_server_port_status=dict(type='str', default='enabled',
|
|
|
|
|
aliases=['portstatus',
|
|
|
|
|
'port_status'],
|
|
|
|
|
choices=['enabled', 'disabled']),
|
|
|
|
|
service_group=dict(type='str', aliases=['pool', 'group']),
|
|
|
|
|
state = dict(type='str', default='present', choices=['present', 'absent']),
|
|
|
|
|
state=dict(type='str', default='present',
|
|
|
|
|
choices=['present', 'absent']),
|
|
|
|
|
),
|
|
|
|
|
supports_check_mode=False
|
|
|
|
|
)
|
|
|
|
@ -195,23 +215,55 @@ def main():
|
|
|
|
|
session_url = axapi_authenticate(axapi_base_url, user, pwd)
|
|
|
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
|
find_slb_virtual = axapi_call(session_url + '&method=slb.virtual_server.search', json.dumps({ 'name': slb_virtual }))
|
|
|
|
|
slb_virtual_fail = re.search('status": "fail', find_slb_virtual, re.I)
|
|
|
|
|
find_slb_virtual = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.search',
|
|
|
|
|
json.dumps({'name': slb_virtual}))
|
|
|
|
|
slb_virtual_fail = re.search('status": "fail',
|
|
|
|
|
find_slb_virtual, re.I)
|
|
|
|
|
|
|
|
|
|
if slb_virtual_fail:
|
|
|
|
|
if slb_virtual_port is None and slb_virtual_port_type is None and slb_service_group is None:
|
|
|
|
|
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status }}
|
|
|
|
|
elif slb_virtual_port is not None and slb_virtual_port_type is not None and slb_service_group is None:
|
|
|
|
|
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status, 'vport_list': [{ 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }]}}
|
|
|
|
|
elif slb_virtual_port is not None and slb_virtual_port_type is not None and slb_service_group is not None:
|
|
|
|
|
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status, 'vport_list': [{ 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }]}}
|
|
|
|
|
if slb_virtual_port is None and slb_virtual_port_type is None \
|
|
|
|
|
and slb_service_group is None:
|
|
|
|
|
json_post = {'virtual_server': {'name': slb_virtual,
|
|
|
|
|
'address': slb_virtual_ip,
|
|
|
|
|
'status': status}}
|
|
|
|
|
elif slb_virtual_port is not None and \
|
|
|
|
|
slb_virtual_port_type is not None and \
|
|
|
|
|
slb_service_group is None:
|
|
|
|
|
json_post = {'virtual_server':
|
|
|
|
|
{'name': slb_virtual,
|
|
|
|
|
'address': slb_virtual_ip,
|
|
|
|
|
'status': status,
|
|
|
|
|
'vport_list':
|
|
|
|
|
[{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port}]}}
|
|
|
|
|
elif slb_virtual_port is not None and \
|
|
|
|
|
slb_virtual_port_type is not None and \
|
|
|
|
|
slb_service_group is not None:
|
|
|
|
|
json_post = {'virtual_server':
|
|
|
|
|
{'name': slb_virtual,
|
|
|
|
|
'address': slb_virtual_ip,
|
|
|
|
|
'status': status, 'vport_list':
|
|
|
|
|
[{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port,
|
|
|
|
|
'service_group': slb_service_group}]}}
|
|
|
|
|
else:
|
|
|
|
|
module.fail_json(msg='virtual_server_port and virtual_server_type are required to create the virtual port')
|
|
|
|
|
module.fail_json(msg='virtual_server_port and
|
|
|
|
|
virtual_server_type are required to
|
|
|
|
|
create the virtual port')
|
|
|
|
|
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.create', json.dumps(json_post))
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.create',
|
|
|
|
|
json.dumps(json_post))
|
|
|
|
|
else:
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.search', json.dumps({ 'name': slb_virtual }))
|
|
|
|
|
slb_virtual_port_exist = re.search('"port":' + str(slb_virtual_port), response, re.I)
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.search',
|
|
|
|
|
json.dumps({'name': slb_virtual}))
|
|
|
|
|
slb_virtual_port_exist = re.search('"port":' +
|
|
|
|
|
str(slb_virtual_port)
|
|
|
|
|
response, re.I)
|
|
|
|
|
current_status = json.loads(response)['virtual_server']['status']
|
|
|
|
|
current_port_status = 1
|
|
|
|
|
|
|
|
|
@ -222,29 +274,68 @@ def main():
|
|
|
|
|
if slb_virtual_port == str(vport_list[port]['port']):
|
|
|
|
|
current_port_status = vport_list[port]['port']
|
|
|
|
|
|
|
|
|
|
json_post = { 'address': slb_virtual_ip, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }, 'status': port_status }
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.vport.update', json.dumps(json_post))
|
|
|
|
|
json_post = {'address': slb_virtual_ip,
|
|
|
|
|
'vport':
|
|
|
|
|
{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port,
|
|
|
|
|
'service_group': slb_service_group},
|
|
|
|
|
'status': port_status}
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.\
|
|
|
|
|
vport.update', json.dumps(json_post))
|
|
|
|
|
else:
|
|
|
|
|
if slb_service_group is None:
|
|
|
|
|
module.fail_json(msg='service_group is required')
|
|
|
|
|
json_post = { 'name': slb_virtual, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port, 'service_group': slb_service_group }, 'status': port_status }
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.vport.create', json.dumps(json_post))
|
|
|
|
|
json_post = {'name': slb_virtual,
|
|
|
|
|
'vport':
|
|
|
|
|
{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port,
|
|
|
|
|
'service_group': slb_service_group},
|
|
|
|
|
'status': port_status}
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.\
|
|
|
|
|
vport.create', json.dumps(json_post))
|
|
|
|
|
|
|
|
|
|
if current_status != status:
|
|
|
|
|
json_post = { 'virtual_server': { 'name': slb_virtual, 'address': slb_virtual_ip, 'status': status }}
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.update', json.dumps(json_post))
|
|
|
|
|
json_post = {'virtual_server':
|
|
|
|
|
{'name': slb_virtual,
|
|
|
|
|
'address': slb_virtual_ip,
|
|
|
|
|
'status': status}}
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.update',
|
|
|
|
|
json.dumps(json_post))
|
|
|
|
|
|
|
|
|
|
if current_port_status != port_status:
|
|
|
|
|
json_post = { 'address': slb_virtual_ip, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }, 'status': port_status }
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.vport.update', json.dumps(json_post))
|
|
|
|
|
json_post = {'address': slb_virtual_ip, 'vport':
|
|
|
|
|
{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port},
|
|
|
|
|
'status': port_status}
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.\
|
|
|
|
|
vport.update', json.dumps(json_post))
|
|
|
|
|
|
|
|
|
|
if state == 'absent':
|
|
|
|
|
if slb_virtual_port is not None and slb_virtual_port_type is not None:
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.vport.delete', json.dumps({ 'name': slb_virtual, 'vport': { 'protocol': vport_types[slb_virtual_port_type], 'port': slb_virtual_port }}))
|
|
|
|
|
if slb_virtual_port is not None and \
|
|
|
|
|
slb_virtual_port_type is not None:
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.\
|
|
|
|
|
vport.delete',
|
|
|
|
|
json.dumps({'name': slb_virtual,
|
|
|
|
|
'vport':
|
|
|
|
|
{'protocol':
|
|
|
|
|
vport_types[slb_virtual_port_type],
|
|
|
|
|
'port': slb_virtual_port}}))
|
|
|
|
|
elif slb_virtual_port is None and slb_virtual_port_type is None:
|
|
|
|
|
response = axapi_call(session_url + '&method=slb.virtual_server.delete', json.dumps({ 'name': slb_virtual }))
|
|
|
|
|
response = axapi_call(session_url +
|
|
|
|
|
'&method=slb.virtual_server.delete',
|
|
|
|
|
json.dumps({'name': slb_virtual}))
|
|
|
|
|
else:
|
|
|
|
|
module.fail_json(msg='virtual_server_port and virtual_server_type are required to remove the virtual port')
|
|
|
|
|
module.fail_json(msg='virtual_server_port and \
|
|
|
|
|
virtual_server_type are required to remove \
|
|
|
|
|
the virtual port')
|
|
|
|
|
|
|
|
|
|
result = json.loads(response)
|
|
|
|
|
axapi_call(session_url + '&method=session.close')
|
|
|
|
@ -252,6 +343,9 @@ def main():
|
|
|
|
|
except Exception, e:
|
|
|
|
|
return module.fail_json(msg='received exception: %s' % e)
|
|
|
|
|
|
|
|
|
|
if 'respone' in result and 'err' in result['response']:
|
|
|
|
|
return module.fail_json(msg=result['response']['err']['msg'])
|
|
|
|
|
|
|
|
|
|
module.exit_json(changed=True, content=result)
|
|
|
|
|
|
|
|
|
|
from ansible.module_utils.basic import *
|
|
|
|
|