| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright (c) 2024, Michael Ilg |
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: consul_agent_check |
| 11 |
short_description: Add, modify, and delete checks within a Consul cluster |
| 12 |
version_added: 9.1.0 |
| 13 |
description: |
| 14 |
- Allows the addition, modification and deletion of checks in a Consul cluster using the agent. For more details on using |
| 15 |
and configuring Checks, see U(https://developer.hashicorp.com/consul/api-docs/agent/check). |
| 16 |
- Currently, there is no complete way to retrieve the script, interval or TTL metadata for a registered check. Without this |
| 17 |
metadata it is not possible to tell if the data supplied with ansible represents a change to a check. As a result, the |
| 18 |
module does not attempt to determine changes and it always reports a changed occurred. An API method is planned to supply |
| 19 |
this metadata so at that stage change management is to be added. |
| 20 |
author: |
| 21 |
- Michael Ilg (@Ilgmi) |
| 22 |
extends_documentation_fragment: |
| 23 |
- community.general._consul |
| 24 |
- community.general._consul.actiongroup_consul |
| 25 |
- community.general._consul.token |
| 26 |
- community.general._attributes |
| 27 |
attributes: |
| 28 |
check_mode: |
| 29 |
support: full |
| 30 |
details: |
| 31 |
- The result is the object as it is defined in the module options and not the object structure of the Consul API. For |
| 32 |
a better overview of what the object structure looks like, take a look at U(https://developer.hashicorp.com/consul/api-docs/agent/check#list-checks). |
| 33 |
diff_mode: |
| 34 |
support: partial |
| 35 |
details: |
| 36 |
- In check mode the diff shows the object as it is defined in the module options and not the object structure of the |
| 37 |
Consul API. |
| 38 |
options: |
| 39 |
state: |
| 40 |
description: |
| 41 |
- Whether the check should be present or absent. |
| 42 |
choices: ['present', 'absent'] |
| 43 |
default: present |
| 44 |
type: str |
| 45 |
name: |
| 46 |
description: |
| 47 |
- Required name for the service check. |
| 48 |
type: str |
| 49 |
id: |
| 50 |
description: |
| 51 |
- Specifies a unique ID for this check on the node. This defaults to the O(name) parameter, but it may be necessary |
| 52 |
to provide an ID for uniqueness. This value is returned in the response as V(CheckId). |
| 53 |
type: str |
| 54 |
interval: |
| 55 |
description: |
| 56 |
- The interval at which the service check is run. This is a number with a V(s) or V(m) suffix to signify the units of |
| 57 |
seconds or minutes, for example V(15s) or V(1m). If no suffix is supplied V(s) is used by default, for example V(10) |
| 58 |
is equivalent to V(10s). |
| 59 |
- Required if one of the parameters O(args), O(http), or O(tcp) is specified. |
| 60 |
type: str |
| 61 |
notes: |
| 62 |
description: |
| 63 |
- Notes to attach to check when registering it. |
| 64 |
type: str |
| 65 |
args: |
| 66 |
description: |
| 67 |
- Specifies command arguments to run to update the status of the check. |
| 68 |
- Requires O(interval) to be provided. |
| 69 |
- Mutually exclusive with O(ttl), O(tcp) and O(http). |
| 70 |
type: list |
| 71 |
elements: str |
| 72 |
ttl: |
| 73 |
description: |
| 74 |
- Checks can be registered with a TTL instead of a O(args) and O(interval) this means that the service checks in with |
| 75 |
the agent before the TTL expires. If it does not the check is considered failed. Required if registering a check and |
| 76 |
the script an interval are missing Similar to the interval this is a number with a V(s) or V(m) suffix to signify |
| 77 |
the units of seconds or minutes, for example V(15s) or V(1m). If no suffix is supplied V(s) is used by default, for |
| 78 |
example V(10) is equivalent to V(10s). |
| 79 |
- Mutually exclusive with O(args), O(tcp) and O(http). |
| 80 |
type: str |
| 81 |
tcp: |
| 82 |
description: |
| 83 |
- Checks can be registered with a TCP port. This means that Consul will check if the connection attempt to that port |
| 84 |
is successful (that is, the port is currently accepting connections). The format is V(host:port), for example V(localhost:80). |
| 85 |
- Requires O(interval) to be provided. |
| 86 |
- Mutually exclusive with O(args), O(ttl) and O(http). |
| 87 |
type: str |
| 88 |
version_added: '1.3.0' |
| 89 |
http: |
| 90 |
description: |
| 91 |
- Checks can be registered with an HTTP endpoint. This means that Consul checks that the HTTP endpoint returns a successful |
| 92 |
HTTP status. |
| 93 |
- Requires O(interval) to be provided. |
| 94 |
- Mutually exclusive with O(args), O(ttl) and O(tcp). |
| 95 |
type: str |
| 96 |
timeout: |
| 97 |
description: |
| 98 |
- A custom HTTP check timeout. The Consul default is 10 seconds. Similar to the interval this is a number with a V(s) |
| 99 |
or V(m) suffix to signify the units of seconds or minutes, for example V(15s) or V(1m). If no suffix is supplied V(s) |
| 100 |
is used by default, for example V(10) is equivalent to V(10s). |
| 101 |
type: str |
| 102 |
service_id: |
| 103 |
description: |
| 104 |
- The ID for the service, must be unique per node. If O(state=absent), defaults to the service name if supplied. |
| 105 |
type: str |
| 106 |
""" |
| 107 |
|
| 108 |
EXAMPLES = r""" |
| 109 |
- name: Register tcp check for service 'nginx' |
| 110 |
community.general.consul_agent_check: |
| 111 |
name: nginx_tcp_check |
| 112 |
service_id: nginx |
| 113 |
interval: 60s |
| 114 |
tcp: localhost:80 |
| 115 |
notes: "Nginx Check" |
| 116 |
|
| 117 |
- name: Register http check for service 'nginx' |
| 118 |
community.general.consul_agent_check: |
| 119 |
name: nginx_http_check |
| 120 |
service_id: nginx |
| 121 |
interval: 60s |
| 122 |
http: http://localhost:80/status |
| 123 |
notes: "Nginx Check" |
| 124 |
|
| 125 |
- name: Remove check for service 'nginx' |
| 126 |
community.general.consul_agent_check: |
| 127 |
state: absent |
| 128 |
id: nginx_http_check |
| 129 |
service_id: "{{ nginx_service.ID }}" |
| 130 |
""" |
| 131 |
|
| 132 |
RETURN = r""" |
| 133 |
check: |
| 134 |
description: The check as returned by the Consul HTTP API. |
| 135 |
returned: always |
| 136 |
type: dict |
| 137 |
sample: |
| 138 |
CheckID: nginx_check |
| 139 |
ServiceID: nginx |
| 140 |
Interval: 30s |
| 141 |
Type: http |
| 142 |
Notes: Nginx Check |
| 143 |
operation: |
| 144 |
description: The operation performed. |
| 145 |
returned: changed |
| 146 |
type: str |
| 147 |
sample: update |
| 148 |
""" |
| 149 |
|
| 150 |
from ansible.module_utils.basic import AnsibleModule |
| 151 |
|
| 152 |
from ansible_collections.community.general.plugins.module_utils._consul import ( |
| 153 |
AUTH_ARGUMENTS_SPEC, |
| 154 |
OPERATION_CREATE, |
| 155 |
OPERATION_DELETE, |
| 156 |
OPERATION_READ, |
| 157 |
OPERATION_UPDATE, |
| 158 |
_ConsulModule, |
| 159 |
validate_check, |
| 160 |
) |
| 161 |
|
| 162 |
_ARGUMENT_SPEC = { |
| 163 |
"state": dict(default="present", choices=["present", "absent"]), |
| 164 |
"name": dict(type="str"), |
| 165 |
"id": dict(type="str"), |
| 166 |
"interval": dict(type="str"), |
| 167 |
"notes": dict(type="str"), |
| 168 |
"args": dict(type="list", elements="str"), |
| 169 |
"http": dict(type="str"), |
| 170 |
"tcp": dict(type="str"), |
| 171 |
"ttl": dict(type="str"), |
| 172 |
"timeout": dict(type="str"), |
| 173 |
"service_id": dict(type="str"), |
| 174 |
} |
| 175 |
|
| 176 |
_MUTUALLY_EXCLUSIVE = [ |
| 177 |
("args", "ttl", "tcp", "http"), |
| 178 |
] |
| 179 |
|
| 180 |
_REQUIRED_IF = [ |
| 181 |
("state", "present", ["name"]), |
| 182 |
("state", "absent", ("id", "name"), True), |
| 183 |
] |
| 184 |
|
| 185 |
_REQUIRED_BY = { |
| 186 |
"args": "interval", |
| 187 |
"http": "interval", |
| 188 |
"tcp": "interval", |
| 189 |
} |
| 190 |
|
| 191 |
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC) |
| 192 |
|
| 193 |
|
| 194 |
class ConsulAgentCheckModule(_ConsulModule): |
| 195 |
api_endpoint = "agent/check" |
| 196 |
result_key = "check" |
| 197 |
unique_identifiers = ["id", "name"] |
| 198 |
operational_attributes = { |
| 199 |
"Node", |
| 200 |
"CheckID", |
| 201 |
"Output", |
| 202 |
"ServiceName", |
| 203 |
"ServiceTags", |
| 204 |
"Status", |
| 205 |
"Type", |
| 206 |
"ExposedPort", |
| 207 |
"Definition", |
| 208 |
} |
| 209 |
|
| 210 |
def endpoint_url(self, operation, identifier=None): |
| 211 |
if operation == OPERATION_READ: |
| 212 |
return "agent/checks" |
| 213 |
if operation in [OPERATION_CREATE, OPERATION_UPDATE]: |
| 214 |
return f"{self.api_endpoint}/register" |
| 215 |
if operation == OPERATION_DELETE: |
| 216 |
return f"{self.api_endpoint}/deregister/{identifier}" |
| 217 |
|
| 218 |
return super().endpoint_url(operation, identifier) |
| 219 |
|
| 220 |
def read_object(self): |
| 221 |
url = self.endpoint_url(OPERATION_READ) |
| 222 |
checks = self.get(url) |
| 223 |
identifier = self.id_from_obj(self.params) |
| 224 |
if identifier in checks: |
| 225 |
return checks[identifier] |
| 226 |
return None |
| 227 |
|
| 228 |
def prepare_object(self, existing, obj): |
| 229 |
existing = super().prepare_object(existing, obj) |
| 230 |
validate_check(existing) |
| 231 |
return existing |
| 232 |
|
| 233 |
def delete_object(self, obj): |
| 234 |
if not self._module.check_mode: |
| 235 |
self.put(self.endpoint_url(OPERATION_DELETE, obj.get("CheckID"))) |
| 236 |
return {} |
| 237 |
|
| 238 |
|
| 239 |
def main(): |
| 240 |
module = AnsibleModule( |
| 241 |
_ARGUMENT_SPEC, |
| 242 |
mutually_exclusive=_MUTUALLY_EXCLUSIVE, |
| 243 |
required_if=_REQUIRED_IF, |
| 244 |
required_by=_REQUIRED_BY, |
| 245 |
supports_check_mode=True, |
| 246 |
) |
| 247 |
|
| 248 |
consul_module = ConsulAgentCheckModule(module) |
| 249 |
consul_module.execute() |
| 250 |
|
| 251 |
|
| 252 |
if __name__ == "__main__": |
| 253 |
main() |