@ -36,7 +36,7 @@ options:
- Action to take .
required : true
default : null
choices : [ " enable _server" , " disable_server " , " get_weight " , " set_weight " ]
choices : [ " enable d" , " disabled " , " get_weight " , " set_weight " ]
host :
description :
- Host ( backend ) to operate in Haproxy .
@ -67,19 +67,19 @@ options:
EXAMPLES = '''
# disable backend server in 'www' backend
- haproxy : action = disable _server host = { { inventory_hostname } } backend = www
- haproxy : action = disable d host = { { inventory_hostname } } backend = www
# disable backend server without backend name (applied to all)
- haproxy : action = disable _server host = { { inventory_hostname } }
- haproxy : action = disable d host = { { inventory_hostname } }
# disable server, provide socket file
- haproxy : action = disable _server host = { { inventory_hostname } } socket = / var / run / haproxy . sock backend = www
- haproxy : action = disable d host = { { inventory_hostname } } socket = / var / run / haproxy . sock backend = www
# disable backend server in 'www' backend and drop open sessions to it
- haproxy : action = disable _server host = { { inventory_hostname } } backend = www shutdown_sessions = true
- haproxy : action = disable d host = { { inventory_hostname } } backend = www shutdown_sessions = true
# enable backend server in 'www' backend
- haproxy : action = enable _server host = { { inventory_hostname } } backend = www
- haproxy : action = enable d host = { { inventory_hostname } } backend = www
# report a server's current weight in 'www' backend
- haproxy : action = get_weight host = { { inventory_hostname } } backend = www
@ -102,8 +102,8 @@ RECV_SIZE = 1024
def main ( ) :
ACTION_CHOICES = [
' enable _server ' ,
' disable _server ' ,
' enable d ' ,
' disable d ' ,
' get_weight ' ,
' set_weight '
]
@ -118,6 +118,8 @@ def main():
socket = dict ( required = False , default = DEFAULT_SOCKET_LOCATION ) ,
shutdown_sessions = dict ( required = False , default = False ) ,
) ,
supports_check_mode = True ,
)
action = module . params [ ' action ' ]
host = module . params [ ' host ' ]
@ -128,22 +130,22 @@ def main():
##################################################################
# Required args per action:
# (enable /disable)_server = (host)
# (enable d /disabled ) = (host)
#
# AnsibleModule will verify most stuff, we need to verify
# 'socket' manually.
##################################################################
if action in [ ' enable_server ' , ' disable_server ' , ' get_weight ' , ' set_weight ' ] :
if not host :
module . fail_json ( msg = ' no host specified for action requiring one ' )
if action in [ ' set_weight ' ] :
if not weight :
module . fail_json ( msg = ' no weight specified for action, require value in number ' )
##################################################################
if not socket :
module . fail_json ( ' unable to locate haproxy.sock ' )
##################################################################
supports_check_mode = True ,
required_one_of = [ [ ' action ' , ' host ' ] ]
ansible_haproxy = HAProxy ( module , * * module . params )
@ -202,7 +204,7 @@ class HAProxy(object):
self . client . close ( )
return result
def enable _server ( self , host , backend ) :
def enable d ( self , host , backend ) :
"""
Enables backend server for a particular backend .
@ -238,7 +240,7 @@ class HAProxy(object):
cmd = " enable server %s / %s " % ( pxname , svname )
self . execute ( cmd )
def disable _server ( self , host , backend , shutdown_sessions ) :
def disable d ( self , host , backend , shutdown_sessions ) :
"""
Disable backend server for a particular backend .
@ -366,11 +368,11 @@ class HAProxy(object):
needful ( at the earliest ) .
"""
# toggle enable/disbale server
if self . action == ' enable _server ' :
self . enable _server ( self . host , self . backend )
if self . action == ' enable d ' :
self . enable d ( self . host , self . backend )
elif self . action == ' disable _server ' :
self . disable _server ( self . host , self . backend , self . shutdown_sessions )
elif self . action == ' disable d ' :
self . disable d ( self . host , self . backend , self . shutdown_sessions )
# toggle get/set weight
elif self . action == ' get_weight ' :