diff --git a/library/haproxy_commands.py b/library/haproxy_commands.py deleted file mode 100644 index 83e932c8e..000000000 --- a/library/haproxy_commands.py +++ /dev/null @@ -1,76 +0,0 @@ -#!/bin/env python -from ansible.module_utils.basic import * - - -def _haproxy_weight(host, weight, app_name): - return { - "host": host, "weight": str(weight) + "%", "backend": app_name + "_be" - } - - -def _haproxy_state(host, state, app_name): - return { - "host": host, "state": state, "backend": app_name + "_be", - "wait": "yes" - } - - -if __name__ == "__main__": - fields = { - "java_blauwrood_servers": {"java_blauwrood_servers": True, "type": "list"}, - "php_blauwrood_servers": {"php_blauwrood_servers": True, "type": "list"}, - "static_blauwrood_servers": {"static_blauwrood_servers": True, "type": "list"}, - "stepup_blauwrood_servers": {"stepup_blauwrood_servers": True, "type": "list"}, - "docker_blauwrood_servers": {"docker_blauwrood_servers": True, "type": "list"}, - "weight": {"type": "str"}, - "color": {"required": True, "type": "str"}, - "app_name": {"required": True, "type": "str"}, - "app_type": {"required": True, "type": "str"}, - "state": {"type": "str"} - } - module = AnsibleModule(argument_spec=fields) - java_servers = [s["label"] for s in module.params["java_blauwrood_servers"]] - php_servers = [s["label"] for s in module.params["php_blauwrood_servers"]] - static_servers = [s["label"] for s in module.params["static_blauwrood_servers"]] - stepup_servers = [s["label"] for s in module.params["stepup_blauwrood_servers"]] - docker_servers = [s["label"] for s in module.params["docker_blauwrood_servers"]] - app_name = module.params["app_name"].lower() - app_type = module.params["app_type"].lower() - - weight = module.params["weight"] - color = module.params["color"] - - state = module.params["state"] - - if app_type == "java": - servers = java_servers - elif app_type == "php": - servers = php_servers - elif app_type == "static": - servers = static_servers - elif app_type == "stepup": - servers = stepup_servers - elif app_type == "docker": - servers = docker_servers - - red_servers = [s for s in servers if s.upper().endswith("ROOD")] - blue_servers = [s for s in servers if s.upper().endswith("BLAUW")] - if color == "rood": - state_servers = red_servers - elif color == "blauw": - state_servers = blue_servers - - if state: - haproxy_items = [_haproxy_state(s, state, app_name) for s in state_servers] - module.exit_json(haproxy_items=haproxy_items) - else: - weight = int(weight[:-1]) if weight.endswith("%") else int(weight) - other_weight = 100 - weight - color = color.lower() - - red_weight = weight if color == "rood" else other_weight - blue_weight = weight if color == "blauw" else other_weight - - red_items = [_haproxy_weight(s, red_weight, app_name) for s in red_servers] - blue_items = [_haproxy_weight(s, blue_weight, app_name) for s in blue_servers] - module.exit_json(haproxy_items=red_items + blue_items) diff --git a/roles/haproxy_mgnt/tasks/main.yml b/roles/haproxy_mgnt/tasks/main.yml index 5babeb3f3..86eacf6ba 100644 --- a/roles/haproxy_mgnt/tasks/main.yml +++ b/roles/haproxy_mgnt/tasks/main.yml @@ -1,74 +1,59 @@ --- -- name: Check if the patchproces is running, except when called from the patchproces - stat: - path: /tmp/patchprocesstarted - register: patch_process_started +- name: Extract list of application names + ansible.builtin.set_fact: + app_names: "{{ haproxy_applications | map(attribute='name') | list }}" -- block: - - name: End the play if the patchprocess is running - debug: - msg: "The patchprocess is running. No loadbalancer management allowed" - - meta: end_play - when: patch_process_started.stat.exists and skip_patch_check is not defined +- name: Check if dynamic_input exists in haproxy_applications + ansible.builtin.set_fact: + app_exists: "{{ app_name in app_names }}" -- name: get new weight for haproxy hosts - haproxy_commands: - java_blauwrood_servers: "{{ java_blauwrood_servers }}" - php_blauwrood_servers: "{{ php_blauwrood_servers }}" - static_blauwrood_servers: "{{ static_blauwrood_servers }}" - stepup_blauwrood_servers: "{{ stepup_blauwrood_servers }}" - docker_blauwrood_servers: "{{ docker_blauwrood_servers }}" - weight: "{{ weight }}" - color: "{{ color }}" - app_name: "{{ app_name }}" - app_type: "{{ app_type }}" - when: weight is defined - register: haproxy_weights +- name: Fail if dynamic_input does not exist in haproxy_applications + ansible.builtin.fail: + msg: "The application '{{ app_name }}' does not exist in haproxy_applications. Available applications are: {{ app_names | join(', ') }}" + when: not app_exists -- name: get new state for haproxy hosts - haproxy_commands: - java_blauwrood_servers: "{{ java_blauwrood_servers }}" - php_blauwrood_servers: "{{ php_blauwrood_servers }}" - static_blauwrood_servers: "{{ static_blauwrood_servers }}" - stepup_blauwrood_servers: "{{ stepup_blauwrood_servers }}" - docker_blauwrood_servers: "{{ docker_blauwrood_servers }}" - color: "{{ color }}" - state: "{{ state }}" - app_name: "{{ app_name }}" - app_type: "{{ app_type }}" - when: state is defined - register: haproxy_states +- name: Set the complementing weight + ansible.builtin.set_fact: + weight_complementing: "{{ 100 - weight | int }}" -- name: dump weight output - debug: - msg: '{{ haproxy_weights.haproxy_items }}' - when: weight is defined +- name: Get the server list from the servers configured in haproxy_applications + ansible.builtin.set_fact: + selected_servers: "{{ (haproxy_applications | selectattr('name', 'equalto', app_name) | map(attribute='servers') | first) }}" -- name: dump state output - debug: - msg: '{{ haproxy_states.haproxy_items }}' - when: state is defined +- name: Check if the patchproces is running, except when called from the patchproces + ansible.builtin.stat: + path: /tmp/patchprocesstarted + register: patch_process_started -- name: set weights for haproxy hosts - haproxy: +- block: + - name: End the play if the patchprocess is running + ansible.builtin.debug: + msg: "The patchprocess is running. No loadbalancer management allowed" + - ansible.builtin.meta: end_play + when: patch_process_started.stat.exists and skip_patch_check is not defined + +- name: Create an empty list + ansible.builtin.set_fact: + server_labels_with_weights: [] + +- name: Create list with labels and weights as dictionaries + ansible.builtin.set_fact: + server_labels_with_weights: "{{ server_labels_with_weights + [{ 'label': item.label, 'weight': (weight if item[app_filter] == app_filtervalue else weight_complementing) }] }}" + loop: "{{ selected_servers }}" + +- name: Show the created list + ansible.builtin.debug: + msg: "{{ server_labels_with_weights }}" + +- name: Set weights for haproxy hosts + community.general.haproxy: state: enabled - host: "{{ item.host }}" - fail_on_not_found: yes - socket: /var/lib/haproxy/haproxy.stats - weight: "{{ item.weight }}" - backend: "{{ item.backend }}" - with_items: "{{ haproxy_weights.haproxy_items }}" - when: weight is defined - -- name: set state for haproxy hosts - haproxy: - state: "{{ item.state }}" - host: "{{ item.host }}" - fail_on_not_found: yes + host: "{{ item.label }}" + fail_on_not_found: true socket: /var/lib/haproxy/haproxy.stats - backend: "{{ item.backend }}" - with_items: "{{ haproxy_states.haproxy_items }}" - when: state is defined + weight: "{{ item.weight }}%" + backend: "{{ app_name }}_be" + with_items: "{{ server_labels_with_weights }}" - name: Write the state to the correct state file - shell: echo "show servers state" | socat /var/lib/haproxy/haproxy.stats - > /var/lib/haproxy/state + ansible.builtin.shell: echo "show servers state" | socat /var/lib/haproxy/haproxy.stats - > /var/lib/haproxy/state