From 7ee67528485f1ef9ba176d30b81805d6f00772f8 Mon Sep 17 00:00:00 2001 From: rockandska Date: Sat, 6 Nov 2021 00:51:46 +0100 Subject: [PATCH] fix(cluster): add discovered inventory nodes to sysctl config --- tasks/config_server.yml | 58 +++++++++++++++---------- templates/etc/rabbitmq/erlang.config.j2 | 4 +- templates/etc/rabbitmq/sysctl.conf.j2 | 10 ++++- 3 files changed, 47 insertions(+), 25 deletions(-) diff --git a/tasks/config_server.yml b/tasks/config_server.yml index 17d8357..f8e2763 100644 --- a/tasks/config_server.yml +++ b/tasks/config_server.yml @@ -27,32 +27,43 @@ or rabbitmq_slave_of != none or rabbitmq_slave_of | d("") | length > 0 -- name: "[RabbitMQ] Define cluster config for master" +- name: "[RabbitMQ] Define cluster nodes" + delegate_to: localhost set_fact: - __rabbitmq_cluster_nodes: >- - 'rabbit@{{ ansible_hostname }}', - 'rabbit@{{ groups['all'] - | map('extract',hostvars) - | selectattr('rabbitmq_slave_of','defined') - | selectattr('rabbitmq_slave_of', 'equalto', inventory_hostname) - | map(attribute='ansible_hostname') | join("','rabbit@") - }}' + __rabbitmq_cluster_nodes: | + {{ + ( __rabbitmq_cluster_nodes + | default([],True) + ) + + [ + node if '@' in node else ([ node, hostvars[item]['ansible_hostname'] ] | join('@')) + ] + }} + loop: + "{{ + ( + ( + groups['all'] + | map('extract',hostvars) + | selectattr('rabbitmq_slave_of','defined') + | selectattr('rabbitmq_slave_of','equalto',(rabbitmq_slave_of | d(inventory_hostname, true))) + | map(attribute='inventory_hostname') + | list + ) + + + [ rabbitmq_slave_of | ternary(rabbitmq_slave_of,inventory_hostname) ] + ) | unique + }}" + loop_control: + index_var: id + vars: + node: "{{ hostvars[item]['rabbitmq_node_name'] | d('rabbit',True) }}" when: - rabbitmq_is_master - -- name: "[RabbitMQ] Define cluster config for slave" - set_fact: - __rabbitmq_cluster_nodes: >- - 'rabbit@{{ hostvars[rabbitmq_slave_of].ansible_hostname }}', - 'rabbit@{{ groups['all'] - | map('extract',hostvars) - | selectattr('rabbitmq_slave_of','defined') - | selectattr('rabbitmq_slave_of', 'equalto', rabbitmq_slave_of) - | map(attribute='ansible_hostname') | join("','rabbit@") - }}' - when: - - rabbitmq_slave_of != none - - rabbitmq_slave_of | d("") | length > 0 + or ( + rabbitmq_slave_of != none + and rabbitmq_slave_of | d("") | length > 0 + ) - name: "[RabbitMQ] Copy custom server config (sysctl format)" template: @@ -76,6 +87,7 @@ when: - rabbitmq_sysctl_config | length == 0 - rabbitmq_series is version('3.7', '>=') + - __rabbitmq_cluster_nodes | d("", True) | length == 0 - name: "[RabbitMQ] Copy custom server config (erlang format)" template: diff --git a/templates/etc/rabbitmq/erlang.config.j2 b/templates/etc/rabbitmq/erlang.config.j2 index e375b8a..5163267 100644 --- a/templates/etc/rabbitmq/erlang.config.j2 +++ b/templates/etc/rabbitmq/erlang.config.j2 @@ -5,12 +5,14 @@ {{ line }} {% endfor %} {% endif %} +{% if rabbitmq_series is version('3.7', '<') %} {% if __rabbitmq_cluster_nodes | d("") | length > 0 and rabbitmq_peer_discovery_classic %} {% if rabbitmq_erlang_config != None and rabbitmq_erlang_config | d("") | length > 0 %} , {% endif %} {rabbit, [ - {cluster_nodes, {[{{ __rabbitmq_cluster_nodes }}], {{ rabbitmq_cluster_node_type }} }} + {cluster_nodes, { {{ __rabbitmq_cluster_nodes }}, {{ rabbitmq_cluster_node_type }} } } ]} {% endif %} +{% endif %} ]. diff --git a/templates/etc/rabbitmq/sysctl.conf.j2 b/templates/etc/rabbitmq/sysctl.conf.j2 index a8c743c..a294b60 100644 --- a/templates/etc/rabbitmq/sysctl.conf.j2 +++ b/templates/etc/rabbitmq/sysctl.conf.j2 @@ -1,4 +1,12 @@ -#jinja2: trim_blocks: "true", lstrip_blocks: "false" +#jinja2: trim_blocks: True, lstrip_blocks: True +{%- if rabbitmq_series is version('3.7', '>=') -%} +{%- if __rabbitmq_cluster_nodes | d("") | length > 0 and rabbitmq_peer_discovery_classic -%} +cluster_formation.peer_discovery_backend = classic_config +{% for node in __rabbitmq_cluster_nodes %} +cluster_formation.classic_config.nodes.{{ loop.index }} = {{ node }} +{% endfor %} +{%- endif -%} +{%- endif -%} {%- macro treedot(dict,string) -%} {%- for key, value in dict.items() -%} {%- if value is mapping -%}