From 4a1a8d6b3810d40c482d7416780b196b6f373a69 Mon Sep 17 00:00:00 2001 From: Alexei Znamensky <103110+russoz@users.noreply.github.com> Date: Mon, 30 Dec 2024 23:49:29 +1300 Subject: [PATCH] pacemaker_cluster: fix run_command() calls (#9471) * pacemaker_cluster: fix run_command() calls * add changelog frag * remove set_node() --- .../fragments/9387-pacemaker-cluster-cmd.yml | 3 + plugins/modules/pacemaker_cluster.py | 56 ++++--------------- 2 files changed, 15 insertions(+), 44 deletions(-) create mode 100644 changelogs/fragments/9387-pacemaker-cluster-cmd.yml diff --git a/changelogs/fragments/9387-pacemaker-cluster-cmd.yml b/changelogs/fragments/9387-pacemaker-cluster-cmd.yml new file mode 100644 index 0000000000..d9cc4c35a4 --- /dev/null +++ b/changelogs/fragments/9387-pacemaker-cluster-cmd.yml @@ -0,0 +1,3 @@ +minor_changes: + - pacemaker_cluster - using safer mechanism to run external command (https://github.com/ansible-collections/community.general/pull/9471). + - pacemaker_cluster - remove unused code (https://github.com/ansible-collections/community.general/pull/9471). diff --git a/plugins/modules/pacemaker_cluster.py b/plugins/modules/pacemaker_cluster.py index af8bb5ff56..fdae7fc367 100644 --- a/plugins/modules/pacemaker_cluster.py +++ b/plugins/modules/pacemaker_cluster.py @@ -71,7 +71,7 @@ _PCS_CLUSTER_DOWN = "Error: cluster is not currently running on this node" def get_cluster_status(module): - cmd = "pcs cluster status" + cmd = ["pcs", "cluster", "status"] rc, out, err = module.run_command(cmd) if out in _PCS_CLUSTER_DOWN: return 'offline' @@ -80,10 +80,8 @@ def get_cluster_status(module): def get_node_status(module, node='all'): - if node == 'all': - cmd = "pcs cluster pcsd-status %s" % node - else: - cmd = "pcs cluster pcsd-status" + node_l = ["all"] if node == "all" else [] + cmd = ["pcs", "cluster", "pcsd-status"] + node_l rc, out, err = module.run_command(cmd) if rc == 1: module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) @@ -94,7 +92,7 @@ def get_node_status(module, node='all'): def clean_cluster(module, timeout): - cmd = "pcs resource cleanup" + cmd = ["pcs", "resource", "cleanup"] rc, out, err = module.run_command(cmd) if rc == 1: module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) @@ -102,11 +100,11 @@ def clean_cluster(module, timeout): def set_cluster(module, state, timeout, force): if state == 'online': - cmd = "pcs cluster start" + cmd = ["pcs", "cluster", "start"] if state == 'offline': - cmd = "pcs cluster stop" + cmd = ["pcs", "cluster", "stop"] if force: - cmd = "%s --force" % cmd + cmd = cmd + ["--force"] rc, out, err = module.run_command(cmd) if rc == 1: module.fail_json(msg="Command execution failed.\nCommand: `%s`\nError: %s" % (cmd, err)) @@ -122,35 +120,6 @@ def set_cluster(module, state, timeout, force): 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 == 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(type='str', choices=['online', 'offline', 'restart', 'cleanup']), @@ -198,7 +167,7 @@ def main(): cluster_state = get_node_status(module, node) module.exit_json(changed=True, out=cluster_state) - if state in ['restart']: + elif state == 'restart': if module.check_mode: module.exit_json(changed=True) set_cluster(module, 'offline', timeout, force) @@ -209,17 +178,16 @@ def main(): 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") + module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be started") else: - module.fail_json(msg="Failed during the restart of the cluster, the cluster can't be stopped") + module.fail_json(msg="Failed during the restart of the cluster, the cluster cannot be stopped") - if state in ['cleanup']: + elif state == 'cleanup': if module.check_mode: module.exit_json(changed=True) clean_cluster(module, timeout) cluster_state = get_cluster_status(module) - module.exit_json(changed=True, - out=cluster_state) + module.exit_json(changed=True, out=cluster_state) if __name__ == '__main__':