forked from openedx-unsupported/configuration
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mongo_rolling_upgrade.yml
155 lines (151 loc) · 5.82 KB
/
mongo_rolling_upgrade.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# Upgrades a full mongo cluster, starting with the hidden members, then the
# secondary, and finally steps down the primary and upgrades it. It checks along
# the way for a healthy cluster, failing if that is not true.
#
# This play expects to have access to a config file where MONGO_RS_CONFIG, as described
# in the mongo_3_0 role, is defined, as well as MONGO_ADMIN_USER and MONGO_ADMIN_PASSWORD.
#
# ansible-playbook -i 127.0.0.1, mongo_rolling_upgrade.yml -e@/path/to/config-file.yml
#
# This play uses MONGO_RS_CONFIG to find a host to connect to and fetch replset config and build an
# inventory, so you can just target localhost.
#
# If there are no hidden secondaries, the 'Upgrade hidden members' task block will just skip.
#
# This will process a hidden secondary twice - first as a 'hidden' server, then as a 'secondary' but
# this is effectively a no-op except for apt checking the versions and then checking that mongo is running.
# It is valid to have other types of hidden machines, so this seemed better than skipping.
#
# If you wish to avoid updating the primary, you can add -e 'SKIP_PRIMARY=true' to your ansible
# invocation.
- name: Find hidden secondaries
hosts: 127.0.0.1
connection: local
gather_facts: False
vars:
- SKIP_PRIMARY: False
tasks:
- name: Get configuration of mongo cluster
mongodb_rs_config:
host: "{{ (MONGO_RS_CONFIG.members|map(attribute='host')|list)[0] }}"
username: "{{ MONGO_ADMIN_USER }}"
password: "{{ MONGO_ADMIN_PASSWORD }}"
register: rs_config
- name: Build inventory of hidden members
add_host:
hostname: "{{ (item.host.split(':'))[0] }}"
instance_id: "{{ item._id }}"
groups: hidden_hosts
ansible_ssh_user: ubuntu
with_items:
- "{{ rs_config.hidden }}"
- name: Build inventory of secondary members
add_host:
hostname: "{{ (item.host.split(':'))[0] }}"
instance_id: "{{ item._id }}"
groups: secondary_hosts
ansible_ssh_user: ubuntu
with_items:
- "{{ rs_config.secondary }}"
- name: Build inventory of primary members
add_host:
hostname: "{{ (item.host.split(':'))[0] }}"
instance_id: "{{ item._id }}"
groups: primary_hosts
ansible_ssh_user: ubuntu
with_items:
- "{{ rs_config.primary }}"
when: not SKIP_PRIMARY
- name: Upgrade hidden members
hosts: hidden_hosts
gather_facts: True
become: True
vars_files:
- ../roles/mongo_3_0/defaults/main.yml
tasks:
- name: install mongo server and recommends
apt:
pkg: "{{ item }}"
state: present
install_recommends: yes
force: yes
update_cache: yes
with_items: "{{ mongodb_debian_pkgs }}"
- name: wait for mongo server to start
wait_for:
port: 27017
delay: 2
- name: Wait for the replica set to update and (if needed) elect a primary
mongodb_rs_status:
host: "{{ ansible_default_ipv4['address'] }}"
username: "{{ MONGO_ADMIN_USER }}"
password: "{{ MONGO_ADMIN_PASSWORD }}"
register: status
# This ensures that no servers are in a state other than PRIMARY or SECONDARY. https://docs.mongodb.com/manual/reference/replica-states/
until: status.status is defined and not (['PRIMARY','SECONDARY'] | symmetric_difference(status.status.members|map(attribute='stateStr')|list|unique))
retries: 5
delay: 2
- name: Upgrade secondary members
hosts: secondary_hosts
gather_facts: True
become: True
serial: 1
vars_files:
- ../roles/mongo_3_0/defaults/main.yml
tasks:
- name: install mongo server and recommends
apt:
pkg: "{{ item }}"
state: present
install_recommends: yes
force: yes
update_cache: yes
with_items: "{{ mongodb_debian_pkgs }}"
- name: wait for mongo server to start
wait_for:
port: 27017
delay: 2
- name: Wait for the replica set to update and (if needed) elect a primary
mongodb_rs_status:
host: "{{ ansible_default_ipv4['address'] }}"
username: "{{ MONGO_ADMIN_USER }}"
password: "{{ MONGO_ADMIN_PASSWORD }}"
register: status
# This ensures that no servers are in a state other than PRIMARY or SECONDARY. https://docs.mongodb.com/manual/reference/replica-states/
until: status.status is defined and not (['PRIMARY','SECONDARY'] | symmetric_difference(status.status.members|map(attribute='stateStr')|list|unique))
retries: 5
delay: 2
- name: Upgrade primary members
hosts: primary_hosts
gather_facts: True
become: True
vars_files:
- ../roles/mongo_3_0/defaults/main.yml
tasks:
- name: Step down (this can take up to a minute to complete while the primary waits on a secondary)
mongodb_step_down:
host: "{{ ansible_default_ipv4['address'] }}"
username: "{{ MONGO_ADMIN_USER }}"
password: "{{ MONGO_ADMIN_PASSWORD }}"
- name: install mongo server and recommends
apt:
pkg: "{{ item }}"
state: present
install_recommends: yes
force: yes
update_cache: yes
with_items: "{{ mongodb_debian_pkgs }}"
- name: wait for mongo server to start
wait_for:
port: 27017
delay: 2
- name: Wait for the replica set to update and (if needed) elect a primary
mongodb_rs_status:
host: "{{ ansible_default_ipv4['address'] }}"
username: "{{ MONGO_ADMIN_USER }}"
password: "{{ MONGO_ADMIN_PASSWORD }}"
register: status
# This ensures that no servers are in a state other than PRIMARY or SECONDARY. https://docs.mongodb.com/manual/reference/replica-states/
until: status.status is defined and not (['PRIMARY','SECONDARY'] | symmetric_difference(status.status.members|map(attribute='stateStr')|list|unique))
retries: 5
delay: 2