Files
openstack-ansible-haproxy_s…/tasks/haproxy_service_config.yml
Dmitriy Rabotyagov e937d08f2c Apply haproxy-service-config tag on include
Rather then applying tag for each task inside the haproxy_service_config
file, it's better to apply it to include. Also, this closes the bug,
when role fails due to fact being undefined,
since setting fact was not covered by the tag.

Change-Id: I533070196dda5387a910f613cdd037fa36880cdb
2023-09-28 09:32:40 +00:00

123 lines
4.9 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.
# NOTE(damiandabrowski): Deprecated haproxy_service_configs format
# conversion will be removed in 2024.1.
- name: Define blank _haproxy_service_configs_simplified variable
set_fact:
_haproxy_service_configs_simplified: []
- name: Append services to _haproxy_service_configs_simplified list
set_fact:
_haproxy_service_configs_simplified: "{{ _haproxy_service_configs_simplified + [(item.service is defined) | ternary(item.service, item)] }}"
loop: "{{ haproxy_service_configs }}"
###########################################################################
# 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/{{ service.haproxy_service_name }}"
owner: root
group: haproxy
mode: "0640"
# NOTE(damiandabrowski): _haproxy_service_configs_simplified should be replaced
# with haproxy_service_configs in 2024.1.
loop: "{{ _haproxy_service_configs_simplified }}"
loop_control:
loop_var: service
when:
- (service.haproxy_backend_nodes is defined and
service.haproxy_backend_nodes | length > 0) or
(service.haproxy_backup_nodes is defined and
service.haproxy_backup_nodes | length > 0) or
service.haproxy_frontend_only | default('False')
- (service.haproxy_service_enabled | default('True')) | bool
- (service.state is not defined or service.state != 'absent')
notify: Regenerate haproxy configuration
- name: Remove haproxy service config files for absent services
file:
path: "/etc/haproxy/conf.d/{{ service.haproxy_service_name }}"
state: absent
notify: Regenerate haproxy configuration
# NOTE(damiandabrowski): _haproxy_service_configs_simplified should be replaced
# with haproxy_service_configs in 2024.1.
loop: "{{ _haproxy_service_configs_simplified }}"
loop_control:
loop_var: service
when:
- ((service.haproxy_service_enabled | default('True')) | bool) is falsy or
(service.state is defined and service.state == 'absent')
###########################################################################
# 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 }}"
owner: root
group: haproxy
mode: "0750"
# NOTE(damiandabrowski): _haproxy_service_configs_simplified should be replaced
# with haproxy_service_configs in 2024.1.
loop: >-
{{
_haproxy_service_configs_simplified | selectattr('haproxy_map_entries', 'defined') | map(attribute='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.haproxy_service_name }}.map"
template:
src: map.j2
dest: "{{ map_file }}"
owner: root
group: haproxy
mode: "0640"
# NOTE(damiandabrowski): _haproxy_service_configs_simplified should be replaced
# with haproxy_service_configs in 2024.1.
with_subelements:
- "{{ _haproxy_service_configs_simplified | selectattr('haproxy_map_entries', 'defined') }}"
- haproxy_map_entries
when:
- (item.0.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.haproxy_service_name }}.map"
when:
- (item.0.haproxy_service_enabled | default('True')) | bool is falsy or
(item.0.state is defined and item.0.state == 'absent') or
(item.1.state | default('present') == 'absent')
# NOTE(damiandabrowski): _haproxy_service_configs_simplified should be replaced
# with haproxy_service_configs in 2024.1.
with_subelements:
- "{{ _haproxy_service_configs_simplified | selectattr('haproxy_map_entries', 'defined') }}"
- haproxy_map_entries
notify: Regenerate maps
register: map_delete