Skip to content

Commit

Permalink
Support producer tags
Browse files Browse the repository at this point in the history
  • Loading branch information
zeelax committed Feb 7, 2018
1 parent 0531c80 commit a45c794
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 2 deletions.
13 changes: 11 additions & 2 deletions tasks/consul-services.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,18 @@
chdir: "{{ consul_config_dir }}"
register: registered_consul_services

- name: consul services | Initialize a flat list
set_fact:
consul_producer_services_flat: []

- name: consul services | Populate a flat list
set_fact:
consul_producer_services_flat: "{{ consul_producer_services_flat + [ item.name if item is mapping else item ] }}"
with_items: "{{ consul_producer_services }}"

- name: consul services | Check if any services are old
set_fact:
old_services: "{{ registered_consul_services.stdout_lines | difference(consul_producer_services) }}"
old_services: "{{ registered_consul_services.stdout_lines | difference(consul_producer_services_flat) }}"

- name: consul services | Ensure old consul services JSON (if any) are deregister
file:
Expand All @@ -22,7 +31,7 @@
- name: consul services | Ensure consul services JSON are registered
template:
src="consul-service.j2"
dest="{{ consul_config_dir }}/sv_{{ item }}.json"
dest="{{ consul_config_dir }}/sv_{{ item.name if item is mapping else item }}.json"
with_items: "{{ consul_producer_services }}"
notify:
- reload consul service
22 changes: 22 additions & 0 deletions templates/consul-service.j2
Original file line number Diff line number Diff line change
@@ -1,3 +1,24 @@
{% if item is mapping %}
{% if 'weight' in consul_services[item.name] %}
{% set weight_value = consul_services[item.name].pop('weight') %}
{% if 'tags' in consul_services[item.name] %}
{% set _ = consul_services[item.name]['tags'].append("WEIGHT:" ~ weight_value ) %}
{% else %}
{% set _ = consul_services[item.name].update({'tags': ['WEIGHT:' ~ weight_value]}) %}
{% endif %}
{% endif %}

{% if 'tags' in consul_services[item.name] %}
{% set _ = consul_services[item.name]['tags'].extend(item.add_tags) %}
{% else %}
{% set _ = consul_services[item.name].update({'tags': item.add_tags}) %}
{% endif %}


{ {{ '' if consul_services[item.name].pop('haproxy', '') else '' }}
"service": {{ consul_services[item.name] | to_nice_json }}
}
{% else %}
{% if 'weight' in consul_services[item] %}
{% set weight_value = consul_services[item].pop('weight') %}
{% if 'tags' in consul_services[item] %}
Expand All @@ -10,3 +31,4 @@
{ {{ '' if consul_services[item].pop('haproxy', '') else '' }}
"service": {{ consul_services[item] | to_nice_json }}
}
{% endif %}

0 comments on commit a45c794

Please sign in to comment.