4

I have two machines hosting one mongo instance each.

I need them to work together as a replica set.

I use Ansible for my provisioning.

How can I achieve that ?

Raphaël
  • 1,924
  • 2
  • 18
  • 22
  • Really should have been asked on [dba.stackexchange.com](http://dba.stackexchange.com). But too old to migrate to there. – Blakes Seven Feb 14 '16 at 09:47

1 Answers1

7

This is how I managed it.

I used Stouts.mongodb from Ansible Galaxy.

In playbook.yml:

---
- name: Provision database servers
  hosts: dbservers
  sudo: true
  vars:
    mongodb_conf_replSet=rs-name/db-hostname-1:27017,db-hostname-2:27017
    mongodb_shell:
      db-name:
        - rs.initiate()
  roles:
  - Stouts.mongodb

See the mongo doc for more details about how to set up a replica set.

Raphaël
  • 1,924
  • 2
  • 18
  • 22
  • Hi Raphaël, I was also trying to do that same thing. But I don't have any `db-name` as of now. I have [this](https://gist.github.com/apanimesh061/89296f321ef835caf2c1#file-test-yml) in my playbook. I just need to get in mongo using `mongo 192.168.111.11:27017` and run `rs.initiate()`. Is this right? – Animesh Pandey Mar 17 '16 at 15:30
  • I don't feel like this will work without a db-name. If you don't provide any db name when connected, the db name may be "test" or "local". Test these two or type `db.getName()` in the mongo shell. The idea behind this playbook is not to have to connect after a play. – Raphaël Mar 17 '16 at 18:11
  • I figured out my issue. I added this in another play. So after server restart in first play, the second initialises the replica set correctly. – Animesh Pandey Mar 17 '16 at 19:57
  • 3
    The `rs.initiate()` should be run once per replica set on the **initial primary node**. If you run `rs.initiate()` from all the nodes you may risk getting the same id for several nodes. You can get the `failed with Received heartbeat from member with the same member ID as ourself' error that will prevent the replica from coming up. – RubenLaguna May 02 '16 at 21:55
  • whats ```hosts: dbservers``` what are the hosts you pass there – Tara Prasad Gurung Mar 30 '21 at 07:07