Skip to content

Commit

Permalink
Merge branch 'devel' of github.com:brianshumate/ansible-consul into d…
Browse files Browse the repository at this point in the history
…evel
  • Loading branch information
brianshumate committed Feb 28, 2019
2 parents daa6234 + c479536 commit 24820e5
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 4 deletions.
68 changes: 64 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -494,17 +494,17 @@ Notice that the dict object has to use precisely the names stated in the documen
- Copy from remote source if TLS files are already on host
- Default value: false

## `consul_encrypt_enable`
### `consul_encrypt_enable`

- Enable Gossip Encryption
- Default value: true

## `consul_disable_keyring_file`
### `consul_disable_keyring_file`

- If set, the keyring will not be persisted to a file. Any installed keys will be lost on shutdown, and only the given -encrypt key will be available on startup.
- Default value: false

## `consul_raw_key`
### `consul_raw_key`

- Set the encryption key; should be the same across a cluster. If not present the key will be generated & retrieved from the bootstrapped server.
- Default value: ''
Expand Down Expand Up @@ -564,7 +564,7 @@ Notice that the dict object has to use precisely the names stated in the documen

- Enable script based checks?
- Default value: false
- This is discouraged in favor of `consul_enable_local_script_checks`.
- This is discouraged in favor of `consul_enable_local_script_checks`.

### `consul_enable_local_script_checks`

Expand Down Expand Up @@ -1006,6 +1006,66 @@ By default these are named:

Then either set the environment variable `CONSUL_TLS_ENABLE=true` or use the Ansible variable `consul_tls_enable=true` at role runtime.

### Service management Support

You can create a configuration file for [consul services](https://www.consul.io/docs/agent/services.html).
Add a list of service in the `consul_services`.

| name | Required | Type | Default | Comment |
| --------------- | -------- | ---- | ------- | ---------------------------------- |
| consul_services | False | List | `[]` | List of service object (see below) |

Services object:

| name | Required | Type | Default | Comment |
| ------------------- | -------- | ------ | ------- | ---------------------------------------------------------------------------------------------------------- |
| name | True | string | | Name of the service |
| id | False | string | | Id of the service |
| tags | False | list | | List of string tags |
| address | False | string | | service-specific IP address |
| meta | False | dict | | Dict of 64 key/values with string semantics |
| port | False | int | | Port of the service |
| enable_tag_override | False | bool | | enable/disable the anti-entropy feature for the service |
| kind | False | string | | identify the service as a Connect proxy instance |
| proxy | False | dict | | [proxy configuration](https://www.consul.io/docs/connect/proxies.html#complete-configuration-example) |
| checks | False | list | | List of [checks configuration](https://www.consul.io/docs/agent/checks.html) |
| connect | False | dict | | [Connect object configuration](https://www.consul.io/docs/connect/index.html) |
| weights | False | dict | | [Weight of a service in DNS SRV responses](https://www.consul.io/docs/agent/services.html#dns-srv-weights) |


Configuration example:
```yaml
consul_services:
- name: "openshift"
tags: ['production']
- name: "redis"
id: "redis"
tags: ['primary']
address: ""
meta:
meta: "for my service"
proxy:
destination_service_name: "redis"
destination_service_id: "redis1"
local_service_address: "127.0.0.1"
local_service_port: 9090
config: {}
upstreams: []
checks:
- args: ["/home/consul/check.sh"]
interval: "10s"
```

Then you can check that the service is well added to the catalog
```
> consul catalog services
consul
openshift
redis
```
>**Note:** to delete a service that has been added from this role, remove it from the `consul_services` list and apply the role again.
### Vagrant and VirtualBox
See [examples/README_VAGRANT.md](https://github.com/brianshumate/ansible-consul/blob/master/examples/README_VAGRANT.md) for details on quick Vagrant deployments under VirtualBox for development, evaluation, testing, etc.
Expand Down
3 changes: 3 additions & 0 deletions defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -225,3 +225,6 @@ consul_snapshot_storage: "{{ consul_config_path }}/snaps"
consul_snapshot_interval: 1h
consul_snapshot_retain: 30
consul_snapshot_stale: false

# services
consul_services: []
7 changes: 7 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -489,3 +489,10 @@
when: consul_dnsmasq_enable | bool

when: ansible_os_family == 'Windows'


- name: Include services management
import_tasks: services.yml
when: consul_services is defined and consul_services|length>0
tags:
- consul_services
37 changes: 37 additions & 0 deletions tasks/services.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@

- name: "Configure consul services"
template:
dest: "{{ consul_configd_path }}/service_{{ item.name }}.json"
src: service.json.j2
owner: "{{ consul_user }}"
group: "{{ consul_group }}"
mode: 0644
with_items: "{{ consul_services }}"
notify:
- restart consul

- name: Get the list of service config file
find:
paths: "{{ consul_configd_path }}"
file_type: file
register: services_enabled

- name: Set fact with list of existing configuration file
set_fact:
list_current_service_config: "{{ list_current_service_config |default([]) + [item.path] }}"
with_items: "{{ services_enabled['files'] }}"

- name: Set fact with list of service we manage
set_fact:
managed_files: "{{ managed_files |default([]) }} + \
[ '{{ consul_configd_path }}/service_{{ item['name'] }}.json' ]"
with_items: "{{ consul_services }}"

- name: Delete non declared services
file:
path: "{{ item }}"
state: absent
when: item not in managed_files
with_items: "{{ list_current_service_config }}"
notify:
- restart consul
36 changes: 36 additions & 0 deletions templates/service.json.j2
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
"service": {
"name": "{{ item.name }}",
{% if item.id is defined -%}
"id": "{{ item.id }}",
{% endif -%}
{% if item.port is defined -%}
"port": {{ item.port }},
{% endif -%}
{% if item.address is defined -%}
"address": "{{ item.address }}",
{% endif -%}
{% if item.enable_tag_override is defined -%}
"enable_tag_override": {{ item.enable_tag_override }},
{% endif -%}
{% if item.kind is defined -%}
"kind": "{{ item.kind }}",
{% endif -%}
{% if item.proxy is defined -%}
"proxy": {{ item.proxy | to_json }},
{% endif -%}
{% if item.meta is defined -%}
"meta": {{ item.meta | to_json }},
{% endif -%}
{% if item.checks is defined -%}
"checks": {{ item.checks | to_json }},
{% endif -%}
{% if item.connect is defined -%}
"connect": {{ item.connect | to_json }},
{% endif -%}
{% if item.weights is defined -%}
"weights": {{ item.weights | to_json }},
{% endif -%}
"tags": {% if item.tags is defined -%}{{ item.tags|to_json }}{% else %}[]{% endif -%}
}
}

0 comments on commit 24820e5

Please sign in to comment.