mirror of
https://github.com/ansible-collections/community.general.git
synced 2025-04-23 10:51:24 -07:00
[PR #7989/03966624 backport][stable-9] Consul implement agent service and check (#8513)
Consul implement agent service and check (#7989)
* Implement agent service and check (#7987)
* implement update of service and check
* update tests
update documentation
* update documentation
* add consul_agent_check/service to action_groups
check if unique_identifier of name is in params to get object
add suggested improvements
* update sanity
* fix sanity issues
update documentation
* fix naming
* fix naming
check if response_data has data
* fix sanity extra-docs
* add as ignore maintainer in BOTMETA.yml
update version_added to 8.4
* fix sanity
* add to maintainers
* Update plugins/modules/consul_agent_check.py
Co-authored-by: Felix Fontein <felix@fontein.de>
* Update plugins/modules/consul_agent_check.py
Co-authored-by: Felix Fontein <felix@fontein.de>
* Update plugins/modules/consul_agent_check.py
Co-authored-by: Felix Fontein <felix@fontein.de>
* update version_added
* if create and update return no object as result we read the object again
* get_first_appearing_identifier check the params for the given identifier and return it to simplify id vs name
* add unique_identifiers as a new property and a method to decide which identifier should be used
* fix sanity
* add self to team consul
remove params with no values
add operational_attributes that inherited classes can set them
get identifier value from object
* fix sanity
fix test
* remove the possibility to add checks with consul_agent_check.
check if service has changed
* remove tests for idempotency check because for checks it is not possible
* remove unique_identifier from consul.py
change unique_identifier to unique_identifiers
* get id from params
* Revert "remove unique_identifier from consul.py"
This reverts commit a4f0d0220dd23e95871914b152c25ff352097a2c.
* update version to 8.5
* Revert "Revert "remove unique_identifier from consul.py""
This reverts commit d2c35cf04c8aaf5f0175d772f862a796e22e35d4.
* update description
update test
* fix sanity tests
* fix sanity tests
* update documentation for agent_check
* fix line length
* add documentation
* fix sanity
* simplified check for Tcp
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
* check duration with regex
* fix
* update documentation
---------
Co-authored-by: Felix Fontein <felix@fontein.de>
Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
(cherry picked from commit 03966624ba
)
Co-authored-by: Ilgmi <michael.ilg@mailbox.org>
This commit is contained in:
parent
d6168a196b
commit
2d26fba0b9
13 changed files with 810 additions and 21 deletions
289
plugins/modules/consul_agent_service.py
Normal file
289
plugins/modules/consul_agent_service.py
Normal file
|
@ -0,0 +1,289 @@
|
|||
#!/usr/bin/python
|
||||
# -*- coding: utf-8 -*-
|
||||
|
||||
# Copyright (c) 2024, Michael Ilg
|
||||
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
|
||||
# SPDX-License-Identifier: GPL-3.0-or-later
|
||||
|
||||
from __future__ import absolute_import, division, print_function
|
||||
|
||||
__metaclass__ = type
|
||||
|
||||
DOCUMENTATION = '''
|
||||
module: consul_agent_service
|
||||
short_description: Add, modify and delete services within a consul cluster
|
||||
version_added: 9.1.0
|
||||
description:
|
||||
- Allows the addition, modification and deletion of services in a consul
|
||||
cluster via the agent.
|
||||
- There are currently no plans to create services and checks in one.
|
||||
This is because the Consul API does not provide checks for a service and
|
||||
the checks themselves do not match the module parameters.
|
||||
Therefore, only a service without checks can be created in this module.
|
||||
author:
|
||||
- Michael Ilg (@Ilgmi)
|
||||
extends_documentation_fragment:
|
||||
- community.general.consul
|
||||
- community.general.consul.actiongroup_consul
|
||||
- community.general.consul.token
|
||||
- community.general.attributes
|
||||
attributes:
|
||||
check_mode:
|
||||
support: full
|
||||
diff_mode:
|
||||
support: partial
|
||||
details:
|
||||
- In check mode the diff will miss operational attributes.
|
||||
options:
|
||||
state:
|
||||
description:
|
||||
- Whether the service should be present or absent.
|
||||
choices: ['present', 'absent']
|
||||
default: present
|
||||
type: str
|
||||
name:
|
||||
description:
|
||||
- Unique name for the service on a node, must be unique per node,
|
||||
required if registering a service.
|
||||
type: str
|
||||
id:
|
||||
description:
|
||||
- Specifies a unique ID for this service. This must be unique per agent. This defaults to the O(name) parameter if not provided.
|
||||
If O(state=absent), defaults to the service name if supplied.
|
||||
type: str
|
||||
tags:
|
||||
description:
|
||||
- Tags that will be attached to the service registration.
|
||||
type: list
|
||||
elements: str
|
||||
address:
|
||||
description:
|
||||
- The address to advertise that the service will be listening on.
|
||||
This value will be passed as the C(address) parameter to Consul's
|
||||
C(/v1/agent/service/register) API method, so refer to the Consul API
|
||||
documentation for further details.
|
||||
type: str
|
||||
meta:
|
||||
description:
|
||||
- Optional meta data used for filtering.
|
||||
For keys, the characters C(A-Z), C(a-z), C(0-9), C(_), C(-) are allowed.
|
||||
Not allowed characters are replaced with underscores.
|
||||
type: dict
|
||||
service_port:
|
||||
description:
|
||||
- The port on which the service is listening. Can optionally be supplied for
|
||||
registration of a service, that is if O(name) or O(id) is set.
|
||||
type: int
|
||||
enable_tag_override:
|
||||
description:
|
||||
- Specifies to disable the anti-entropy feature for this service's tags.
|
||||
If EnableTagOverride is set to true then external agents can update this service in the catalog and modify the tags.
|
||||
type: bool
|
||||
default: False
|
||||
weights:
|
||||
description:
|
||||
- Specifies weights for the service
|
||||
type: dict
|
||||
suboptions:
|
||||
passing:
|
||||
description:
|
||||
- Weights for passing.
|
||||
type: int
|
||||
default: 1
|
||||
warning:
|
||||
description:
|
||||
- Weights for warning.
|
||||
type: int
|
||||
default: 1
|
||||
default: {"passing": 1, "warning": 1}
|
||||
'''
|
||||
|
||||
EXAMPLES = '''
|
||||
- name: Register nginx service with the local consul agent
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
|
||||
- name: Register nginx with a tcp check
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
|
||||
- name: Register nginx with an http check
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
|
||||
- name: Register external service nginx available at 10.1.5.23
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
address: 10.1.5.23
|
||||
|
||||
- name: Register nginx with some service tags
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
tags:
|
||||
- prod
|
||||
- webservers
|
||||
|
||||
- name: Register nginx with some service meta
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: nginx
|
||||
service_port: 80
|
||||
meta:
|
||||
nginx_version: 1.25.3
|
||||
|
||||
- name: Remove nginx service
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
service_id: nginx
|
||||
state: absent
|
||||
|
||||
- name: Register celery worker service
|
||||
community.general.consul_agent_service:
|
||||
host: consul1.example.com
|
||||
token: some_management_acl
|
||||
name: celery-worker
|
||||
tags:
|
||||
- prod
|
||||
- worker
|
||||
'''
|
||||
|
||||
RETURN = """
|
||||
service:
|
||||
description: The service as returned by the consul HTTP API.
|
||||
returned: always
|
||||
type: dict
|
||||
sample:
|
||||
ID: nginx
|
||||
Service: nginx
|
||||
Address: localhost
|
||||
Port: 80
|
||||
Tags:
|
||||
- http
|
||||
Meta:
|
||||
- nginx_version: 1.23.3
|
||||
Datacenter: dc1
|
||||
Weights:
|
||||
Passing: 1
|
||||
Warning: 1
|
||||
ContentHash: 61a245cd985261ac
|
||||
EnableTagOverride: false
|
||||
operation:
|
||||
description: The operation performed.
|
||||
returned: changed
|
||||
type: str
|
||||
sample: update
|
||||
"""
|
||||
|
||||
from ansible.module_utils.basic import AnsibleModule
|
||||
from ansible_collections.community.general.plugins.module_utils.consul import (
|
||||
AUTH_ARGUMENTS_SPEC,
|
||||
OPERATION_CREATE,
|
||||
OPERATION_UPDATE,
|
||||
OPERATION_DELETE,
|
||||
_ConsulModule
|
||||
)
|
||||
|
||||
_CHECK_MUTUALLY_EXCLUSIVE = [('args', 'ttl', 'tcp', 'http')]
|
||||
_CHECK_REQUIRED_BY = {
|
||||
'args': 'interval',
|
||||
'http': 'interval',
|
||||
'tcp': 'interval',
|
||||
}
|
||||
|
||||
_ARGUMENT_SPEC = {
|
||||
"state": dict(default="present", choices=["present", "absent"]),
|
||||
"name": dict(type='str'),
|
||||
"id": dict(type='str'),
|
||||
"tags": dict(type='list', elements='str'),
|
||||
"address": dict(type='str'),
|
||||
"meta": dict(type='dict'),
|
||||
"service_port": dict(type='int'),
|
||||
"enable_tag_override": dict(type='bool', default=False),
|
||||
"weights": dict(type='dict', options=dict(
|
||||
passing=dict(type='int', default=1, no_log=False),
|
||||
warning=dict(type='int', default=1)
|
||||
), default={"passing": 1, "warning": 1})
|
||||
}
|
||||
|
||||
_REQUIRED_IF = [
|
||||
('state', 'present', ['name']),
|
||||
('state', 'absent', ('id', 'name'), True),
|
||||
]
|
||||
|
||||
_ARGUMENT_SPEC.update(AUTH_ARGUMENTS_SPEC)
|
||||
|
||||
|
||||
class ConsulAgentServiceModule(_ConsulModule):
|
||||
api_endpoint = "agent/service"
|
||||
result_key = "service"
|
||||
unique_identifiers = ["id", "name"]
|
||||
operational_attributes = {"Service", "ContentHash", "Datacenter"}
|
||||
|
||||
def endpoint_url(self, operation, identifier=None):
|
||||
if operation in [OPERATION_CREATE, OPERATION_UPDATE]:
|
||||
return "/".join([self.api_endpoint, "register"])
|
||||
if operation == OPERATION_DELETE:
|
||||
return "/".join([self.api_endpoint, "deregister", identifier])
|
||||
|
||||
return super(ConsulAgentServiceModule, self).endpoint_url(operation, identifier)
|
||||
|
||||
def prepare_object(self, existing, obj):
|
||||
existing = super(ConsulAgentServiceModule, self).prepare_object(existing, obj)
|
||||
if "ServicePort" in existing:
|
||||
existing["Port"] = existing.pop("ServicePort")
|
||||
|
||||
if "ID" not in existing:
|
||||
existing["ID"] = existing["Name"]
|
||||
|
||||
return existing
|
||||
|
||||
def needs_update(self, api_obj, module_obj):
|
||||
obj = {}
|
||||
if "Service" in api_obj:
|
||||
obj["Service"] = api_obj["Service"]
|
||||
api_obj = self.prepare_object(api_obj, obj)
|
||||
|
||||
if "Name" in module_obj:
|
||||
module_obj["Service"] = module_obj.pop("Name")
|
||||
if "ServicePort" in module_obj:
|
||||
module_obj["Port"] = module_obj.pop("ServicePort")
|
||||
|
||||
return super(ConsulAgentServiceModule, self).needs_update(api_obj, module_obj)
|
||||
|
||||
def delete_object(self, obj):
|
||||
if not self._module.check_mode:
|
||||
url = self.endpoint_url(OPERATION_DELETE, self.id_from_obj(obj, camel_case=True))
|
||||
self.put(url)
|
||||
return {}
|
||||
|
||||
|
||||
def main():
|
||||
module = AnsibleModule(
|
||||
_ARGUMENT_SPEC,
|
||||
required_if=_REQUIRED_IF,
|
||||
supports_check_mode=True,
|
||||
)
|
||||
|
||||
consul_module = ConsulAgentServiceModule(module)
|
||||
consul_module.execute()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
Loading…
Add table
Add a link
Reference in a new issue