2013-03-06 15:32:01 +01:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
"""
|
|
|
|
Ansible module to manage Citrix NetScaler entities
|
|
|
|
(c) 2013, Nandor Sivok <nandor@gawker.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: netscaler
|
2013-05-11 22:37:42 +02:00
|
|
|
version_added: "1.1"
|
2013-03-06 15:32:01 +01:00
|
|
|
short_description: Manages Citrix NetScaler entities
|
|
|
|
description:
|
|
|
|
- Manages Citrix NetScaler server and service entities.
|
|
|
|
options:
|
|
|
|
nsc_host:
|
|
|
|
description:
|
|
|
|
- hostname or ip of your netscaler
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
nsc_protocol:
|
|
|
|
description:
|
|
|
|
- protocol used to access netscaler
|
|
|
|
required: false
|
|
|
|
default: https
|
|
|
|
aliases: []
|
|
|
|
user:
|
|
|
|
description:
|
|
|
|
- username
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
password:
|
|
|
|
description:
|
|
|
|
- password
|
|
|
|
required: true
|
|
|
|
default: null
|
|
|
|
aliases: []
|
|
|
|
action:
|
|
|
|
description:
|
|
|
|
- the action you want to perform on the entity
|
|
|
|
required: false
|
|
|
|
default: disable
|
|
|
|
choices: ["enable", "disable"]
|
|
|
|
aliases: []
|
|
|
|
name:
|
|
|
|
description:
|
|
|
|
- name of the entity
|
|
|
|
required: true
|
|
|
|
default: hostname
|
|
|
|
aliases: []
|
|
|
|
type:
|
|
|
|
description:
|
|
|
|
- type of the entity
|
|
|
|
required: false
|
|
|
|
default: server
|
|
|
|
choices: ["server", "service"]
|
|
|
|
aliases: []
|
2014-03-10 22:06:52 +01:00
|
|
|
validate_certs:
|
|
|
|
description:
|
|
|
|
- If C(no), SSL certificates for the target url will not be validated. This should only be used
|
|
|
|
on personally controlled sites using self-signed certificates.
|
|
|
|
required: false
|
|
|
|
default: 'yes'
|
|
|
|
choices: ['yes', 'no']
|
|
|
|
|
2013-03-06 15:32:01 +01:00
|
|
|
requirements: [ "urllib", "urllib2" ]
|
2015-05-13 23:27:49 +02:00
|
|
|
author: "Nandor Sivok (@dominis)" <nandor@gawker.com>
|
2013-03-06 15:32:01 +01:00
|
|
|
'''
|
|
|
|
|
2013-06-14 11:53:43 +02:00
|
|
|
EXAMPLES = '''
|
|
|
|
# Disable the server
|
|
|
|
ansible host -m netscaler -a "nsc_host=nsc.example.com user=apiuser password=apipass"
|
|
|
|
|
|
|
|
# Enable the server
|
|
|
|
ansible host -m netscaler -a "nsc_host=nsc.example.com user=apiuser password=apipass action=enable"
|
|
|
|
|
|
|
|
# Disable the service local:8080
|
|
|
|
ansible host -m netscaler -a "nsc_host=nsc.example.com user=apiuser password=apipass name=local:8080 type=service action=disable"
|
|
|
|
'''
|
|
|
|
|
2013-03-06 15:32:01 +01:00
|
|
|
|
|
|
|
import base64
|
|
|
|
import socket
|
|
|
|
|
|
|
|
|
|
|
|
class netscaler(object):
|
|
|
|
|
|
|
|
_nitro_base_url = '/nitro/v1/'
|
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
def __init__(self, module):
|
|
|
|
self.module = module
|
|
|
|
|
2013-03-06 15:32:01 +01:00
|
|
|
def http_request(self, api_endpoint, data_json={}):
|
|
|
|
request_url = self._nsc_protocol + '://' + self._nsc_host + self._nitro_base_url + api_endpoint
|
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
data_json = urllib.urlencode(data_json)
|
|
|
|
if not len(data_json):
|
|
|
|
data_json = None
|
2013-03-06 15:32:01 +01:00
|
|
|
|
2014-03-10 22:06:52 +01:00
|
|
|
auth = base64.encodestring('%s:%s' % (self._nsc_user, self._nsc_pass)).replace('\n', '').strip()
|
|
|
|
headers = {
|
|
|
|
'Authorization': 'Basic %s' % auth,
|
|
|
|
'Content-Type' : 'application/x-www-form-urlencoded',
|
|
|
|
}
|
2013-03-06 15:32:01 +01:00
|
|
|
|
2014-07-18 00:04:21 +02:00
|
|
|
response, info = fetch_url(self.module, request_url, data=data_json, headers=headers)
|
2013-03-06 15:32:01 +01:00
|
|
|
|
2014-07-18 00:04:21 +02:00
|
|
|
return json.load(response)
|
2013-03-06 15:32:01 +01:00
|
|
|
|
|
|
|
def prepare_request(self, action):
|
|
|
|
resp = self.http_request(
|
|
|
|
'config',
|
|
|
|
{
|
|
|
|
"object":
|
|
|
|
{
|
|
|
|
"params": {"action": action},
|
|
|
|
self._type: {"name": self._name}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
def core(module):
|
2014-03-10 22:06:52 +01:00
|
|
|
n = netscaler(module)
|
2013-03-06 15:32:01 +01:00
|
|
|
n._nsc_host = module.params.get('nsc_host')
|
|
|
|
n._nsc_user = module.params.get('user')
|
|
|
|
n._nsc_pass = module.params.get('password')
|
|
|
|
n._nsc_protocol = module.params.get('nsc_protocol')
|
|
|
|
n._name = module.params.get('name')
|
|
|
|
n._type = module.params.get('type')
|
|
|
|
action = module.params.get('action')
|
|
|
|
|
|
|
|
r = n.prepare_request(action)
|
|
|
|
|
|
|
|
return r['errorcode'], r
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec = dict(
|
|
|
|
nsc_host = dict(required=True),
|
|
|
|
nsc_protocol = dict(default='https'),
|
|
|
|
user = dict(required=True),
|
|
|
|
password = dict(required=True),
|
|
|
|
action = dict(default='enable', choices=['enable','disable']),
|
|
|
|
name = dict(default=socket.gethostname()),
|
2014-03-10 22:06:52 +01:00
|
|
|
type = dict(default='server', choices=['service', 'server']),
|
|
|
|
validate_certs=dict(default='yes', type='bool'),
|
2013-03-06 15:32:01 +01:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
rc = 0
|
|
|
|
try:
|
|
|
|
rc, result = core(module)
|
|
|
|
except Exception, e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
if rc != 0:
|
|
|
|
module.fail_json(rc=rc, msg=result)
|
|
|
|
else:
|
|
|
|
result['changed'] = True
|
|
|
|
module.exit_json(**result)
|
|
|
|
|
|
|
|
|
2013-12-02 21:13:49 +01:00
|
|
|
# import module snippets
|
2013-12-02 21:11:23 +01:00
|
|
|
from ansible.module_utils.basic import *
|
2014-03-10 22:06:52 +01:00
|
|
|
from ansible.module_utils.urls import *
|
2013-03-06 15:32:01 +01:00
|
|
|
main()
|