| 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_service |
| 11 |
short_description: Add, modify and delete services within a Consul cluster |
| 12 |
version_added: 9.1.0 |
| 13 |
description: |
| 14 |
- Allows the addition, modification and deletion of services in a Consul cluster using the agent. |
| 15 |
- There are currently no plans to create services and checks in one. This is because the Consul API does not provide checks |
| 16 |
for a service and the checks themselves do not match the module parameters. Therefore, only a service without checks can |
| 17 |
be created in this module. |
| 18 |
author: |
| 19 |
- Michael Ilg (@Ilgmi) |
| 20 |
extends_documentation_fragment: |
| 21 |
- community.general._consul |
| 22 |
- community.general._consul.actiongroup_consul |
| 23 |
- community.general._consul.token |
| 24 |
- community.general._attributes |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: full |
| 28 |
diff_mode: |
| 29 |
support: partial |
| 30 |
details: |
| 31 |
- In check mode the diff misses operational attributes. |
| 32 |
options: |
| 33 |
state: |
| 34 |
description: |
| 35 |
- Whether the service should be present or absent. |
| 36 |
choices: ['present', 'absent'] |
| 37 |
default: present |
| 38 |
type: str |
| 39 |
name: |
| 40 |
description: |
| 41 |
- Unique name for the service on a node, must be unique per node, required if registering a service. |
| 42 |
type: str |
| 43 |
id: |
| 44 |
description: |
| 45 |
- Specifies a unique ID for this service. This must be unique per agent. This defaults to the O(name) parameter if not |
| 46 |
provided. If O(state=absent), defaults to the service name if supplied. |
| 47 |
type: str |
| 48 |
tags: |
| 49 |
description: |
| 50 |
- Tags that are attached to the service registration. |
| 51 |
type: list |
| 52 |
elements: str |
| 53 |
address: |
| 54 |
description: |
| 55 |
- The address to advertise that the service listens on. This value is passed as the C(address) parameter to Consul's |
| 56 |
C(/v1/agent/service/register) API method, so refer to the Consul API documentation for further details. |
| 57 |
type: str |
| 58 |
meta: |
| 59 |
description: |
| 60 |
- Optional meta data used for filtering. For keys, the characters C(A-Z), C(a-z), C(0-9), C(_), C(-) are allowed. Not |
| 61 |
allowed characters are replaced with underscores. |
| 62 |
type: dict |
| 63 |
service_port: |
| 64 |
description: |
| 65 |
- The port on which the service is listening. Can optionally be supplied for registration of a service, that is if O(name) |
| 66 |
or O(id) is set. |
| 67 |
type: int |
| 68 |
enable_tag_override: |
| 69 |
description: |
| 70 |
- Specifies to disable the anti-entropy feature for this service's tags. If C(EnableTagOverride) is set to true then |
| 71 |
external agents can update this service in the catalog and modify the tags. |
| 72 |
type: bool |
| 73 |
default: false |
| 74 |
weights: |
| 75 |
description: |
| 76 |
- Specifies weights for the service. |
| 77 |
type: dict |
| 78 |
suboptions: |
| 79 |
passing: |
| 80 |
description: |
| 81 |
- Weights for passing. |
| 82 |
type: int |
| 83 |
default: 1 |
| 84 |
warning: |
| 85 |
description: |
| 86 |
- Weights for warning. |
| 87 |
type: int |
| 88 |
default: 1 |
| 89 |
default: {"passing": 1, "warning": 1} |
| 90 |
""" |
| 91 |
|
| 92 |
EXAMPLES = r""" |
| 93 |
- name: Register nginx service with the local Consul agent |
| 94 |
community.general.consul_agent_service: |
| 95 |
host: consul1.example.com |
| 96 |
token: some_management_acl |
| 97 |
name: nginx |
| 98 |
service_port: 80 |
| 99 |
|
| 100 |
- name: Register nginx with a tcp check |
| 101 |
community.general.consul_agent_service: |
| 102 |
host: consul1.example.com |
| 103 |
token: some_management_acl |
| 104 |
name: nginx |
| 105 |
service_port: 80 |
| 106 |
|
| 107 |
- name: Register nginx with an http check |
| 108 |
community.general.consul_agent_service: |
| 109 |
host: consul1.example.com |
| 110 |
token: some_management_acl |
| 111 |
name: nginx |
| 112 |
service_port: 80 |
| 113 |
|
| 114 |
- name: Register external service nginx available at 10.1.5.23 |
| 115 |
community.general.consul_agent_service: |
| 116 |
host: consul1.example.com |
| 117 |
token: some_management_acl |
| 118 |
name: nginx |
| 119 |
service_port: 80 |
| 120 |
address: 10.1.5.23 |
| 121 |
|
| 122 |
- name: Register nginx with some service tags |
| 123 |
community.general.consul_agent_service: |
| 124 |
host: consul1.example.com |
| 125 |
token: some_management_acl |
| 126 |
name: nginx |
| 127 |
service_port: 80 |
| 128 |
tags: |
| 129 |
- prod |
| 130 |
- webservers |
| 131 |
|
| 132 |
- name: Register nginx with some service meta |
| 133 |
community.general.consul_agent_service: |
| 134 |
host: consul1.example.com |
| 135 |
token: some_management_acl |
| 136 |
name: nginx |
| 137 |
service_port: 80 |
| 138 |
meta: |
| 139 |
nginx_version: 1.25.3 |
| 140 |
|
| 141 |
- name: Remove nginx service |
| 142 |
community.general.consul_agent_service: |
| 143 |
host: consul1.example.com |
| 144 |
token: some_management_acl |
| 145 |
service_id: nginx |
| 146 |
state: absent |
| 147 |
|
| 148 |
- name: Register celery worker service |
| 149 |
community.general.consul_agent_service: |
| 150 |
host: consul1.example.com |
| 151 |
token: some_management_acl |
| 152 |
name: celery-worker |
| 153 |
tags: |
| 154 |
- prod |
| 155 |
- worker |
| 156 |
""" |
| 157 |
|
| 158 |
RETURN = r""" |
| 159 |
service: |
| 160 |
description: The service as returned by the Consul HTTP API. |
| 161 |
returned: always |
| 162 |
type: dict |
| 163 |
sample: |
| 164 |
ID: nginx |
| 165 |
Service: nginx |
| 166 |
Address: localhost |
| 167 |
Port: 80 |
| 168 |
Tags: |
| 169 |
- http |
| 170 |
Meta: |
| 171 |
- nginx_version: 1.23.3 |
| 172 |
Datacenter: dc1 |
| 173 |
Weights: |
| 174 |
Passing: 1 |
| 175 |
Warning: 1 |
| 176 |
ContentHash: 61a245cd985261ac |
| 177 |
EnableTagOverride: false |
| 178 |
operation: |
| 179 |
description: The operation performed. |
| 180 |
returned: changed |
| 181 |
type: str |
| 182 |
sample: update |
| 183 |
""" |
| 184 |
|
| 185 |
from ansible.module_utils.basic import AnsibleModule |
| 186 |
|
| 187 |
from ansible_collections.community.general.plugins.module_utils._consul import ( |
| 188 |
AUTH_ARGUMENTS_SPEC, |
| 189 |
OPERATION_CREATE, |
| 190 |
OPERATION_DELETE, |
| 191 |
OPERATION_UPDATE, |
| 192 |
_ConsulModule, |
| 193 |
) |
| 194 |
|
| 195 |
_CHECK_MUTUALLY_EXCLUSIVE = [("args", "ttl", "tcp", "http")] |
| 196 |
_CHECK_REQUIRED_BY = { |
| 197 |
"args": "interval", |
| 198 |
"http": "interval", |
| 199 |
"tcp": "interval", |
| 200 |
} |
| 201 |
|
| 202 |
_ARGUMENT_SPEC = { |
| 203 |
"state": dict(default="present", choices=["present", "absent"]), |
| 204 |
"name": dict(type="str"), |
| 205 |
"id": dict(type="str"), |
| 206 |
"tags": dict(type="list", elements="str"), |
| 207 |
"address": dict(type="str"), |
| 208 |
"meta": dict(type="dict"), |
| 209 |
"service_port": dict(type="int"), |
| 210 |
"enable_tag_override": dict(type="bool", default=False), |
| 211 |
"weights": dict( |
| 212 |
type="dict", |
| 213 |
options=dict(passing=dict(type="int", default=1, no_log=False), warning=dict(type="int", default=1)), |
| 214 |
default={"passing": 1, "warning": 1}, |
| 215 |
), |
| 216 |
} |
| 217 |
|
| 218 |
_REQUIRED_IF = [ |
| 219 |
("state", "present", ["name"]), |
| 220 |
("state", "absent", ("id", "name"), True), |
| 221 |
] |
| 222 |
|
| 223 |
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC) |
| 224 |
|
| 225 |
|
| 226 |
class ConsulAgentServiceModule(_ConsulModule): |
| 227 |
api_endpoint = "agent/service" |
| 228 |
result_key = "service" |
| 229 |
unique_identifiers = ["id", "name"] |
| 230 |
operational_attributes = {"Service", "ContentHash", "Datacenter"} |
| 231 |
|
| 232 |
def endpoint_url(self, operation, identifier=None): |
| 233 |
if operation in [OPERATION_CREATE, OPERATION_UPDATE]: |
| 234 |
return f"{self.api_endpoint}/register" |
| 235 |
if operation == OPERATION_DELETE: |
| 236 |
return f"{self.api_endpoint}/deregister/{identifier}" |
| 237 |
|
| 238 |
return super().endpoint_url(operation, identifier) |
| 239 |
|
| 240 |
def prepare_object(self, existing, obj): |
| 241 |
existing = super().prepare_object(existing, obj) |
| 242 |
if "ServicePort" in existing: |
| 243 |
existing["Port"] = existing.pop("ServicePort") |
| 244 |
|
| 245 |
if "ID" not in existing: |
| 246 |
existing["ID"] = existing["Name"] |
| 247 |
|
| 248 |
return existing |
| 249 |
|
| 250 |
def needs_update(self, api_obj, module_obj): |
| 251 |
obj = {} |
| 252 |
if "Service" in api_obj: |
| 253 |
obj["Service"] = api_obj["Service"] |
| 254 |
api_obj = self.prepare_object(api_obj, obj) |
| 255 |
|
| 256 |
if "Name" in module_obj: |
| 257 |
module_obj["Service"] = module_obj.pop("Name") |
| 258 |
if "ServicePort" in module_obj: |
| 259 |
module_obj["Port"] = module_obj.pop("ServicePort") |
| 260 |
|
| 261 |
return super().needs_update(api_obj, module_obj) |
| 262 |
|
| 263 |
def delete_object(self, obj): |
| 264 |
if not self._module.check_mode: |
| 265 |
url = self.endpoint_url(OPERATION_DELETE, self.id_from_obj(obj, camel_case=True)) |
| 266 |
self.put(url) |
| 267 |
return {} |
| 268 |
|
| 269 |
|
| 270 |
def main(): |
| 271 |
module = AnsibleModule( |
| 272 |
_ARGUMENT_SPEC, |
| 273 |
required_if=_REQUIRED_IF, |
| 274 |
supports_check_mode=True, |
| 275 |
) |
| 276 |
|
| 277 |
consul_module = ConsulAgentServiceModule(module) |
| 278 |
consul_module.execute() |
| 279 |
|
| 280 |
|
| 281 |
if __name__ == "__main__": |
| 282 |
main() |