--- # 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 ansible.builtin.set_fact: _haproxy_service_configs_simplified: [] - name: Append services to _haproxy_service_configs_simplified list ansible.builtin.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 ansible.builtin.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 ansible.builtin.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: Create haproxy map fragment directories ansible.builtin.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" ansible.builtin.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 ansible.builtin.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