Adding nodes to MongoDB Replica Set
-
Danilo Pala
- 22 Dec, 2025
- 02 Mins read
Adding nodes to MongoDB Replica Set by using OPS Manager API
In this article I'm going to talk about adding nodes to MongoDB Replica Sets managed by OPS Manager.
Adding nodes to a Replica Set (RS) is a common task for a MongoDB DBA, if the RS is managed manually, we have to deploy the instance and use the command rs.add(), but if the RS is managed by OPS Manager we can use the UI or the API.
In order to automate these tasks by using OPS Manager API, I've developed a python script (version 3.9) that allow us to add a node to a Replica Set via command line.
The script accept the following arguments:
--project_id: project id
--cluster_name: replica set name
--add_node: node with format <hostname.fqdn:port>
--copy_from: copy configuration from node with format <hostname.fqdn:port>
--override_process_config: override process configuration in json format
--override_member_config: override member configuration in json format
There are more arguments, but I'm going to omit in this article.
An API key is required for invoking OPS Manager API, and it must have permission to edit deployments of the project.
The override_process_config argument is a json format string, it's optional and it's value has to respect the structure defined by MongoDB, if you are not familiar with that, don't pass this argument.
For the override_member_config argument, I've limited the changes to the fields: ["arbiterOnly" ,"buildIndexes","hidden", "priority", "secondaryDelaySecs", "tags", "votes"], it's also optional.
Behavior
The beginning is basically the same as rs_reconfig.py. The differences begin in the "processes" field, where the script identifies the node passed as copy_from, and copy it's value. This value will be replaced with the dictionary passed at override_process_config argument. That's will be the process configuration of the new node.
In the "replicaSets" field, the script first identifies the RS by searching for the cluster_name, once it gets the correct one, it will identify and copy the member that matches with the copy_from node. This value will be replaced with the dictionary passed at override_member_config argument. That's will be the member configuration of the new node.
Then the script requests a put to the OPS Manager API passing the modified automationConfig, finally it will wait until all processes achieve the goal version to return the final result.
Example
I have Replica Set with the following configuration:
Then I launched the script rs_add.py (I masked sensible data):
python3.9 rs_add.py --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME --add_node="**.local:27031" --copy_from="**.local:27033" --override_member_config='{"arbiterOnly": false,"buildIndexes": true,"hidden": true,"priority": 0.0,"secondaryDelaySecs": 0,"tags": {},"votes": 0}' --override_process_config='{"args2_6":{"processManagement": {"pidFilePath": "/data/PROV9_B/db/mongod.pid"},"storage": {"dbPath": "/data/PROV9_B/db"},"systemLog": {"path": "/data/PROV9_B/log/mongod.log"}, "setParameter": {"initialSyncMethod": "fileCopyBased", "initialSyncSourceReadPreference": "nearest"}}}'
on terminal
As result, at the OPS Manager UI:
Conclusion
As you are already familiar with, also this script was designed to be flexible, in this way it's able to cover many use cases, we just need to pass the arguments as desired and a new node will be added to the Replica Set.
I've developed a lot of python scripts that I'm going to share, the next one will be rs_remove.py.
By Danilo Pala, 22/12/2025