diff --git a/changelogs/fragments/689-haproxy_agent_and_health.yml b/changelogs/fragments/689-haproxy_agent_and_health.yml new file mode 100644 index 0000000000..0e86d75e65 --- /dev/null +++ b/changelogs/fragments/689-haproxy_agent_and_health.yml @@ -0,0 +1,7 @@ +--- +minor_changes: + - haproxy - add options to dis/enable health and agent checks. When health + and agent checks are enabled for a service, a disabled service will + re-enable itself automatically. These options also change the state of + the agent checks to match the requested state for the backend + (https://github.com/ansible-collections/community.general/issues/684). diff --git a/plugins/modules/net_tools/haproxy.py b/plugins/modules/net_tools/haproxy.py index e56c9dabb1..1ed8c9843f 100644 --- a/plugins/modules/net_tools/haproxy.py +++ b/plugins/modules/net_tools/haproxy.py @@ -62,6 +62,18 @@ options: type: str required: true choices: [ disabled, drain, enabled ] + agent: + description: + - Disable/enable agent checks (depending on I(state) value). + type: bool + default: no + version_added: 1.0.0 + health: + description: + - Disable/enable health checks (depending on I(state) value). + type: bool + default: no + version_added: "1.0.0" fail_on_not_found: description: - Fail whenever trying to enable/disable a backend host that does not exist @@ -100,6 +112,13 @@ EXAMPLES = r''' host: '{{ inventory_hostname }}' backend: www +- name: Disable server in 'www' backend pool, also stop health/agent checks + community.general.haproxy: + state: disabled + host: '{{ inventory_hostname }}' + health: yes + agent: yes + - name: Disable server without backend pool name (apply to all available backend pool) community.general.haproxy: state: disabled @@ -228,6 +247,8 @@ class HAProxy(object): self.socket = self.module.params['socket'] self.shutdown_sessions = self.module.params['shutdown_sessions'] self.fail_on_not_found = self.module.params['fail_on_not_found'] + self.agent = self.module.params['agent'] + self.health = self.module.params['health'] self.wait = self.module.params['wait'] self.wait_retries = self.module.params['wait_retries'] self.wait_interval = self.module.params['wait_interval'] @@ -360,6 +381,10 @@ class HAProxy(object): set the weight for haproxy backend server when provides. """ cmd = "get weight $pxname/$svname; enable server $pxname/$svname" + if self.agent: + cmd += "; enable agent $pxname/$svname" + if self.health: + cmd += "; enable health $pxname/$svname" if weight: cmd += "; set weight $pxname/$svname %s" % weight self.execute_for_backends(cmd, backend, host, 'UP') @@ -370,7 +395,12 @@ class HAProxy(object): performed on the server until it leaves maintenance, also it shutdown sessions while disabling backend host server. """ - cmd = "get weight $pxname/$svname; disable server $pxname/$svname" + cmd = "get weight $pxname/$svname" + if self.agent: + cmd += "; disable agent $pxname/$svname" + if self.health: + cmd += "; disable health $pxname/$svname" + cmd += "; disable server $pxname/$svname" if shutdown_sessions: cmd += "; shutdown sessions server $pxname/$svname" self.execute_for_backends(cmd, backend, host, 'MAINT') @@ -428,6 +458,8 @@ def main(): socket=dict(type='path', default=DEFAULT_SOCKET_LOCATION), shutdown_sessions=dict(type='bool', default=False), fail_on_not_found=dict(type='bool', default=False), + health=dict(type='bool', default=False), + agent=dict(type='bool', default=False), wait=dict(type='bool', default=False), wait_retries=dict(type='int', default=WAIT_RETRIES), wait_interval=dict(type='int', default=WAIT_INTERVAL),