Documentation additions and major refactor

reviewable/pr18780/r1
Matt Hite 11 years ago
parent bb8ae983c3
commit 459f290da3

@ -1,4 +1,4 @@
#!/usr/bin/env python #!/usr/bin/python
# -*- coding: utf-8 -*- # -*- coding: utf-8 -*-
# (c) 2013, Matt Hite <mhite@hotmail.com> # (c) 2013, Matt Hite <mhite@hotmail.com>
@ -21,10 +21,10 @@
DOCUMENTATION = ''' DOCUMENTATION = '''
--- ---
module: bigip_node module: bigip_node
short_description: "Manages F5 BIG-IP LTM pool members" short_description: "Manages F5 BIG-IP LTM nodes"
description: description:
- "Manages F5 BIG-IP LTM pool members via iControl SOAP API" - "Manages F5 BIG-IP LTM nodes via iControl SOAP API"
version_added: "TBD" version_added: "1.3"
author: Matt Hite author: Matt Hite
notes: notes:
- "Requires BIG-IP software version >= 11" - "Requires BIG-IP software version >= 11"
@ -69,21 +69,21 @@ options:
choices: [] choices: []
aliases: [] aliases: []
name: name:
description description:
- "Node name" - "Node name"
required: false required: false
default: null default: null
choices: [] choices: []
host: host:
description: description:
- "Node IP. Required when state=present. Ignored when state=absent." - "Node IP. Required when state=present and node does not exist. Error when state=absent."
required: true required: true
default: null default: null
choices: [] choices: []
aliases: ['address'] aliases: ['address', 'ip']
description: description:
description: description:
- "Optional node description." - "Node description."
required: false required: false
default: null default: null
choices: [] choices: []
@ -91,10 +91,51 @@ options:
EXAMPLES = ''' EXAMPLES = '''
TBD ## playbook task examples:
TBD
TBD ---
TBD # file bigip-test.yml
# ...
- hosts: bigip-test
tasks:
- name: Add node
local_action: >
bigip_node
server=lb.mydomain.com
user=admin
password=mysecret
state=present
partition=matthite
host="{{ ansible_default_ipv4["address"] }}"
name="{{ ansible_default_ipv4["address"] }}"
# Note that the BIG-IP automatically names the node using the
# IP address specified in previous play's host parameter.
# Future plays referencing this node no longer use the host
# parameter but instead use the name parameter.
# Alternatively, you could have specified a name with the
# name parameter when state=present.
- name: Modify node description
local_action: >
bigip_node
server=lb.mydomain.com
user=admin
password=mysecret
state=present
partition=matthite
name="{{ ansible_default_ipv4["address"] }}"
description="Our best server yet"
- name: Delete node
local_action: >
bigip_node
server=lb.mydomain.com
user=admin
password=mysecret
state=absent
partition=matthite
name="{{ ansible_default_ipv4["address"] }}"
''' '''
@ -106,8 +147,8 @@ else:
bigsuds_found = True bigsuds_found = True
import traceback import traceback
# =========================================== # ==========================
# bigip_pool_member module specific support methods. # bigip_node module specific
# #
def bigip_api(bigip, user, password): def bigip_api(bigip, user, password):
@ -131,41 +172,42 @@ def node_exists(api, address):
def create_node_address(api, address, name): def create_node_address(api, address, name):
try: try:
api.LocalLB.NodeAddressV2.create(nodes=[name], addresses=[address], limits=[0]) api.LocalLB.NodeAddressV2.create(nodes=[name], addresses=[address], limits=[0])
return True, "" result = True
desc = ""
except bigsuds.OperationFailed, e: except bigsuds.OperationFailed, e:
if "invalid node address" in str(e) and "already exists" in str(e): if "already exists" in str(e):
return (False, "The referenced IP address is already in use.") result = False
desc = "referenced name or IP already in use"
else: else:
# genuine exception # genuine exception
raise raise
return (result, desc)
def get_node_address(api, name): def get_node_address(api, name):
return api.LocalLB.NodeAddressV2.get_address(nodes=[name])[0] return api.LocalLB.NodeAddressV2.get_address(nodes=[name])[0]
def delete_node_address(api, address): def delete_node_address(api, address):
result = False
try: try:
api.LocalLB.NodeAddressV2.delete_node_address(nodes=[address]) api.LocalLB.NodeAddressV2.delete_node_address(nodes=[address])
result = True result = True
desc = ""
except bigsuds.OperationFailed, e: except bigsuds.OperationFailed, e:
if "is referenced by a member of pool" in str(e): if "is referenced by a member of pool" in str(e):
result = False result = False
desc = "node referenced by pool"
else: else:
# genuine exception # genuine exception
raise raise
return result return (result, desc)
def set_node_description(api, name, description): def set_node_description(api, name, description):
api.LocalLB.NodeAddressV2.set_description(nodes=[name], api.LocalLB.NodeAddressV2.set_description(nodes=[name],
descriptions=[description]) descriptions=[description])
def get_node_description(api, name): def get_node_description(api, name):
return api.LocalLB.NodeAddressV2.get_description(nodes=[name])[0] return api.LocalLB.NodeAddressV2.get_description(nodes=[name])[0]
def main(): def main():
module = AnsibleModule( module = AnsibleModule(
argument_spec = dict( argument_spec = dict(
server = dict(type='str', required=True), server = dict(type='str', required=True),
@ -175,7 +217,7 @@ def main():
partition = dict(type='str', default='Common'), partition = dict(type='str', default='Common'),
name = dict(type='str', required=True), name = dict(type='str', required=True),
host = dict(type='str', aliases=['address', 'ip']), host = dict(type='str', aliases=['address', 'ip']),
description = dict(type='str', default='') description = dict(type='str')
), ),
supports_check_mode=True supports_check_mode=True
) )
@ -193,10 +235,8 @@ def main():
address = "/%s/%s" % (partition, name) address = "/%s/%s" % (partition, name)
description = module.params['description'] description = module.params['description']
# sanity check user supplied values if state == 'absent' and host is not None:
module.fail_json(msg="host parameter invalid when state=absent")
if state == 'present' and host is None:
module.fail_json(msg='host required when state=present')
try: try:
api = bigip_api(server, user, password) api = bigip_api(server, user, password)
@ -205,33 +245,41 @@ def main():
if state == 'absent': if state == 'absent':
if node_exists(api, address): if node_exists(api, address):
if not module.check_mode: if not module.check_mode:
deleted = delete_node_address(api, address) deleted, desc = delete_node_address(api, address)
if not deleted: if not deleted:
module.fail_json(msg="unable to delete: node referenced by pool") module.fail_json(msg="unable to delete: %s" % desc)
else: else:
result = {'changed': True} result = {'changed': True}
else: else:
# check-mode return value
result = {'changed': True} result = {'changed': True}
elif state == 'present': elif state == 'present':
if not node_exists(api, address): if not node_exists(api, address):
if host is None:
module.fail_json(msg="host parameter required when " \
"state=present and node does not exist")
if not module.check_mode: if not module.check_mode:
if not node_exists(api, address): created, desc = create_node_address(api, address=host, name=address)
res, desc = create_node_address(api, address=host, name=address) if not created:
if not res: module.fail_json(msg="unable to create: %s" % desc)
module.fail_json(msg=desc) else:
result = {'changed': True} result = {'changed': True}
if description is not None:
set_node_description(api, address, description)
result = {'changed': True}
else:
# check-mode return value
result = {'changed': True}
else: else:
# node exists -- potentially modify attributes # node exists -- potentially modify attributes
if host is not None: if host is not None:
current_address = get_node_address(api, address) if get_node_address(api, address) != host:
if current_address != host: module.fail_json(msg="Changing the node address is " \
module.fail_json(msg="""Changing the node address is not "not supported by the API; " \
supported by the API, delete and "delete and recreate the node.")
recreate the node.""") if description is not None:
if get_node_description(api, address) != description:
current_descrip = get_node_description(api, address)
if current_descrip != description:
if not module.check_mode: if not module.check_mode:
set_node_description(api, address, description) set_node_description(api, address, description)
result = {'changed': True} result = {'changed': True}

Loading…
Cancel
Save