MongoDB Replica Set Reconfiguration
-
Danilo Pala
- 19 Dec, 2025
- 03 Mins read
MongoDB Replica Set Reconfig by using OPS Manager API: rs_reconfig.py
Today I'm going to talk about a pretty common task: Replica Set reconfiguration, being more specific, about reconfig a Replica Set that's managed by OPS Manager.
There are a lot a cases when it's required to reconfig a Replica Set, for example: in a migration, for business requirement, isolating a node/zone/region, and so on.
If a Replica Set is managed manually, we use the command: rs.reconfig(), but if the Replica Set is managed by OPS Manager we can use OPS Manager UI or its API.
In order to automate the reconfiguration by using OPS Manager API, I've developed a python script (version 3.9) that allow us to reconfig a Replica Set via command line.
The script accept the following arguments:
--project_id: project id
--cluster_name: replica set name
--primary_nodes: list of primary hosts with format <hostname.fqdn:port> separated by comma(,)
--primary_config: member(s) new configuration file in json format
--secondary_nodes: list of secondary hosts with format <hostname.fqdn:port> separated by comma(,)
--secondary_config: member(s) new configuration file in json format
--third_nodes: list of third hosts with format <hostname.fqdn:port> separated by comma(,)
--third_config: member(s) new configuration file in json format
--fourth_nodes: list of fourth hosts with format <hostname.fqdn:port> separated by comma(,)
--fourth_config: member(s) new configuration file in json format
There are more arguments, but I'm going to omit in this article, because I want to focus on reconfig.
An API key is required for invoking OPS Manager API, and it must have permission to edit deployments of the project.
For each _config argument I've limited the changes to the fields: ["arbiterOnly","buildIndexes","hidden","priority","secondaryDelaySecs","tags","votes"]
It's possible to apply 4 different member configuration at once, by passing: primary_config, secondary_config, third_config, fourth_config. These parameters expected a json format string. And for each config passed is expected the respective list of nodes at the arguments: primary_nodes, secondary_nodes, third_nodes ,fourth_nodes.
Behavior
First, the script validates the inputted data, then invokes the OPS Manager API getting the automationConfig of the project_id, the response is a json that's loaded into a dictionary. Here is available the doc.
In this script I'm going to use basically 2 fields of the automationConfig: "processes" and "replicaSets". The "processes" field is an array, where each element represents two types of processes: mongod or mongos. I'm interessed in Replica Sets, so the script will look for mongod processes only. Also the "replicaSets" field is an array, and each element represents a replica set. A replica set has another array field: "members", those are the documents that will updated if they match the nodes passed as argument.
At the "processes" field, the script identifies each node passed in the arguments primary_nodes, secondary_nodes, third_nodes ,fourth_nodes, when the node matches a process, the "name" of the process is discovered, and this information will be used in the next step.
In the "replicaSets" field, the script first identifies replica set by searching for the cluster_name, once it gets the correct replica set, it will loop at "members" field, if the "host" field of an element of this array matches with the "name" of a process discovered in the previous step, the configuration of this member will be updated.
The modified automationConfig will be put to the OPS Manager API, finally it will wait until all processes achieve the goal version to return a final result.
Example
I have Replica Set with the following configuration:
Then I launched the script rs_reconfig.py (I masked sensible data):
python3.9 rs_reconfig.py --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME \
--primary_nodes="**.local:27032,**.local:27033" --primary_config='{"priority": 2.0, "votes":1, "hidden": false}' \
--secondary_nodes="**.local:27030" --secondary_config='{"priority": 1.0, "votes":1, "hidden": false}' \
--third_nodes="**.local:27031" --third_config='{"priority": 0.0, "votes":1, "hidden": false}'
Image:
As result, at the OPS Manager UI:
Conclusion
This script was designed to be flexible, in this way it's able to cover a lot of reconfig cases, we just need to pass the arguments as desired and the Replica Set will be reconfigurated.
I've developed a lot of python scripts that I'm going to share in the future articles, the next is going to be rs_add.py.
By Danilo Pala, 17/12/2025