Skip to content

Instantly share code, notes, and snippets.

@maxrothman
Created October 28, 2015 20:02
Show Gist options
  • Save maxrothman/4a50992ca89c922e7f68 to your computer and use it in GitHub Desktop.
Save maxrothman/4a50992ca89c922e7f68 to your computer and use it in GitHub Desktop.
An Ansible playbook that changes the name of a mongo replication set
# Renames a mongo replication set
# This necessarily resyncs the secondaries, taking them offline, but total downtime on the primary should be short.
#
# Usage:
# ansible-playbook rename_replset.yml -e 'password=SuperSecretAdminPassword name=new_replset_name' -i host1,host2,host3
#
# Note: the first host in the specified inventory will become the new primary
- hosts: all
vars:
password: null
name: null
replset_config:
- "replication:"
- "\\s+replSetName:.*"
tasks:
- fail: '{{ item }} must be set. Pass it with "-e {{ item }}=value"'
when: item is none
with_items:
- password
- name
- name: Remove replset config
become: true
replace:
dest: /etc/mongod.conf
regexp: "^({{ item }})"
replace: "#\\1"
with_items: replset_config
- name: Restart mongod
become: true
service:
name: mongod
state: restarted
# The "local" database stores the replset config. We've got to wipe it out to make a new replset.
- name: Drop local replset config
shell: >
/usr/bin/mongo -u admin -p {{ password }} --authenticationDatabase admin local
--eval 'printjson(db.dropDatabase())'
- name: Switch to new replset name
become: true
replace:
dest: /etc/mongod.conf
regexp: "(^#\\s+replSetName:)(.*)"
replace: "\\1 {{ name }}"
- name: Re-add replset config
become: true
replace:
dest: /etc/mongod.conf
regexp: "^#({{ item }})"
replace: "\\1"
with_items: replset_config
- name: Restart mongod
become: true
service:
name: mongod
state: restarted
- name: Initialize replica set
run_once: true
delegate_to: "{{ play_hosts[0] }}"
shell: >
/usr/bin/mongo -u admin -p {{ password }} admin --eval 'printjson(rs.initiate())'
- name: Format secondaries
run_once: true
local_action:
module: debug
msg: '"{{ item }}:27017"'
with_items: play_hosts[1:]
register: secondaries
- name: Add secondaries
run_once: true
delegate_to: "{{ play_hosts[0] }}"
shell: >
/usr/bin/mongo -u admin -p {{ password }} admin --eval 'printjson(rs.add({{ item.msg }}))'
with_items: secondaries.results
@rashminair90
Copy link

Hi..I see below error when I use these last three tasks

TASK [mongodb-setup : Add secondaries] *******************************
fatal: [host1]: FAILED! => {"msg": "The task includes an option with an undefined variable. The error was: 'ansible.utils.unsafe_proxy.AnsibleUnsafeText object' has no attribute 'msg'\n\nThe error appears to have been in '/var/lib/awx/projects/_dev/roles/mongodb-setup/tasks/users.yml': line 15, column 3, but may\nbe elsewhere in the file depending on the exact syntax problem.\n\nThe offending line appears to be:\n\n\n- name: Add secondaries\n  ^ here\n"}
mong

@maxrothman
Copy link
Author

I last used this playbook in October 2015. Since then Ansible and possibly MongoDB have evolved significantly, so it's more than likely that this playbook no longer works. Pull requests welcome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment