Created
October 28, 2015 20:02
-
-
Save maxrothman/4a50992ca89c922e7f68 to your computer and use it in GitHub Desktop.
An Ansible playbook that changes the name of a mongo replication set
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
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
Hi..I see below error when I use these last three tasks