diff --git a/tasks/consul-services.yml b/tasks/consul-services.yml index 9a8bc60..12e42f9 100644 --- a/tasks/consul-services.yml +++ b/tasks/consul-services.yml @@ -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: @@ -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 diff --git a/templates/consul-service.j2 b/templates/consul-service.j2 index aea0885..8b7a824 100644 --- a/templates/consul-service.j2 +++ b/templates/consul-service.j2 @@ -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] %} @@ -10,3 +31,4 @@ { {{ '' if consul_services[item].pop('haproxy', '') else '' }} "service": {{ consul_services[item] | to_nice_json }} } +{% endif %}