Files
openstack-ansible-haproxy_s…/tasks/haproxy_service_config.yml
Jonathan Rosser d548b7e5ff Add support for haproxy map files
HAProxy supports the use of map files for selecting backends, or
a number of other functions. See [1] and [2].

This patch adds the key `maps` for each service definition allowing
fragments of a complete map to be defined across all the services,
with each service contributing some elements to the overall map file.

The service enabled/disabled and state flags are observed to add and
remove entries from the map file, and individual map entries can also
be marked as present/absent to make inclusion conditional.

[1] https://www.haproxy.com/blog/introduction-to-haproxy-maps/
[2] https://www.haproxy.com/documentation/hapee/latest/configuration/map-files/syntax/

Change-Id: I755c18a4d33ee69c42d68a50daa63614a2b2feb7
2023-03-16 13:17:39 +01:00

90 lines
3.8 KiB
YAML

---
# Copyright 2014, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
###########################################################################
# Service frontends and backends assembled from fragments into haproxy.conf
###########################################################################
- name: Create haproxy service config files
template:
src: service.j2
dest: "/etc/haproxy/conf.d/{{ item.service.haproxy_service_name }}"
with_items: "{{ haproxy_service_configs }}"
when:
- (item.service.haproxy_backend_nodes is defined and
item.service.haproxy_backend_nodes | length > 0) or
(item.service.haproxy_backup_nodes is defined and
item.service.haproxy_backup_nodes | length > 0) or
item.service.haproxy_frontend_only | default('False')
- (item.service.haproxy_service_enabled | default('True')) | bool
- (item.service.state is not defined or item.service.state != 'absent')
notify: Regenerate haproxy configuration
tags:
- haproxy-service-config
- name: Remove haproxy service config files for absent services
file:
path: "/etc/haproxy/conf.d/{{ item.service.haproxy_service_name }}"
state: absent
notify: Regenerate haproxy configuration
with_items: "{{ haproxy_service_configs }}"
when:
- ((item.service.haproxy_service_enabled | default('True')) | bool) is falsy or
(item.service.state is defined and item.service.state == 'absent')
tags:
- haproxy-service-config
###########################################################################
# Map files assembled from fragments from each service into <map-name>.map
###########################################################################
- name: Create haproxy map fragment directories
file:
state: directory
path: "/etc/haproxy/map.conf.d/{{ item }}"
with_items: "{{ haproxy_service_configs | selectattr('service.haproxy_map_entries', 'defined') | map(attribute='service.haproxy_map_entries') | flatten | map(attribute='name') | unique }}"
# create map entries when the service is enabled and an existing map fragment is not absent
- name: Create haproxy map files
vars:
map_file: "/etc/haproxy/map.conf.d/{{ item.1.name }}/{{ item.1.order | default('00') }}-{{ item.0.service.haproxy_service_name }}.map"
template:
src: map.j2
dest: "{{ map_file }}"
with_subelements:
- "{{ haproxy_service_configs | selectattr('service.haproxy_map_entries', 'defined') }}"
- service.haproxy_map_entries
when:
- (item.0.service.haproxy_service_enabled | default(True)) | bool
- item.1.state | default('present') != 'absent'
notify: regenerate maps
register: map_create
# remove map entries when the service is not enabled, the service is absent or the map is absent
- name: Delete unused map entries
file:
state: absent
path: "/etc/haproxy/map.conf.d/{{ item.1.name }}/{{ item.1.order | default('00') }}-{{ item.0.service.haproxy_service_name }}.map"
when:
- (item.0.service.haproxy_service_enabled | default('True')) | bool is falsy or
(item.0.service.state is defined and item.0.service.state == 'absent') or
(item.1.state | default('present') == 'absent')
with_subelements:
- "{{ haproxy_service_configs | selectattr('service.haproxy_map_entries', 'defined') }}"
- service.haproxy_map_entries
notify: regenerate maps
register: map_delete