Fix consul module service deregistration (#34847)

* Fix consul module service deregistration

Upstream pr in the python-consul library:
https://github.com/cablehead/python-consul/pull/174

This is based on work from https://github.com/bobh

Fixes ansible/ansible#34628

* Pass ACL token when deregistering consul service
pull/37086/head
Alvaro Aleman 7 years ago committed by ansibot
parent d984afa5ba
commit c9cb0016a0

@ -228,6 +228,16 @@ EXAMPLES = '''
try: try:
import consul import consul
from requests.exceptions import ConnectionError 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 python_consul_installed = True
except ImportError: except ImportError:
python_consul_installed = False python_consul_installed = False
@ -337,18 +347,20 @@ def remove_service(module, service_id):
consul_api = get_consul_api(module) consul_api = get_consul_api(module)
service = get_service_by_id_or_name(consul_api, service_id) service = get_service_by_id_or_name(consul_api, service_id)
if service: if service:
consul_api.agent.service.deregister(service_id) consul_api.agent.service.deregister(service_id, token=module.params.get('token'))
module.exit_json(changed=True, id=service_id) module.exit_json(changed=True, id=service_id)
module.exit_json(changed=False, id=service_id) module.exit_json(changed=False, id=service_id)
def get_consul_api(module, token=None): def get_consul_api(module, token=None):
return consul.Consul(host=module.params.get('host'), consulClient = consul.Consul(host=module.params.get('host'),
port=module.params.get('port'), port=module.params.get('port'),
scheme=module.params.get('scheme'), scheme=module.params.get('scheme'),
verify=module.params.get('validate_certs'), verify=module.params.get('validate_certs'),
token=module.params.get('token')) token=module.params.get('token'))
consulClient.agent.service = PatchedConsulAgentService(consulClient)
return consulClient
def get_service_by_id_or_name(consul_api, service_id_or_name): def get_service_by_id_or_name(consul_api, service_id_or_name):

Loading…
Cancel
Save