community.general.consul (8.5.0) — module

Add, modify & delete services within a consul cluster

Authors: Steve Gargan (@sgargan)

Install collection

Install with ansible-galaxy collection install community.general:==8.5.0


Add to requirements.yml

  collections:
    - name: community.general
      version: 8.5.0

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 O(check_name) and optionally a O(check_id).

Currently, there is no complete way to retrieve the script, interval or TTL metadata for a registered check. Without this metadata it is not possible to 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

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register nginx service with the local consul agent
  community.general.consul:
    service_name: nginx
    service_port: 80
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register nginx service with curl check
  community.general.consul:
    service_name: nginx
    service_port: 80
    script: curl http://localhost
    interval: 60s
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: register nginx with a tcp check
  community.general.consul:
    service_name: nginx
    service_port: 80
    interval: 60s
    tcp: localhost:80
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register nginx with an http check
  community.general.consul:
    service_name: nginx
    service_port: 80
    interval: 60s
    http: http://localhost:80/status
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register external service nginx available at 10.1.5.23
  community.general.consul:
    service_name: nginx
    service_port: 80
    service_address: 10.1.5.23
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register nginx with some service tags
  community.general.consul:
    service_name: nginx
    service_port: 80
    tags:
      - prod
      - webservers
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Remove nginx service
  community.general.consul:
    service_name: nginx
    state: absent
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register celery worker service
  community.general.consul:
    service_name: celery-worker
    tags:
      - prod
      - worker
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Create a node level check to test disk usage
  community.general.consul:
    check_name: Disk usage
    check_id: disk_usage
    script: /opt/disk_usage.py
    interval: 5m
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Register an http check against a service that's already registered
  community.general.consul:
    check_name: nginx-check2
    check_id: nginx-check2
    service_id: nginx
    interval: 60s
    http: http://localhost:80/morestatus

Inputs

    
tcp:
    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 V(host:port), for example V(localhost:80).
    - Requires O(interval) to be provided.
    - Mutually exclusive with O(script), O(ttl) and O(http).
    type: str
    version_added: 1.3.0
    version_added_collection: community.general

ttl:
    description:
    - Checks can be registered with a TTL instead of a O(script) and O(interval) 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 Similar to the interval this is a number with
      a V(s) 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) will be used by default, for example V(10)
      will be V(10s).
    - Mutually exclusive with O(script), O(tcp) and O(http).
    type: str

host:
    default: localhost
    description:
    - Host of the consul agent defaults to localhost.
    type: str

http:
    description:
    - Checks can be registered with an HTTP endpoint. This means that consul will check
      that the http endpoint returns a successful HTTP status.
    - Requires O(interval) to be provided.
    - Mutually exclusive with O(script), O(ttl) and O(tcp).
    type: str

port:
    default: 8500
    description:
    - The port on which the consul agent is running.
    type: int

tags:
    description:
    - Tags that will be attached to the service registration.
    elements: str
    type: list

notes:
    description:
    - Notes to attach to check when registering it.
    type: str

state:
    choices:
    - present
    - absent
    default: present
    description:
    - Register or deregister the consul service, defaults to present.
    type: str

token:
    description:
    - The token key identifying an ACL rule set. May be required to register services.
    type: str

scheme:
    default: http
    description:
    - The protocol scheme on which the consul agent is running.
    type: str

script:
    description:
    - The script/command that will be run periodically to check the health of the service.
    - Requires O(interval) to be provided.
    - Mutually exclusive with O(ttl), O(tcp) and O(http).
    type: str

timeout:
    description:
    - A custom HTTP check timeout. The consul default is 10 seconds. Similar to the interval
      this is a number with a V(s) 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) will be used by default,
      for example V(10) will be V(10s).
    type: str

check_id:
    description:
    - An ID for the service check. If O(state=absent), defaults to O(check_name). Ignored
      if part of a service definition.
    type: str

interval:
    description:
    - The interval at which the service check will be run. This is a number with a V(s)
      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) will be used by default, for example V(10)
      will be V(10s).
    - Required if one of the parameters O(script), O(http), or O(tcp) is specified.
    type: str

check_host:
    description:
    - Host name.
    type: str

check_name:
    description:
    - Name for the service check. Required if standalone, ignored if part of service definition.
    type: str

check_node:
    description:
    - Node name.
    type: str

service_id:
    description:
    - The ID for the service, must be unique per node. If O(state=absent), defaults to
      the service name if supplied.
    type: str

service_name:
    description:
    - Unique name for the service on a node, must be unique per node, required if registering
      a service. May be omitted if registering a node level check.
    type: str

service_port:
    description:
    - The port on which the service is listening. Can optionally be supplied for registration
      of a service, that is if O(service_name) or O(service_id) is set.
    type: int

validate_certs:
    default: true
    description:
    - Whether to verify the TLS certificate of the consul agent.
    type: bool

service_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

ack_params_state_absent:
    description:
    - This parameter has no more effect and is deprecated. It will be removed in community.general
      10.0.0.
    type: bool