2020-03-09 09:11:07 +00:00
|
|
|
#!/usr/bin/python
|
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
2022-08-05 10:28:29 +00:00
|
|
|
# Copyright (c) 2016, Mathieu Bultel <mbultel@redhat.com>
|
|
|
|
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt)
|
|
|
|
# SPDX-License-Identifier: GPL-3.0-or-later
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
from __future__ import absolute_import, division, print_function
|
|
|
|
__metaclass__ = type
|
|
|
|
|
2024-12-26 07:22:24 +00:00
|
|
|
DOCUMENTATION = r"""
|
2020-03-09 09:11:07 +00:00
|
|
|
module: pacemaker_cluster
|
|
|
|
short_description: Manage pacemaker clusters
|
|
|
|
author:
|
2023-02-20 16:29:14 +00:00
|
|
|
- Mathieu Bultel (@matbu)
|
2020-03-09 09:11:07 +00:00
|
|
|
description:
|
2024-12-26 07:22:24 +00:00
|
|
|
- This module can manage a pacemaker cluster and nodes from Ansible using the pacemaker CLI.
|
2023-02-20 16:29:14 +00:00
|
|
|
extends_documentation_fragment:
|
|
|
|
- community.general.attributes
|
|
|
|
attributes:
|
2024-12-26 07:22:24 +00:00
|
|
|
check_mode:
|
|
|
|
support: full
|
|
|
|
diff_mode:
|
|
|
|
support: none
|
2020-03-09 09:11:07 +00:00
|
|
|
options:
|
2024-12-26 07:22:24 +00:00
|
|
|
state:
|
|
|
|
description:
|
|
|
|
- Indicate desired state of the cluster.
|
|
|
|
choices: [cleanup, offline, online, restart]
|
|
|
|
type: str
|
|
|
|
node:
|
|
|
|
description:
|
2025-01-08 19:41:03 +00:00
|
|
|
- Specify which node of the cluster you want to manage. V(null) == the cluster status itself, V(all) == check the status
|
|
|
|
of all nodes.
|
2024-12-26 07:22:24 +00:00
|
|
|
type: str
|
|
|
|
timeout:
|
|
|
|
description:
|
|
|
|
- Timeout when the module should considered that the action has failed.
|
|
|
|
default: 300
|
|
|
|
type: int
|
|
|
|
force:
|
|
|
|
description:
|
|
|
|
- Force the change of the cluster state.
|
|
|
|
type: bool
|
|
|
|
default: true
|
|
|
|
"""
|
|
|
|
|
|
|
|
EXAMPLES = r"""
|
2020-03-09 09:11:07 +00:00
|
|
|
- name: Set cluster Online
|
|
|
|
hosts: localhost
|
2022-08-24 18:00:11 +00:00
|
|
|
gather_facts: false
|
2020-03-09 09:11:07 +00:00
|
|
|
tasks:
|
2024-12-26 07:22:24 +00:00
|
|
|
- name: Get cluster state
|
|
|
|
community.general.pacemaker_cluster:
|
|
|
|
state: online
|
|
|
|
"""
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2024-12-26 07:22:24 +00:00
|
|
|
RETURN = r"""
|
2020-03-09 09:11:07 +00:00
|
|
|
out:
|
2024-12-26 07:22:24 +00:00
|
|
|
description: The output of the current state of the cluster. It returns a list of the nodes state.
|
|
|
|
type: str
|
|
|
|
sample: 'out: [[" overcloud-controller-0", " Online"]]}'
|
|
|
|
returned: always
|
|
|
|
"""
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
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):
|
2024-12-30 10:49:29 +00:00
|
|
|
cmd = ["pcs", "cluster", "status"]
|
2020-03-09 09:11:07 +00:00
|
|
|
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'):
|
2024-12-30 10:49:29 +00:00
|
|
|
node_l = ["all"] if node == "all" else []
|
|
|
|
cmd = ["pcs", "cluster", "pcsd-status"] + node_l
|
2020-03-09 09:11:07 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 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):
|
2024-12-30 10:49:29 +00:00
|
|
|
cmd = ["pcs", "resource", "cleanup"]
|
2020-03-09 09:11:07 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 1:
|
|
|
|
module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err))
|
|
|
|
|
|
|
|
|
|
|
|
def set_cluster(module, state, timeout, force):
|
|
|
|
if state == 'online':
|
2024-12-30 10:49:29 +00:00
|
|
|
cmd = ["pcs", "cluster", "start"]
|
2020-03-09 09:11:07 +00:00
|
|
|
if state == 'offline':
|
2024-12-30 10:49:29 +00:00
|
|
|
cmd = ["pcs", "cluster", "stop"]
|
2020-03-09 09:11:07 +00:00
|
|
|
if force:
|
2024-12-30 10:49:29 +00:00
|
|
|
cmd = cmd + ["--force"]
|
2020-03-09 09:11:07 +00:00
|
|
|
rc, out, err = module.run_command(cmd)
|
|
|
|
if rc == 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 main():
|
|
|
|
argument_spec = dict(
|
|
|
|
state=dict(type='str', choices=['online', 'offline', 'restart', 'cleanup']),
|
|
|
|
node=dict(type='str'),
|
|
|
|
timeout=dict(type='int', default=300),
|
|
|
|
force=dict(type='bool', default=True),
|
|
|
|
)
|
|
|
|
|
|
|
|
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:
|
2024-03-14 21:11:13 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2020-03-09 09:11:07 +00:00
|
|
|
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:
|
2024-03-14 21:11:13 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2020-03-09 09:11:07 +00:00
|
|
|
# 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)
|
|
|
|
|
2024-12-30 10:49:29 +00:00
|
|
|
elif state == 'restart':
|
2024-03-14 21:11:13 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2020-03-09 09:11:07 +00:00
|
|
|
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:
|
2024-12-30 10:49:29 +00:00
|
|
|
module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be started")
|
2020-03-09 09:11:07 +00:00
|
|
|
else:
|
2024-12-30 10:49:29 +00:00
|
|
|
module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be stopped")
|
2020-03-09 09:11:07 +00:00
|
|
|
|
2024-12-30 10:49:29 +00:00
|
|
|
elif state == 'cleanup':
|
2024-03-14 21:11:13 +00:00
|
|
|
if module.check_mode:
|
|
|
|
module.exit_json(changed=True)
|
2020-03-09 09:11:07 +00:00
|
|
|
clean_cluster(module, timeout)
|
|
|
|
cluster_state = get_cluster_status(module)
|
2024-12-30 10:49:29 +00:00
|
|
|
module.exit_json(changed=True, out=cluster_state)
|
2020-03-09 09:11:07 +00:00
|
|
|
|
|
|
|
|
|
|
|
if __name__ == '__main__':
|
|
|
|
main()
|