How can I setup mongo replica set with ansible?

Raphaël picture Raphaël · Aug 19, 2015 · Viewed 7.2k times · Source

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 ?

Answer

Raphaël picture Raphaël · Aug 19, 2015

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.