Automating changes in a MongoDB replica set managed by OPS Manager

Automating changes in a MongoDB replica set managed by OPS Manager

Automating changes in a MongoDB Replica Set managed by OPS Manager

In my latest articles I provide information about 3 python scripts: rs_reconfig.py, rs_add.py and rs_remove.py, in this article I'm going to explain the rs_migrate.py script, and migrate MongoDB Replica Set as use case example.

There are several use cases where it's necessary to modify a Replica Set (RS) in a well-defined order.

Let's take the migration example, from database perspective, a RS can be migrated with zero down time. In simple words the procedure is: adding new nodes, switch the priorities (and votes) to the new nodes, and finally remove each old node. From application perspective, it's important to remember that is required updating any connection string in all layers that communicates with the RS.

In order to automate tasks like that, I've developed a python script (version 3.9) named rs_migrate.py, it works as wrapper invoking other scripts in an sequence ordered.

The script accepts the following arguments:

--project_id: project id
--cluster_name: replica set name
--inventory: path of the inventory file (json)
--phase: name of the phase that will be executed

There are more arguments, but I'm going to omit in this article.

The inventory file is a json, with the following format:

{
    "sequence": [
        {
            "id": 0,
            "phase": "name fo the phase",
            "action": "script name",
            "arguments": [
                { "argumentOne": "ValueOne" },
                { "argumentTwo": "ValueTwo" },
                ...
            ],
            "status": "pending"
        },
        ...
    ],
    "mongosh": "/usr/bin/mongosh"
}

Each element of sequence field represents a script that will be invoked, with the fields:

  • "id" : sequential number,
  • "phase" : name of the phase
  • "action" : name of the script that's going to be executed: "rs_add.py", "rs_reconfig.py" or "rs_remove.py"
  • "arguments": array of arguments that will be passed to the action script

An API key is required for invoking OPS Manager API, and it must have permission to edit deployments of the project.

A mongosh binary is also required.

Behavior

The script is simple, it reads the json and loop in the sequence array, each element that matches the phase argument is processed, if the status is "pending", it's changed to "running" and then it invokes the script defined at the action field passing all arguments defined at arguments field. When the execution of the action script is finished, it checks the result and then updates the status to "completed". So it will move to the next element of the sequence.

Example

I have Replica Set with the following configuration:

1.00

I defined the inventory file to migrate this Replica Set.

The first phase was called "extension", and it's is composed by 3 elements of rs_add.py on ports 27040, 27041 and 27042.

python3.9 rs_migrate.py --inventory=inventory/rs_migration_sequence.json --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME --phase="extension"

From terminal:

1.00

As result on OPS Manager UI:

1.00

The next phase was named "switch", that's composed by a serie of rs_reconfig.py.

python3.9 rs_migrate.py --inventory=inventory/rs_migration_sequence.json --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME --phase="switch"

From terminal:

1.00

By the end of this phase, we can see all three new nodes priorities and votes higher than the old ones.

1.00

The last phase was named "remove_old_nodes", that's composed by a serie of rs_remove.py.

python3.9 rs_migrate.py --inventory=inventory/rs_migration_sequence.json --project_id=$MONGODB_PROJECTID --cluster_name=$MONGODB_REPLICASETNAME --phase="remove_old_nodes"

From terminal:

1.00

As result on OPS Manager UI, we can see only the new nodes:

1.00

The migration was completed successfully.

Conclusion

This script is able to cover several use cases, we just need to define the inventory file and pass the desired "phase" as argument and the Replica Set will be modified sequentially.

That's all for 2025, Happy New Year!

By Danilo Pala, 30/12/2025