2020-03-09 10:11:07 +01:00
|
|
|
#!/usr/bin/python
|
2021-08-08 10:40:22 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
2020-03-09 10:11:07 +01:00
|
|
|
#
|
2022-08-05 22:12:10 +02:00
|
|
|
# Copyright (c) 2015, Steve Gargan <steve.gargan@gmail.com>
|
2022-08-05 12:28:29 +02:00
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
|
|
|
|
|
|
|
DOCUMENTATION = '''
|
|
|
|
module: consul
|
2022-11-09 14:11:41 +01:00
|
|
|
short_description: Add, modify & delete services within a consul cluster
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
|
|
|
- Registers services and checks for an agent with a consul cluster.
|
|
|
|
A service is some process running on the agent node that should be advertised by
|
|
|
|
consul's discovery mechanism. It may optionally supply a check definition,
|
|
|
|
a periodic service test to notify the consul cluster of service's health.
|
|
|
|
- "Checks may also be registered per node e.g. disk usage, or cpu usage and
|
|
|
|
notify the health of the entire node to the cluster.
|
|
|
|
Service level checks do not require a check name or id as these are derived
|
|
|
|
by Consul from the Service name and id respectively by appending 'service:'
|
|
|
|
Node level checks require a I(check_name) and optionally a I(check_id)."
|
|
|
|
- Currently, there is no complete way to retrieve the script, interval or ttl
|
2023-01-06 15:17:46 +01:00
|
|
|
metadata for a registered check. Without this metadata it is not possible to
|
2020-03-09 10:11:07 +01:00
|
|
|
tell if the data supplied with ansible represents a change to a check. As a
|
|
|
|
result this does not attempt to determine changes and will always report a
|
|
|
|
changed occurred. An API method is planned to supply this metadata so at that
|
|
|
|
stage change management will be added.
|
|
|
|
- "See U(http://consul.io) for more details."
|
|
|
|
requirements:
|
|
|
|
- python-consul
|
|
|
|
- requests
|
|
|
|
author: "Steve Gargan (@sgargan)"
|
|
|
|
options:
|
|
|
|
state:
|
2021-02-26 11:37:23 +01:00
|
|
|
type: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Register or deregister the consul service, defaults to present.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: present
|
|
|
|
choices: ['present', 'absent']
|
|
|
|
service_name:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Unique name for the service on a node, must be unique per node,
|
|
|
|
required if registering a service. May be omitted if registering
|
2023-01-06 15:17:46 +01:00
|
|
|
a node level check.
|
2020-03-09 10:11:07 +01:00
|
|
|
service_id:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The ID for the service, must be unique per node. If I(state=absent),
|
2020-03-09 10:11:07 +01:00
|
|
|
defaults to the service name if supplied.
|
|
|
|
host:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Host of the consul agent defaults to localhost.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: localhost
|
|
|
|
port:
|
|
|
|
type: int
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The port on which the consul agent is running.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: 8500
|
|
|
|
scheme:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The protocol scheme on which the consul agent is running.
|
2020-03-09 10:11:07 +01:00
|
|
|
default: http
|
|
|
|
validate_certs:
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Whether to verify the TLS certificate of the consul agent.
|
2020-03-09 10:11:07 +01:00
|
|
|
type: bool
|
2022-08-24 20:00:11 +02:00
|
|
|
default: true
|
2020-03-09 10:11:07 +01:00
|
|
|
notes:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Notes to attach to check when registering it.
|
|
|
|
service_port:
|
|
|
|
type: int
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The port on which the service is listening. Can optionally be supplied for
|
|
|
|
registration of a service, i.e. if I(service_name) or I(service_id) is set.
|
2020-03-09 10:11:07 +01:00
|
|
|
service_address:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The address to advertise that the service will be listening on.
|
2020-03-09 10:11:07 +01:00
|
|
|
This value will be passed as the I(address) parameter to Consul's
|
2021-11-09 19:59:34 +01:00
|
|
|
C(/v1/agent/service/register) API method, so refer to the Consul API
|
2020-03-09 10:11:07 +01:00
|
|
|
documentation for further details.
|
|
|
|
tags:
|
|
|
|
type: list
|
2021-02-26 11:37:23 +01:00
|
|
|
elements: str
|
2020-03-09 10:11:07 +01:00
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Tags that will be attached to the service registration.
|
2020-03-09 10:11:07 +01:00
|
|
|
script:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The script/command that will be run periodically to check the health of the service.
|
|
|
|
- Requires I(interval) to be provided.
|
2020-03-09 10:11:07 +01:00
|
|
|
interval:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The interval at which the service check will be run.
|
|
|
|
This is a number with a C(s) or C(m) suffix to signify the units of seconds or minutes e.g C(15s) or C(1m).
|
|
|
|
If no suffix is supplied C(s) will be used by default, e.g. C(10) will be C(10s).
|
|
|
|
- Required if one of the parameters I(script), I(http), or I(tcp) is specified.
|
2020-03-09 10:11:07 +01:00
|
|
|
check_id:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- An ID for the service check. If I(state=absent), defaults to
|
2020-03-09 10:11:07 +01:00
|
|
|
I(check_name). Ignored if part of a service definition.
|
|
|
|
check_name:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Name for the service check. Required if standalone, ignored if
|
2020-03-09 10:11:07 +01:00
|
|
|
part of service definition.
|
|
|
|
ttl:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Checks can be registered with a ttl instead of a I(script) and I(interval)
|
2020-03-09 10:11:07 +01:00
|
|
|
this means that the service will check in with the agent before the
|
|
|
|
ttl expires. If it doesn't the check will be considered failed.
|
|
|
|
Required if registering a check and the script an interval are missing
|
2023-01-06 15:17:46 +01:00
|
|
|
Similar to the interval this is a number with a C(s) or C(m) suffix to
|
|
|
|
signify the units of seconds or minutes e.g C(15s) or C(1m).
|
|
|
|
If no suffix is supplied C(s) will be used by default, e.g. C(10) will be C(10s).
|
2020-10-30 06:18:48 +01:00
|
|
|
tcp:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- Checks can be registered with a TCP port. This means that consul
|
|
|
|
will check if the connection attempt to that port is successful (that is, the port is currently accepting connections).
|
|
|
|
The format is C(host:port), for example C(localhost:80).
|
2023-01-06 15:17:46 +01:00
|
|
|
- Requires I(interval) to be provided.
|
2020-10-30 06:18:48 +01:00
|
|
|
version_added: '1.3.0'
|
2020-03-09 10:11:07 +01:00
|
|
|
http:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- Checks can be registered with an HTTP endpoint. This means that consul
|
2020-03-09 10:11:07 +01:00
|
|
|
will check that the http endpoint returns a successful HTTP status.
|
2023-01-06 15:17:46 +01:00
|
|
|
- Requires I(interval) to be provided.
|
2020-03-09 10:11:07 +01:00
|
|
|
timeout:
|
|
|
|
type: str
|
|
|
|
description:
|
|
|
|
- A custom HTTP check timeout. The consul default is 10 seconds.
|
|
|
|
Similar to the interval this is a number with a C(s) or C(m) suffix to
|
|
|
|
signify the units of seconds or minutes, e.g. C(15s) or C(1m).
|
2023-01-06 15:17:46 +01:00
|
|
|
If no suffix is supplied C(s) will be used by default, e.g. C(10) will be C(10s).
|
2020-03-09 10:11:07 +01:00
|
|
|
token:
|
|
|
|
type: str
|
|
|
|
description:
|
2023-01-06 15:17:46 +01:00
|
|
|
- The token key identifying an ACL rule set. May be required to register services.
|
2023-01-13 21:23:08 +01:00
|
|
|
ack_params_state_absent:
|
|
|
|
type: bool
|
|
|
|
description:
|
|
|
|
- Disable deprecation warning when using parameters incompatible with I(state=absent).
|
2020-03-09 10:11:07 +01:00
|
|
|
'''
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register nginx service with the local consul agent
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register nginx service with curl check
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
script: curl http://localhost
|
|
|
|
interval: 60s
|
|
|
|
|
2020-10-30 06:18:48 +01:00
|
|
|
- name: register nginx with a tcp check
|
|
|
|
community.general.consul:
|
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
interval: 60s
|
|
|
|
tcp: localhost:80
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register nginx with an http check
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
interval: 60s
|
|
|
|
http: http://localhost:80/status
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register external service nginx available at 10.1.5.23
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
service_address: 10.1.5.23
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register nginx with some service tags
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
service_port: 80
|
|
|
|
tags:
|
|
|
|
- prod
|
|
|
|
- webservers
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Remove nginx service
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: nginx
|
|
|
|
state: absent
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register celery worker service
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
service_name: celery-worker
|
|
|
|
tags:
|
|
|
|
- prod
|
|
|
|
- worker
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Create a node level check to test disk usage
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
check_name: Disk usage
|
|
|
|
check_id: disk_usage
|
|
|
|
script: /opt/disk_usage.py
|
|
|
|
interval: 5m
|
|
|
|
|
2020-05-16 15:07:51 +02:00
|
|
|
- name: Register an http check against a service that's already registered
|
2020-07-13 21:50:31 +02:00
|
|
|
community.general.consul:
|
2020-03-09 10:11:07 +01:00
|
|
|
check_name: nginx-check2
|
|
|
|
check_id: nginx-check2
|
|
|
|
service_id: nginx
|
|
|
|
interval: 60s
|
|
|
|
http: http://localhost:80/morestatus
|
|
|
|
'''
|
|
|
|
|
|
|
|
try:
|
|
|
|
import consul
|
|
|
|
from requests.exceptions import ConnectionError
|
|
|
|
|
|
|
|
class PatchedConsulAgentService(consul.Consul.Agent.Service):
|
|
|
|
def deregister(self, service_id, token=None):
|
|
|
|
params = {}
|
|
|
|
if token:
|
|
|
|
params['token'] = token
|
|
|
|
return self.agent.http.put(consul.base.CB.bool(),
|
|
|
|
'/v1/agent/service/deregister/%s' % service_id,
|
|
|
|
params=params)
|
|
|
|
|
|
|
|
python_consul_installed = True
|
|
|
|
except ImportError:
|
|
|
|
python_consul_installed = False
|
|
|
|
|
2020-10-30 06:18:48 +01:00
|
|
|
import re
|
2020-03-09 10:11:07 +01:00
|
|
|
from ansible.module_utils.basic import AnsibleModule
|
|
|
|
|
|
|
|
|
|
|
|
def register_with_consul(module):
|
2022-10-18 09:17:13 +02:00
|
|
|
state = module.params['state']
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
if state == 'present':
|
|
|
|
add(module)
|
|
|
|
else:
|
|
|
|
remove(module)
|
|
|
|
|
|
|
|
|
|
|
|
def add(module):
|
|
|
|
''' adds a service or a check depending on supplied configuration'''
|
|
|
|
check = parse_check(module)
|
|
|
|
service = parse_service(module)
|
|
|
|
|
|
|
|
if not service and not check:
|
|
|
|
module.fail_json(msg='a name and port are required to register a service')
|
|
|
|
|
|
|
|
if service:
|
|
|
|
if check:
|
|
|
|
service.add_check(check)
|
|
|
|
add_service(module, service)
|
|
|
|
elif check:
|
|
|
|
add_check(module, check)
|
|
|
|
|
|
|
|
|
|
|
|
def remove(module):
|
|
|
|
''' removes a service or a check '''
|
2022-10-18 09:17:13 +02:00
|
|
|
service_id = module.params['service_id'] or module.params['service_name']
|
|
|
|
check_id = module.params['check_id'] or module.params['check_name']
|
2020-03-09 10:11:07 +01:00
|
|
|
if service_id:
|
|
|
|
remove_service(module, service_id)
|
|
|
|
else:
|
|
|
|
remove_check(module, check_id)
|
|
|
|
|
|
|
|
|
|
|
|
def add_check(module, check):
|
|
|
|
''' registers a check with the given agent. currently there is no way
|
|
|
|
retrieve the full metadata of an existing check through the consul api.
|
|
|
|
Without this we can't compare to the supplied check and so we must assume
|
|
|
|
a change. '''
|
|
|
|
if not check.name and not check.service_id:
|
|
|
|
module.fail_json(msg='a check name is required for a node level check, one not attached to a service')
|
|
|
|
|
|
|
|
consul_api = get_consul_api(module)
|
|
|
|
check.register(consul_api)
|
|
|
|
|
|
|
|
module.exit_json(changed=True,
|
|
|
|
check_id=check.check_id,
|
|
|
|
check_name=check.name,
|
|
|
|
script=check.script,
|
|
|
|
interval=check.interval,
|
|
|
|
ttl=check.ttl,
|
2020-10-30 06:18:48 +01:00
|
|
|
tcp=check.tcp,
|
2020-03-09 10:11:07 +01:00
|
|
|
http=check.http,
|
|
|
|
timeout=check.timeout,
|
|
|
|
service_id=check.service_id)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_check(module, check_id):
|
|
|
|
''' removes a check using its id '''
|
|
|
|
consul_api = get_consul_api(module)
|
|
|
|
|
|
|
|
if check_id in consul_api.agent.checks():
|
|
|
|
consul_api.agent.check.deregister(check_id)
|
|
|
|
module.exit_json(changed=True, id=check_id)
|
|
|
|
|
|
|
|
module.exit_json(changed=False, id=check_id)
|
|
|
|
|
|
|
|
|
|
|
|
def add_service(module, service):
|
|
|
|
''' registers a service with the current agent '''
|
|
|
|
result = service
|
|
|
|
changed = False
|
|
|
|
|
|
|
|
consul_api = get_consul_api(module)
|
|
|
|
existing = get_service_by_id_or_name(consul_api, service.id)
|
|
|
|
|
|
|
|
# there is no way to retrieve the details of checks so if a check is present
|
|
|
|
# in the service it must be re-registered
|
|
|
|
if service.has_checks() or not existing or not existing == service:
|
|
|
|
|
|
|
|
service.register(consul_api)
|
|
|
|
# check that it registered correctly
|
|
|
|
registered = get_service_by_id_or_name(consul_api, service.id)
|
|
|
|
if registered:
|
|
|
|
result = registered
|
|
|
|
changed = True
|
|
|
|
|
|
|
|
module.exit_json(changed=changed,
|
|
|
|
service_id=result.id,
|
|
|
|
service_name=result.name,
|
|
|
|
service_port=result.port,
|
2022-05-23 07:22:15 +02:00
|
|
|
checks=[check.to_dict() for check in service.checks()],
|
2020-03-09 10:11:07 +01:00
|
|
|
tags=result.tags)
|
|
|
|
|
|
|
|
|
|
|
|
def remove_service(module, service_id):
|
|
|
|
''' deregister a service from the given agent using its service id '''
|
|
|
|
consul_api = get_consul_api(module)
|
|
|
|
service = get_service_by_id_or_name(consul_api, service_id)
|
|
|
|
if service:
|
2022-10-18 09:17:13 +02:00
|
|
|
consul_api.agent.service.deregister(service_id, token=module.params['token'])
|
2020-03-09 10:11:07 +01:00
|
|
|
module.exit_json(changed=True, id=service_id)
|
|
|
|
|
|
|
|
module.exit_json(changed=False, id=service_id)
|
|
|
|
|
|
|
|
|
2021-02-26 11:37:23 +01:00
|
|
|
def get_consul_api(module):
|
2022-10-18 09:17:13 +02:00
|
|
|
consulClient = consul.Consul(host=module.params['host'],
|
|
|
|
port=module.params['port'],
|
|
|
|
scheme=module.params['scheme'],
|
|
|
|
verify=module.params['validate_certs'],
|
|
|
|
token=module.params['token'])
|
2020-03-09 10:11:07 +01:00
|
|
|
consulClient.agent.service = PatchedConsulAgentService(consulClient)
|
|
|
|
return consulClient
|
|
|
|
|
|
|
|
|
|
|
|
def get_service_by_id_or_name(consul_api, service_id_or_name):
|
|
|
|
''' iterate the registered services and find one with the given id '''
|
2022-10-18 09:17:13 +02:00
|
|
|
for dummy, service in consul_api.agent.services().items():
|
|
|
|
if service_id_or_name in (service['ID'], service['Service']):
|
2020-03-09 10:11:07 +01:00
|
|
|
return ConsulService(loaded=service)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_check(module):
|
2022-10-18 09:17:13 +02:00
|
|
|
_checks = [module.params[p] for p in ('script', 'ttl', 'tcp', 'http') if module.params[p]]
|
|
|
|
|
|
|
|
if len(_checks) > 1:
|
2020-03-09 10:11:07 +01:00
|
|
|
module.fail_json(
|
2020-10-30 06:18:48 +01:00
|
|
|
msg='checks are either script, tcp, http or ttl driven, supplying more than one does not make sense')
|
2020-03-09 10:11:07 +01:00
|
|
|
|
2022-10-18 09:17:13 +02:00
|
|
|
if module.params['check_id'] or _checks:
|
2020-03-09 10:11:07 +01:00
|
|
|
return ConsulCheck(
|
2022-10-18 09:17:13 +02:00
|
|
|
module.params['check_id'],
|
|
|
|
module.params['check_name'],
|
|
|
|
module.params['check_node'],
|
|
|
|
module.params['check_host'],
|
|
|
|
module.params['script'],
|
|
|
|
module.params['interval'],
|
|
|
|
module.params['ttl'],
|
|
|
|
module.params['notes'],
|
|
|
|
module.params['tcp'],
|
|
|
|
module.params['http'],
|
|
|
|
module.params['timeout'],
|
|
|
|
module.params['service_id'],
|
2020-03-09 10:11:07 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
|
|
def parse_service(module):
|
2022-10-18 09:17:13 +02:00
|
|
|
return ConsulService(
|
|
|
|
module.params['service_id'],
|
|
|
|
module.params['service_name'],
|
|
|
|
module.params['service_address'],
|
|
|
|
module.params['service_port'],
|
|
|
|
module.params['tags'],
|
|
|
|
)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
|
2021-02-26 11:37:23 +01:00
|
|
|
class ConsulService(object):
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def __init__(self, service_id=None, name=None, address=None, port=-1,
|
|
|
|
tags=None, loaded=None):
|
|
|
|
self.id = self.name = name
|
|
|
|
if service_id:
|
|
|
|
self.id = service_id
|
|
|
|
self.address = address
|
|
|
|
self.port = port
|
|
|
|
self.tags = tags
|
2022-05-05 08:23:00 +02:00
|
|
|
self._checks = []
|
2020-03-09 10:11:07 +01:00
|
|
|
if loaded:
|
|
|
|
self.id = loaded['ID']
|
|
|
|
self.name = loaded['Service']
|
|
|
|
self.port = loaded['Port']
|
|
|
|
self.tags = loaded['Tags']
|
|
|
|
|
|
|
|
def register(self, consul_api):
|
|
|
|
optional = {}
|
|
|
|
|
|
|
|
if self.port:
|
|
|
|
optional['port'] = self.port
|
|
|
|
|
2022-05-05 08:23:00 +02:00
|
|
|
if len(self._checks) > 0:
|
|
|
|
optional['check'] = self._checks[0].check
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
consul_api.agent.service.register(
|
|
|
|
self.name,
|
|
|
|
service_id=self.id,
|
|
|
|
address=self.address,
|
|
|
|
tags=self.tags,
|
|
|
|
**optional)
|
|
|
|
|
|
|
|
def add_check(self, check):
|
2022-05-05 08:23:00 +02:00
|
|
|
self._checks.append(check)
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def checks(self):
|
2022-05-05 08:23:00 +02:00
|
|
|
return self._checks
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def has_checks(self):
|
2022-05-05 08:23:00 +02:00
|
|
|
return len(self._checks) > 0
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (isinstance(other, self.__class__) and
|
|
|
|
self.id == other.id and
|
|
|
|
self.name == other.name and
|
|
|
|
self.port == other.port and
|
|
|
|
self.tags == other.tags)
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
data = {'id': self.id, "name": self.name}
|
|
|
|
if self.port:
|
|
|
|
data['port'] = self.port
|
|
|
|
if self.tags and len(self.tags) > 0:
|
|
|
|
data['tags'] = self.tags
|
2022-05-05 08:23:00 +02:00
|
|
|
if len(self._checks) > 0:
|
|
|
|
data['check'] = self._checks[0].to_dict()
|
2020-03-09 10:11:07 +01:00
|
|
|
return data
|
|
|
|
|
|
|
|
|
|
|
|
class ConsulCheck(object):
|
|
|
|
|
|
|
|
def __init__(self, check_id, name, node=None, host='localhost',
|
2020-10-30 06:18:48 +01:00
|
|
|
script=None, interval=None, ttl=None, notes=None, tcp=None, http=None, timeout=None, service_id=None):
|
2020-03-09 10:11:07 +01:00
|
|
|
self.check_id = self.name = name
|
|
|
|
if check_id:
|
|
|
|
self.check_id = check_id
|
|
|
|
self.service_id = service_id
|
|
|
|
self.notes = notes
|
|
|
|
self.node = node
|
|
|
|
self.host = host
|
|
|
|
|
|
|
|
self.interval = self.validate_duration('interval', interval)
|
|
|
|
self.ttl = self.validate_duration('ttl', ttl)
|
|
|
|
self.script = script
|
2020-10-30 06:18:48 +01:00
|
|
|
self.tcp = tcp
|
2020-03-09 10:11:07 +01:00
|
|
|
self.http = http
|
|
|
|
self.timeout = self.validate_duration('timeout', timeout)
|
|
|
|
|
|
|
|
self.check = None
|
|
|
|
|
|
|
|
if script:
|
|
|
|
self.check = consul.Check.script(script, self.interval)
|
|
|
|
|
|
|
|
if ttl:
|
|
|
|
self.check = consul.Check.ttl(self.ttl)
|
|
|
|
|
|
|
|
if http:
|
|
|
|
if interval is None:
|
|
|
|
raise Exception('http check must specify interval')
|
|
|
|
|
|
|
|
self.check = consul.Check.http(http, self.interval, self.timeout)
|
|
|
|
|
2020-10-30 06:18:48 +01:00
|
|
|
if tcp:
|
|
|
|
if interval is None:
|
|
|
|
raise Exception('tcp check must specify interval')
|
|
|
|
|
2022-10-18 09:17:13 +02:00
|
|
|
regex = r"(?P<host>.*):(?P<port>(?:[0-9]+))$"
|
2020-10-30 06:18:48 +01:00
|
|
|
match = re.match(regex, tcp)
|
|
|
|
|
2022-10-18 09:17:13 +02:00
|
|
|
if not match:
|
2020-10-30 06:18:48 +01:00
|
|
|
raise Exception('tcp check must be in host:port format')
|
|
|
|
|
|
|
|
self.check = consul.Check.tcp(match.group('host').strip('[]'), int(match.group('port')), self.interval)
|
|
|
|
|
2020-03-09 10:11:07 +01:00
|
|
|
def validate_duration(self, name, duration):
|
|
|
|
if duration:
|
|
|
|
duration_units = ['ns', 'us', 'ms', 's', 'm', 'h']
|
2022-10-18 09:17:13 +02:00
|
|
|
if not any(duration.endswith(suffix) for suffix in duration_units):
|
2020-03-09 10:11:07 +01:00
|
|
|
duration = "{0}s".format(duration)
|
|
|
|
return duration
|
|
|
|
|
|
|
|
def register(self, consul_api):
|
|
|
|
consul_api.agent.check.register(self.name, check_id=self.check_id, service_id=self.service_id,
|
|
|
|
notes=self.notes,
|
|
|
|
check=self.check)
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
return (isinstance(other, self.__class__) and
|
|
|
|
self.check_id == other.check_id and
|
|
|
|
self.service_id == other.service_id and
|
|
|
|
self.name == other.name and
|
|
|
|
self.script == other.script and
|
|
|
|
self.interval == other.interval)
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self.__eq__(other)
|
|
|
|
|
|
|
|
def to_dict(self):
|
|
|
|
data = {}
|
|
|
|
self._add(data, 'id', attr='check_id')
|
|
|
|
self._add(data, 'name', attr='check_name')
|
|
|
|
self._add(data, 'script')
|
|
|
|
self._add(data, 'node')
|
|
|
|
self._add(data, 'notes')
|
|
|
|
self._add(data, 'host')
|
|
|
|
self._add(data, 'interval')
|
|
|
|
self._add(data, 'ttl')
|
2020-10-30 06:18:48 +01:00
|
|
|
self._add(data, 'tcp')
|
2020-03-09 10:11:07 +01:00
|
|
|
self._add(data, 'http')
|
|
|
|
self._add(data, 'timeout')
|
|
|
|
self._add(data, 'service_id')
|
|
|
|
return data
|
|
|
|
|
|
|
|
def _add(self, data, key, attr=None):
|
|
|
|
try:
|
|
|
|
if attr is None:
|
|
|
|
attr = key
|
|
|
|
data[key] = getattr(self, attr)
|
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
|
|
|
|
|
|
|
|
def test_dependencies(module):
|
|
|
|
if not python_consul_installed:
|
|
|
|
module.fail_json(msg="python-consul required for this module. see https://python-consul.readthedocs.io/en/latest/#installation")
|
|
|
|
|
|
|
|
|
|
|
|
def main():
|
|
|
|
module = AnsibleModule(
|
|
|
|
argument_spec=dict(
|
|
|
|
host=dict(default='localhost'),
|
|
|
|
port=dict(default=8500, type='int'),
|
2021-02-26 11:37:23 +01:00
|
|
|
scheme=dict(default='http'),
|
|
|
|
validate_certs=dict(default=True, type='bool'),
|
|
|
|
check_id=dict(),
|
|
|
|
check_name=dict(),
|
|
|
|
check_node=dict(),
|
|
|
|
check_host=dict(),
|
|
|
|
notes=dict(),
|
|
|
|
script=dict(),
|
|
|
|
service_id=dict(),
|
|
|
|
service_name=dict(),
|
|
|
|
service_address=dict(type='str'),
|
|
|
|
service_port=dict(type='int'),
|
2020-03-09 10:11:07 +01:00
|
|
|
state=dict(default='present', choices=['present', 'absent']),
|
2021-02-26 11:37:23 +01:00
|
|
|
interval=dict(type='str'),
|
|
|
|
ttl=dict(type='str'),
|
|
|
|
tcp=dict(type='str'),
|
|
|
|
http=dict(type='str'),
|
|
|
|
timeout=dict(type='str'),
|
|
|
|
tags=dict(type='list', elements='str'),
|
2023-01-13 21:23:08 +01:00
|
|
|
token=dict(no_log=True),
|
|
|
|
ack_params_state_absent=dict(type='bool'),
|
2020-03-09 10:11:07 +01:00
|
|
|
),
|
2022-10-18 09:17:13 +02:00
|
|
|
required_if=[
|
|
|
|
('state', 'present', ['service_name']),
|
|
|
|
('state', 'absent', ['service_id', 'service_name', 'check_id', 'check_name'], True),
|
|
|
|
],
|
2020-03-09 10:11:07 +01:00
|
|
|
supports_check_mode=False,
|
|
|
|
)
|
2023-01-13 21:23:08 +01:00
|
|
|
p = module.params
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
test_dependencies(module)
|
2023-01-13 21:23:08 +01:00
|
|
|
if p['state'] == 'absent' and any(p[x] for x in ['script', 'ttl', 'tcp', 'http', 'interval']) and not p['ack_params_state_absent']:
|
|
|
|
module.deprecate(
|
|
|
|
"The use of parameters 'script', 'ttl', 'tcp', 'http', 'interval' along with 'state=absent' is deprecated. "
|
|
|
|
"In community.general 8.0.0 their use will become an error. "
|
|
|
|
"To suppress this deprecation notice, set parameter ack_params_state_absent=true.",
|
|
|
|
version="8.0.0",
|
|
|
|
collection_name="community.general",
|
|
|
|
)
|
|
|
|
# When reaching c.g 8.0.0:
|
|
|
|
# - Replace the deprecation with a fail_json(), remove the "ack_params_state_absent" condition from the "if"
|
|
|
|
# - Add mutually_exclusive for ('script', 'ttl', 'tcp', 'http'), then remove that validation from parse_check()
|
|
|
|
# - Add required_by {'script': 'interval', 'http': 'interval', 'tcp': 'interval'}, then remove checks for 'interval' in ConsulCheck.__init__()
|
|
|
|
# - Deprecate the parameter ack_params_state_absent
|
2020-03-09 10:11:07 +01:00
|
|
|
|
|
|
|
try:
|
|
|
|
register_with_consul(module)
|
2023-01-13 21:23:08 +01:00
|
|
|
except SystemExit:
|
|
|
|
raise
|
2020-03-09 10:11:07 +01:00
|
|
|
except ConnectionError as e:
|
2023-01-13 21:23:08 +01:00
|
|
|
module.fail_json(msg='Could not connect to consul agent at %s:%s, error was %s' % (p['host'], p['port'], str(e)))
|
2020-03-09 10:11:07 +01:00
|
|
|
except Exception as e:
|
|
|
|
module.fail_json(msg=str(e))
|
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|