Skip to content

Commit b9c8c56

Browse files
committed
more documentation and fix for writeconcern number format issue
1 parent 1ac754c commit b9c8c56

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

roles/mongo/README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ Another issue is the serial value, it is safest to set it to 1 in your playbook,
2222

2323
See also https://docs.ansible.com/projects/ansible/latest/playbook_guide/playbooks_strategies.html#setting-the-batch-size-with-serial
2424

25+
# Cluster reconfiguration
26+
27+
Warning: the cluster reconfiguration option in the mongodb_replicationset module is experimental. and you can only add or remove one node at a time.
2528

2629
# Todo
2730
- [x] Check mongo_replication_roles and give a clear fail message when not set
@@ -30,3 +33,4 @@ See also https://docs.ansible.com/projects/ansible/latest/playbook_guide/playboo
3033
- [x] Add the possibility for a standalone mongo server
3134
- [x] Cluster changes can be enabled or disabled
3235
- [ ] Reconfigure cluster always reports changed
36+
- [ ] Initialise cluster always reports changed

roles/mongo/tasks/clusterconfig.yml

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@
5151
state: started
5252

5353
# Initialise cluster block
54-
- name: initialise or reconfigure cluster block
54+
- name: Initialise or reconfigure cluster block
5555
when: mongo_replication_role == 'primary'
5656
block:
5757
- name: Check if replica set is already initialised
@@ -97,6 +97,7 @@
9797
verbosity: 2
9898

9999
# Reconfigure cluster
100+
# todo: this alwasy returns changed even wehn nothing changes
100101
- name: Reconfigure cluster if necessary
101102
community.mongodb.mongodb_replicaset:
102103
login_host: localhost
@@ -132,23 +133,38 @@
132133
login_user: admin
133134
login_password: "{{ mongo_admin_password }}"
134135
eval: "db.adminCommand({ getDefaultRWConcern: 1 })"
135-
#transform: raw
136136
register: current_write_concern
137137
changed_when: false
138138

139139
- name: Debug write concern check
140140
ansible.builtin.debug:
141141
msg: "{{ current_write_concern.transformed_output.defaultWriteConcern }}"
142142
verbosity: 2
143+
when: current_write_concern.transformed_output.defaultWriteConcern is defined
143144

144145
- name: Set default write concern
145-
community.mongodb.mongodb_shell:
146-
login_host: localhost
147-
login_user: admin
148-
login_password: "{{ mongo_admin_password }}"
149-
login_port: "{{ mongo_port }}"
150-
eval: "db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: {{ mongo_cluster_write_concern | default('majority') }}, wtimeout: {{ mongo_cluster_write_timeout | default(5000) }} } })"
151146
when: >
152-
current_write_concern.transformed_output.defaultWriteConcern.w | string != mongo_cluster_write_concern | default('majority') | string
147+
current_write_concern.transformed_output.defaultWriteConcern is defined
148+
and
149+
(current_write_concern.transformed_output.defaultWriteConcern.w | string != mongo_cluster_write_concern | default('majority') | string
153150
or
154-
current_write_concern.transformed_output.defaultWriteConcern.wtimeout | int != mongo_cluster_write_timeout | default(5000) | int
151+
current_write_concern.transformed_output.defaultWriteConcern.wtimeout | int != mongo_cluster_write_timeout | default(5000) | int)
152+
or current_write_concern.transformed_output.defaultWriteConcern is not defined
153+
block:
154+
- name: "set write concern majority"
155+
when: mongo_cluster_write_concern == "majority"
156+
community.mongodb.mongodb_shell:
157+
login_host: localhost
158+
login_user: admin
159+
login_password: "{{ mongo_admin_password }}"
160+
login_port: "{{ mongo_port }}"
161+
eval: "db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: \"{{ mongo_cluster_write_concern | default('majority') }}\", wtimeout: {{ mongo_cluster_write_timeout | default(5000) }} } })"
162+
# could not get this to work with either majority with quotes or number without quotes so for now an ugly fix
163+
- name: "set write concern numeric"
164+
when: mongo_cluster_write_concern != "majority"
165+
community.mongodb.mongodb_shell:
166+
login_host: localhost
167+
login_user: admin
168+
login_password: "{{ mongo_admin_password }}"
169+
login_port: "{{ mongo_port }}"
170+
eval: "db.adminCommand({ setDefaultRWConcern: 1, defaultWriteConcern: { w: {{ mongo_cluster_write_concern | default('majority') }}, wtimeout: {{ mongo_cluster_write_timeout | default(5000) }} } })"

0 commit comments

Comments
 (0)