diff --git a/lib/ansible/modules/clustering/pacemaker_cluster.py b/lib/ansible/modules/clustering/pacemaker_cluster.py index 169712076b..2503125616 100644 --- a/lib/ansible/modules/clustering/pacemaker_cluster.py +++ b/lib/ansible/modules/clustering/pacemaker_cluster.py @@ -1,24 +1,23 @@ #!/usr/bin/python -#coding: utf-8 -*- +# -*- coding: utf-8 -*- -# (c) 2016, Mathieu Bultel +# Copyright: (c) 2016, Mathieu Bultel # 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 +short_description: Manage pacemaker clusters version_added: "2.3" -author: "Mathieu Bultel (matbu)" +author: +- Mathieu Bultel (@matbu) description: - This module can manage a pacemaker cluster and nodes from Ansible using the pacemaker cli. @@ -26,26 +25,21 @@ options: state: description: - Indicate desired state of the cluster - choices: ['online', 'offline', 'restart', 'cleanup'] - required: true + choices: [ cleanup, offline, online, restart ] + required: yes 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" + type: bool + default: 'yes' ''' EXAMPLES = ''' --- @@ -53,8 +47,9 @@ EXAMPLES = ''' hosts: localhost gather_facts: no tasks: - - name: get cluster state - pacemaker_cluster: state=online + - name: Get cluster state + pacemaker_cluster: + state: online ''' RETURN = ''' @@ -79,7 +74,7 @@ import time from ansible.module_utils.basic import AnsibleModule -_PCS_CLUSTER_DOWN="Error: cluster is not currently running on this node" +_PCS_CLUSTER_DOWN = "Error: cluster is not currently running on this node" def get_cluster_status(module): @@ -90,6 +85,7 @@ def get_cluster_status(module): else: return 'online' + def get_node_status(module, node='all'): if node == 'all': cmd = "pcs cluster pcsd-status %s" % node @@ -103,12 +99,14 @@ def get_node_status(module, node='all'): 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" @@ -122,7 +120,7 @@ def set_cluster(module, state, timeout, force): t = time.time() ready = False - while time.time() < t+timeout: + while time.time() < t + timeout: cluster_state = get_cluster_status(module) if cluster_state == state: ready = True @@ -130,6 +128,7 @@ def set_cluster(module, state, timeout, force): 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': @@ -149,7 +148,7 @@ def set_node(module, state, timeout, force, node='all'): t = time.time() ready = False - while time.time() < t+timeout: + while time.time() < t + timeout: nodes_state = get_node_status(module) for node in nodes_state: if node[1].strip().lower() == state: @@ -158,15 +157,17 @@ def set_node(module, state, timeout, force, node='all'): 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'), + 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, + module = AnsibleModule( + argument_spec, supports_check_mode=True, ) changed = False @@ -180,14 +181,12 @@ def main(): if node is None: cluster_state = get_cluster_status(module) if cluster_state == state: - module.exit_json(changed=changed, - out=cluster_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) + module.exit_json(changed=True, out=cluster_state) else: module.fail_json(msg="Fail to bring the cluster %s" % state) else: @@ -195,14 +194,12 @@ def main(): # Check cluster state for node_state in cluster_state: if node_state[1].strip().lower() == state: - module.exit_json(changed=changed, - out=cluster_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) + module.exit_json(changed=True, out=cluster_state) if state in ['restart']: set_cluster(module, 'offline', timeout, force) @@ -211,8 +208,7 @@ def main(): set_cluster(module, 'online', timeout, force) cluster_state = get_cluster_status(module) if cluster_state == 'online': - module.exit_json(changed=True, - out=cluster_state) + 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: diff --git a/test/sanity/pep8/legacy-files.txt b/test/sanity/pep8/legacy-files.txt index 1c52cb30a4..dd6d90bdb0 100644 --- a/test/sanity/pep8/legacy-files.txt +++ b/test/sanity/pep8/legacy-files.txt @@ -110,7 +110,6 @@ lib/ansible/modules/cloud/webfaction/webfaction_db.py lib/ansible/modules/cloud/webfaction/webfaction_domain.py lib/ansible/modules/cloud/webfaction/webfaction_site.py lib/ansible/modules/clustering/consul_session.py -lib/ansible/modules/clustering/pacemaker_cluster.py lib/ansible/modules/database/misc/kibana_plugin.py lib/ansible/modules/database/misc/riak.py lib/ansible/modules/database/mongodb/mongodb_parameter.py