|
1 | 1 | --- |
2 | | -# todo this weorks only for new deployments |
3 | | -# rewrite so mongo config can be changed and cluster members can be added or removed |
4 | | -- name: Check if hosts are in clustered |
5 | | - ansible.builtin.command: mongosh --port {{ mongod_port }} --quiet --eval 'db.isMaster().hosts' |
6 | | - register: check_cluster |
7 | | - changed_when: false |
8 | | - check_mode: false |
9 | | - |
10 | | -- name: Debug check_cluster variable |
| 2 | +# In this task file the cluster is configured |
| 3 | + |
| 4 | +# priority moet matchen met replication role, of replication role uit cluster mebers halen? |
| 5 | +# todo write concern zetten |
| 6 | + |
| 7 | +# Do some preflight checks |
| 8 | +- name: Check some cluster related variables |
| 9 | + when: mongo_mode == "cluster" |
| 10 | + block: |
| 11 | + - name: Fail on undefined mongo_replica_set_name |
| 12 | + when: mongo_replica_set_name is not defined |
| 13 | + ansible.builtin.fail: |
| 14 | + msg: "Something is wrong, mongo_mode was set to cluster but mongo_replica_set_name is undefined." |
| 15 | + |
| 16 | +- name: Debug replica settings |
11 | 17 | ansible.builtin.debug: |
12 | | - msg: "{{ check_cluster }}" |
| 18 | + msg: "Replica set name {{ mongo_replica_set_name }}" |
13 | 19 | verbosity: 2 |
14 | 20 |
|
15 | | -- name: Debug mongo_cluster_members variable |
| 21 | +# Loop over cluster members and check their presence in mong_servers group and their mode (not standalone) |
| 22 | + |
| 23 | +- name: Check if mongo_cluster_members exist in inventory group |
| 24 | + ansible.builtin.assert: |
| 25 | + that: |
| 26 | + - item.host in groups['mongo_servers'] |
| 27 | + fail_msg: "Server '{{ item.host }}' is not in the mongo_servers inventory group" |
| 28 | + success_msg: "Server '{{ item.host }}' found in mongo_servers inventory group" |
| 29 | + run_once: true |
| 30 | + loop: "{{ mongo_cluster_members }}" |
| 31 | + |
| 32 | +# Loop over cluster members and check for primary |
| 33 | + |
| 34 | +- name: Set primary host fact |
| 35 | + ansible.builtin.set_fact: |
| 36 | + mongo_primary_host: "{{ (mongo_cluster_members | max(attribute='priority')).host }}" |
| 37 | + |
| 38 | +- name: Debug primary settings |
16 | 39 | ansible.builtin.debug: |
17 | | - msg: "{{ mongo_cluster_members }}" |
| 40 | + msg: "Primary is {{ mongo_primary_host }}" |
18 | 41 | verbosity: 2 |
19 | 42 |
|
20 | | -- name: Debug mongo_replication_role variable |
| 43 | +# What is the replication role of the current host |
| 44 | +- name: Debug replication role settings |
21 | 45 | ansible.builtin.debug: |
22 | | - msg: "{{ mongo_replication_role }}" |
| 46 | + msg: "This nodes replication role is {{ mongo_replication_role }}" |
23 | 47 | verbosity: 2 |
24 | 48 |
|
25 | | -- name: Initial cluster initialisation |
26 | | - community.mongodb.mongodb_replicaset: |
27 | | - login_host: localhost |
28 | | - login_user: admin |
29 | | - login_port: "{{ mongod_port }}" |
30 | | - login_password: "{{ mongo_admin_password }}" |
31 | | - replica_set: "{{ replica_set_name }}" |
32 | | - members: "{{ mongo_cluster_members }}" |
33 | | - arbiter_at_index: "{{ mongo_arbiter_index | default(0) }}" |
34 | | - validate: false |
35 | | - run_once: true |
36 | | - when: mongo_replication_role == 'primary' |
| 49 | +# Cannot initialise a cluster without starting....... |
| 50 | +- name: Enable and start mongod |
| 51 | + ansible.builtin.service: |
| 52 | + name: mongod.service |
| 53 | + enabled: true |
| 54 | + state: started |
37 | 55 |
|
38 | | -- name: Wait until cluster health is ok |
39 | | - community.mongodb.mongodb_status: |
40 | | - login_user: admin |
41 | | - login_password: "{{ mongo_admin_password }}" |
42 | | - login_database: admin |
43 | | - login_port: "{{ mongod_port }}" |
44 | | - validate: default |
45 | | - poll: 5 |
46 | | - interval: 12 |
47 | | - replica_set: "{{ replica_set_name }}" |
| 56 | +# Initialise cluster block |
| 57 | +- name: Initialise or reconfigure cluster block |
48 | 58 | when: mongo_replication_role == 'primary' |
| 59 | + block: |
| 60 | + - name: Check if replica set is already initialised |
| 61 | + community.mongodb.mongodb_shell: |
| 62 | + login_host: localhost |
| 63 | + login_user: admin |
| 64 | + login_port: "{{ mongo_port }}" |
| 65 | + login_password: "{{ mongo_admin_password }}" |
| 66 | + eval: "rs.status().ok" |
| 67 | + db: admin |
| 68 | + register: rs_already_init |
| 69 | + ignore_errors: true |
49 | 70 |
|
50 | | -- name: Add the admin user |
51 | | - community.mongodb.mongodb_user: |
52 | | - database: admin |
53 | | - name: admin |
54 | | - password: "{{ mongo_admin_password }}" |
55 | | - login_port: "{{ mongod_port }}" |
56 | | - roles: root |
57 | | - state: present |
58 | | - when: check_cluster.stdout == "" |
59 | | - no_log: true |
60 | | - run_once: true |
| 71 | + - name: Debug cluster initialization check |
| 72 | + ansible.builtin.debug: |
| 73 | + msg: "{{ rs_already_init }}" |
| 74 | + verbosity: 2 |
| 75 | + |
| 76 | + # This should be possible with community.mongodb.mongodb_replicaset |
| 77 | + # But we keep getting authenticatione error so leave it like this for now |
| 78 | + - name: Initialise replica set if necessary |
| 79 | + community.mongodb.mongodb_shell: |
| 80 | + login_host: localhost |
| 81 | + login_user: admin |
| 82 | + login_port: "{{ mongo_port }}" |
| 83 | + login_password: "{{ mongo_admin_password }}" |
| 84 | + eval: | |
| 85 | + rs.initiate({ |
| 86 | + _id: "{{ mongo_replica_set_name }}", |
| 87 | + members: [ |
| 88 | + {% for m in mongo_cluster_members %} |
| 89 | + { _id: {{ loop.index0 }}, host: "{{ m.host }}:{{ m.port }}", priority: {{ m.priority }}, votes: {{ m.votes }}{% if m.arbiterOnly is defined and m.arbiterOnly and m.arbiterOnly == true %}, arbiterOnly: true {% endif %} }{{ "," if not loop.last else "" }} |
| 90 | + {% endfor %} |
| 91 | + ] |
| 92 | + }) |
| 93 | + db: admin |
| 94 | + when: rs_already_init.failed |
| 95 | + register: rs_init |
| 96 | + |
| 97 | + - name: Debug cluster initialization |
| 98 | + ansible.builtin.debug: |
| 99 | + msg: "{{ rs_init }}" |
| 100 | + verbosity: 2 |
| 101 | + |
| 102 | + - name: Format members list |
| 103 | + ansible.builtin.set_fact: |
| 104 | + mongo_cluster_members_formatted: "{{ mongo_cluster_members_formatted | default([]) + [m | combine({'host': m.host ~ ':' ~ (m.port | string)}) | dict2items | rejectattr('key', 'eq', 'port') | list | items2dict] }}" |
| 105 | + loop: "{{ mongo_cluster_members }}" |
| 106 | + loop_control: |
| 107 | + loop_var: m |
| 108 | + |
| 109 | + - name: Debug members list |
| 110 | + ansible.builtin.debug: |
| 111 | + msg: "{{ mongo_cluster_members }}" |
| 112 | + verbosity: 2 |
| 113 | + |
| 114 | + - name: Debug formatted members list |
| 115 | + ansible.builtin.debug: |
| 116 | + msg: "{{ mongo_cluster_members_formatted }}" |
| 117 | + verbosity: 2 |
| 118 | + |
| 119 | + # Reconfigure cluster |
| 120 | + # todo: this always returns changed even when nothing changes |
| 121 | + - name: Reconfigure cluster if necessary |
| 122 | + community.mongodb.mongodb_replicaset: |
| 123 | + login_host: localhost |
| 124 | + login_user: admin |
| 125 | + login_password: "{{ mongo_admin_password }}" |
| 126 | + login_port: "{{ mongo_port }}" |
| 127 | + reconfigure: true |
| 128 | + replica_set: "{{ mongo_replica_set_name }}" |
| 129 | + members: "{{ mongo_cluster_members_formatted }}" |
| 130 | + register: rs_reconfigure |
| 131 | + |
| 132 | + - name: Debug cluster reconfiguration |
| 133 | + ansible.builtin.debug: |
| 134 | + msg: "{{ rs_reconfigure }}" |
| 135 | + verbosity: 2 |
| 136 | + |
| 137 | + - name: Wait for the replicaset to stabilise |
| 138 | + community.mongodb.mongodb_status: |
| 139 | + replica_set: "{{ mongo_replica_set_name }}" |
| 140 | + login_host: localhost |
| 141 | + login_user: admin |
| 142 | + login_password: "{{ mongo_admin_password }}" |
| 143 | + login_port: "{{ mongo_port }}" |
| 144 | + poll: 5 |
| 145 | + interval: 30 |
| 146 | + validate: minimal # default fails on even number of servers and although this is not a great situation, it is sometimes the temporary situation because we can onlye add or remove 1 node at a time |
| 147 | + |
| 148 | + # Cluster settings that cannot be changed with mongodb_replicaset |
| 149 | + |
| 150 | + - name: Get current default write concern |
| 151 | + community.mongodb.mongodb_shell: |
| 152 | + login_host: localhost |
| 153 | + login_port: 27017 |
| 154 | + login_user: admin |
| 155 | + login_password: "{{ mongo_admin_password }}" |
| 156 | + eval: "db.adminCommand({ getDefaultRWConcern: 1 })" |
| 157 | + register: current_write_concern |
| 158 | + changed_when: false |
| 159 | + |
| 160 | + - name: Debug write concern check |
| 161 | + ansible.builtin.debug: |
| 162 | + msg: "{{ current_write_concern.transformed_output.defaultWriteConcern }}" |
| 163 | + verbosity: 2 |
| 164 | + when: current_write_concern.transformed_output.defaultWriteConcern is defined |
| 165 | + |
| 166 | + - name: Set default write concern |
| 167 | + when: > |
| 168 | + current_write_concern.transformed_output.defaultWriteConcern is defined |
| 169 | + and |
| 170 | + (current_write_concern.transformed_output.defaultWriteConcern.w | string != mongo_cluster_write_concern | default('majority') | string |
| 171 | + or |
| 172 | + current_write_concern.transformed_output.defaultWriteConcern.wtimeout | int != mongo_cluster_write_timeout | default(5000) | int) |
| 173 | + or current_write_concern.transformed_output.defaultWriteConcern is not defined |
| 174 | + block: |
| 175 | + - name: "set write concern majority" |
| 176 | + when: mongo_cluster_write_concern == "majority" |
| 177 | + community.mongodb.mongodb_shell: |
| 178 | + login_host: localhost |
| 179 | + login_user: admin |
| 180 | + login_password: "{{ mongo_admin_password }}" |
| 181 | + login_port: "{{ mongo_port }}" |
| 182 | + eval: "db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: \"{{ mongo_cluster_write_concern | default('majority') }}\", wtimeout: {{ mongo_cluster_write_timeout | default(5000) }} } })" |
| 183 | + # could not get this to work with either majority with quotes or number without quotes so for now an ugly fix |
| 184 | + - name: "set write concern numeric" |
| 185 | + when: mongo_cluster_write_concern != "majority" |
| 186 | + community.mongodb.mongodb_shell: |
| 187 | + login_host: localhost |
| 188 | + login_user: admin |
| 189 | + login_password: "{{ mongo_admin_password }}" |
| 190 | + login_port: "{{ mongo_port }}" |
| 191 | + eval: "db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: {{ mongo_cluster_write_concern | default('majority') }}, wtimeout: {{ mongo_cluster_write_timeout | default(5000) }} } })" |
0 commit comments