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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
|
#!/usr/bin/python
#coding: utf-8 -*-
# (c) 2016, Mathieu Bultel <mbultel@redhat.com>
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
from __future__ import absolute_import, division, print_function
__metaclass__ = type
ANSIBLE_METADATA = {'metadata_version': '1.1',
'status': ['preview'],
'supported_by': 'community'}
DOCUMENTATION = '''
---
module: pacemaker_cluster
short_description: Manage a pacemaker cluster
version_added: "2.3"
author: "Mathieu Bultel (matbu)"
description:
- This module can manage a pacemaker cluster and nodes from Ansible using
the pacemaker cli.
options:
state:
description:
- Indicate desired state of the cluster
choices: ['online', 'offline', 'restart', 'cleanup']
required: true
node:
description:
- Specify which node of the cluster you want to manage. None == the
cluster status itself, 'all' == check the status of all nodes.
required: false
default: None
timeout:
description:
- Timeout when the module should considered that the action has failed
required: false
default: 300
force:
description:
- Force the change of the cluster state
required: false
default: true
requirements:
- "python >= 2.6"
'''
EXAMPLES = '''
---
- name: Set cluster Online
hosts: localhost
gather_facts: no
tasks:
- name: get cluster state
pacemaker_cluster: state=online
'''
RETURN = '''
changed:
description: True if the cluster state has changed
type: bool
returned: always
out:
description: The output of the current state of the cluster. It return a
list of the nodes state.
type: string
sample: 'out: [[" overcloud-controller-0", " Online"]]}'
returned: always
rc:
description: exit code of the module
type: bool
returned: always
'''
import time
from ansible.module_utils.basic import AnsibleModule
_PCS_CLUSTER_DOWN="Error: cluster is not currently running on this node"
def get_cluster_status(module):
cmd = "pcs cluster status"
rc, out, err = module.run_command(cmd)
if out in _PCS_CLUSTER_DOWN:
return 'offline'
else:
return 'online'
def get_node_status(module, node='all'):
if node == 'all':
cmd = "pcs cluster pcsd-status %s" % node
else:
cmd = "pcs cluster pcsd-status"
rc, out, err = module.run_command(cmd)
if rc is 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
status = []
for o in out.splitlines():
status.append(o.split(':'))
return status
def clean_cluster(module, timeout):
cmd = "pcs resource cleanup"
rc, out, err = module.run_command(cmd)
if rc is 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
def set_cluster(module, state, timeout, force):
if state == 'online':
cmd = "pcs cluster start"
if state == 'offline':
cmd = "pcs cluster stop"
if force:
cmd = "%s --force" % cmd
rc, out, err = module.run_command(cmd)
if rc is 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
t = time.time()
ready = False
while time.time() < t+timeout:
cluster_state = get_cluster_status(module)
if cluster_state == state:
ready = True
break
if not ready:
module.fail_json(msg="Failed to set the state `%s` on the cluster\n" % (state))
def set_node(module, state, timeout, force, node='all'):
# map states
if state == 'online':
cmd = "pcs cluster start"
if state == 'offline':
cmd = "pcs cluster stop"
if force:
cmd = "%s --force" % cmd
nodes_state = get_node_status(module, node)
for node in nodes_state:
if node[1].strip().lower() != state:
cmd = "%s %s" % (cmd, node[0].strip())
rc, out, err = module.run_command(cmd)
if rc is 1:
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
t = time.time()
ready = False
while time.time() < t+timeout:
nodes_state = get_node_status(module)
for node in nodes_state:
if node[1].strip().lower() == state:
ready = True
break
if not ready:
module.fail_json(msg="Failed to set the state `%s` on the cluster\n" % (state))
def main():
argument_spec = dict(
state = dict(choices=['online', 'offline', 'restart', 'cleanup']),
node = dict(default=None),
timeout=dict(default=300, type='int'),
force=dict(default=True, type='bool'),
)
module = AnsibleModule(argument_spec,
supports_check_mode=True,
)
changed = False
state = module.params['state']
node = module.params['node']
force = module.params['force']
timeout = module.params['timeout']
if state in ['online', 'offline']:
# Get cluster status
if node is None:
cluster_state = get_cluster_status(module)
if cluster_state == state:
module.exit_json(changed=changed,
out=cluster_state)
else:
set_cluster(module, state, timeout, force)
cluster_state = get_cluster_status(module)
if cluster_state == state:
module.exit_json(changed=True,
out=cluster_state)
else:
module.fail_json(msg="Fail to bring the cluster %s" % state)
else:
cluster_state = get_node_status(module, node)
# Check cluster state
for node_state in cluster_state:
if node_state[1].strip().lower() == state:
module.exit_json(changed=changed,
out=cluster_state)
else:
# Set cluster status if needed
set_cluster(module, state, timeout, force)
cluster_state = get_node_status(module, node)
module.exit_json(changed=True,
out=cluster_state)
if state in ['restart']:
set_cluster(module, 'offline', timeout, force)
cluster_state = get_cluster_status(module)
if cluster_state == 'offline':
set_cluster(module, 'online', timeout, force)
cluster_state = get_cluster_status(module)
if cluster_state == 'online':
module.exit_json(changed=True,
out=cluster_state)
else:
module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be started")
else:
module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be stopped")
if state in ['cleanup']:
clean_cluster(module, timeout)
cluster_state = get_cluster_status(module)
module.exit_json(changed=True,
out=cluster_state)
if __name__ == '__main__':
main()
|