Repository: ansible-collections/community.general

Issue:
haproxy module fails to disable host if health and agent checks are enabled.
_From @linsomniac on Jul 24, 2020 05:05_

##### SUMMARY
The haproxy module sends the "disable server" command, but if health and agent checks are enabled, the server immediately re-enables itself due to these checks passing.

I have a fix on https://github.com/linsomniac/ansible-modules-extras/tree/haproxy-agent-health which adds "health" and "agent" options and will send "disable health" and "disable agent" commands in addition to "disable server".  The same is done for the "enable".

I was unable to figure out how to submit a PR of this change in this repo, because ansible-modules-extra is a locked repo and instructions on how to submit PRs in that repo reference a URL that no longer exists (issue for that filed separately).

##### ISSUE TYPE
Add ability for disable/enable to stop/start health and agent checks.

##### COMPONENT NAME
haproxy.py

##### ANSIBLE VERSION
```
ansible 2.9.2
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'/home/sean/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible
  python version = 2.7.17 (default, Apr 15 2020, 17:20:14) [GCC 7.5.0]
```

##### ADDITIONAL INFORMATION

```yaml
tasks:
  - name: Disable server
    haproxy:
      host: web1
      state: disabled
      agent: true
      health: true
```


_Copied from original issue: ansible/ansible#70859_


Discussion:
Files identified in the description:
* [`plugins/modules/net_tools/haproxy.py`](https://github.com/ansible-collections/community.general/blob/master/plugins/modules/net_tools/haproxy.py)

If these files are inaccurate, please update the `component name` section of the description or use the `!component` bot command.

[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: components_banner --->
cc @nerzhul @ravibhure
[click here for bot help](https://github.com/ansible/ansibullbot/blob/master/ISSUE_HELP.md)
<!--- boilerplate: notify --->
@linsomniac Please submit your PR in this repo.
Also note that the ansible-modules-extras repo is outdated **by many years**.

Resolving patch:
diff --git a/plugins/modules/net_tools/haproxy.py b/plugins/modules/net_tools/haproxy.py
--- a/plugins/modules/net_tools/haproxy.py
+++ b/plugins/modules/net_tools/haproxy.py
@@ -62,6 +62,18 @@
     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 @@
     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 @@ def __init__(self, module):
         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 @@ def enabled(self, host, backend, weight):
         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 @@ def disabled(self, host, backend, shutdown_sessions):
         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),


Test patch: