2

We are deploying mongodb cluster through ansible on AWS EC2 instance. Once a DB instance is provisioned, we give it corresponding tag for Primary / Secondary. We have 3 nodes - 1 for Primary and 2 for secondary.

While running the ansible scripts for the first time, we are choosing the primary instance by Tag type, initiating it and adding secondary nodes to it

rs.initiate()
rs.add(secodnaryHost1)
rs.add(secondaryHost2)

This works fine.

However at times, when one of the secondary becomes the primary, our instance Tag type - primary and mongoDB - primary are not the same. At such a scenario, if we run our ansible scripts, they fail because the primary tag type (which is now a secondary) can't run above commands.

What are the best ways to handles such mismatch?

Deepak Chaudhry
  • 219
  • 4
  • 12
  • It isn't helpful to use Tags to indicate which node is the primary and which are secondary, because (as you have noticed) when the existing primary node goes offline then a different node will be [elected](https://docs.mongodb.com/manual/core/replica-set-elections/) to take the role of primary. Is there any special reason why your Ansible script needs to know which node is the primary? – Vince Bowdren Sep 06 '16 at 12:04

1 Answers1

2

You should read the current status and in a template or through command line define appropriate settings. Please search in available ansible-galaxy roles.

PS:

Example template repset_init.j2 and its provision

- name: Create the file to initialize the mongod replica set
  template: src=repset_init.j2 dest=/tmp/repset_init.js

- name: Pause for a while
  pause: seconds=20

- name: Initialize the replication set
  shell: /usr/bin/mongo --port "{{ mongod_port }}" /tmp/repset_init.js
Valeriy Solovyov
  • 5,384
  • 3
  • 27
  • 45