Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Change RabbitMQ cluster node names to short hostname #43

8 changes: 4 additions & 4 deletions roles/rabbitmq/tasks/configure-clustering.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
- name: Write all RabbitMQ nodes to the hosts file
ansible.builtin.lineinfile:
path: "/etc/hosts"
line: "{{ hostvars[item].ansible_default_ipv4.address }} rabbit{{ my_idx + 1 }} {{ hostvars[item].ansible_hostname }}"
line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ item }} {{ hostvars[item].inventory_hostname_short }} rabbit{{ my_idx + 1 }}"
loop: "{{ lookup('inventory_hostnames', 'rabbitmq', wantlist=True) }}"
loop_control:
index_var: my_idx
Expand All @@ -13,7 +13,7 @@
- name: Write all RabbitMQ nodes to the hosts file for secondary DR
ansible.builtin.lineinfile:
path: "/etc/hosts"
line: "{{ hostvars[item].ansible_default_ipv4.address }} rabbit{{ my_idx + 1 }} {{ hostvars[item].ansible_hostname }}"
line: "{{ hostvars[item].ansible_default_ipv4.address }} {{ item }} {{ hostvars[item].inventory_hostname_short }} rabbit{{ my_idx + 1 }}"
loop: "{{ lookup('inventory_hostnames', 'rabbitmq_secondary', wantlist=True) }}"
loop_control:
index_var: my_idx
Expand All @@ -25,7 +25,7 @@
ansible.builtin.lineinfile:
path: "{{ rabbitmq_config }}"
insertafter: "cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config"
line: "cluster_formation.classic_config.nodes.{{ my_idx + 1 }} = rabbit@{{ hostvars[item].ansible_hostname }}"
line: "cluster_formation.classic_config.nodes.{{ my_idx + 1 }} = rabbit@{{ hostvars[item].inventory_hostname_short }}"
loop: "{{ lookup('inventory_hostnames', 'rabbitmq', wantlist=True) }}"
loop_control:
index_var: my_idx
Expand All @@ -35,7 +35,7 @@
ansible.builtin.lineinfile:
path: "{{ rabbitmq_config }}"
insertafter: "cluster_formation.peer_discovery_backend = rabbit_peer_discovery_classic_config"
line: "cluster_formation.classic_config.nodes.{{ my_idx + 1 }} = rabbit@{{ hostvars[item].ansible_hostname }}"
line: "cluster_formation.classic_config.nodes.{{ my_idx + 1 }} = rabbit@{{ hostvars[item].inventory_hostname_short }}"
loop: "{{ lookup('inventory_hostnames', 'rabbitmq_secondary', wantlist=True) }}"
loop_control:
index_var: my_idx
Expand Down
24 changes: 12 additions & 12 deletions roles/rabbitmq/tasks/enable-clustering.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
changed_when: output.rc != 0
when:
- inventory_hostname in groups['rabbitmq']
- hostvars[groups['rabbitmq'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq.index(inventory_hostname) != 0

- name: Stop RabbitMQ for secondary
ansible.builtin.command:
Expand All @@ -21,7 +21,7 @@
when:
- groups['rabbitmq_secondary'] is defined
- inventory_hostname in groups['rabbitmq_secondary']
- hostvars[groups['rabbitmq_secondary'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq_secondary.index(inventory_hostname) != 0

- name: Reset node
ansible.builtin.command:
Expand All @@ -30,7 +30,7 @@
changed_when: output.rc != 0
when:
- inventory_hostname in groups['rabbitmq']
- hostvars[groups['rabbitmq'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq.index(inventory_hostname) != 0

- name: Reset node for secondary
ansible.builtin.command:
Expand All @@ -40,26 +40,26 @@
when:
- groups['rabbitmq_secondary'] is defined
- inventory_hostname in groups['rabbitmq_secondary']
- hostvars[groups['rabbitmq_secondary'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq_secondary.index(inventory_hostname) != 0

- name: Join cluster
ansible.builtin.command:
cmd: "rabbitmqctl join_cluster rabbit@{{ hostvars[groups['rabbitmq'][0]].ansible_hostname }}"
cmd: "rabbitmqctl join_cluster rabbit@{{ hostvars[groups['rabbitmq'][0]].inventory_hostname_short }}"
register: output
changed_when: output.rc != 0
when:
- inventory_hostname in groups['rabbitmq']
- hostvars[groups['rabbitmq'][0]].ansible_hostname != ansible_hostname
- inventory_hostname in groups.rabbitmq
- groups.rabbitmq.index(inventory_hostname) != 0

- name: Join cluster for secondary DR
ansible.builtin.command:
cmd: "rabbitmqctl join_cluster rabbit@{{ hostvars[groups['rabbitmq_secondary'][0]].ansible_hostname }}"
cmd: "rabbitmqctl join_cluster rabbit@{{ hostvars[groups['rabbitmq_secondary'][0]].inventory_hostname_short }}"
register: output
changed_when: output.rc != 0
when:
- groups['rabbitmq_secondary'] is defined
- inventory_hostname in groups['rabbitmq_secondary']
- hostvars[groups['rabbitmq_secondary'][0]].ansible_hostname != ansible_hostname
- inventory_hostname in groups.rabbitmq_secondary
- groups.rabbitmq_secondary.index(inventory_hostname) != 0

- name: Restart RabbitMQ server
ansible.builtin.systemd:
Expand All @@ -68,7 +68,7 @@
state: restarted
when:
- inventory_hostname in groups['rabbitmq']
- hostvars[groups['rabbitmq'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq.index(inventory_hostname) != 0

- name: Restart RabbitMQ server for secondary
ansible.builtin.systemd:
Expand All @@ -78,7 +78,7 @@
when:
- groups['rabbitmq_secondary'] is defined
- inventory_hostname in groups['rabbitmq_secondary']
- hostvars[groups['rabbitmq_secondary'][0]].ansible_hostname != ansible_hostname
- groups.rabbitmq_secondary.index(inventory_hostname) != 0

# From the cmd line run this to set the proper HA policy on the iap vhost
# rabbitmqctl set_policy ha-all -p iap ".*" '{"ha-mode":"all", "ha-promote-on-shutdown": "when-synced", "ha-sync-mode": "automatic"}'
Expand Down
Loading