From 22946365fc0fd1adc55f64b3c1d2a68335924d98 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 27 Aug 2025 22:09:33 +0200 Subject: [PATCH] [PR #10413/3b09e9d9 backport][stable-11] pacemaker_resource: add cleanup state (#10750) pacemaker_resource: add cleanup state (#10413) * refactor(deprecate): Add cleanup deprecations for pacemaker_cluster * Additional code review changes * Add changelog fragment (cherry picked from commit 3b09e9d9edc3c222a526d7454735cb00e46aab65) Co-authored-by: Dexter <45038532+munchtoast@users.noreply.github.com> --- .../10413-pacemaker-resource-cleanup.yml | 3 + plugins/modules/pacemaker_resource.py | 23 ++++-- .../modules/test_pacemaker_resource.yaml | 75 ++++++++++++++++++- 3 files changed, 95 insertions(+), 6 deletions(-) create mode 100644 changelogs/fragments/10413-pacemaker-resource-cleanup.yml diff --git a/changelogs/fragments/10413-pacemaker-resource-cleanup.yml b/changelogs/fragments/10413-pacemaker-resource-cleanup.yml new file mode 100644 index 0000000000..f4157559cc --- /dev/null +++ b/changelogs/fragments/10413-pacemaker-resource-cleanup.yml @@ -0,0 +1,3 @@ +minor_changes: + - pacemaker_resource - add ``state=cleanup`` for cleaning up pacemaker resources (https://github.com/ansible-collections/community.general/pull/10413) + - pacemaker_resource - the parameter ``name`` is no longer a required parameter in community.general 11.3.0 (https://github.com/ansible-collections/community.general/pull/10413) diff --git a/plugins/modules/pacemaker_resource.py b/plugins/modules/pacemaker_resource.py index 2fdf785487..3f278d3cd2 100644 --- a/plugins/modules/pacemaker_resource.py +++ b/plugins/modules/pacemaker_resource.py @@ -27,13 +27,14 @@ options: state: description: - Indicate desired state for cluster resource. - choices: [present, absent, enabled, disabled] + - The state V(cleanup) has been added in community.general 11.3.0. + choices: [present, absent, enabled, disabled, cleanup] default: present type: str name: description: - Specify the resource name to create. - required: true + - This is required if O(state=present), O(state=absent), O(state=enabled), or O(state=disabled). type: str resource_type: description: @@ -141,8 +142,8 @@ class PacemakerResource(StateModuleHelper): module = dict( argument_spec=dict( state=dict(type='str', default='present', choices=[ - 'present', 'absent', 'enabled', 'disabled']), - name=dict(type='str', required=True), + 'present', 'absent', 'enabled', 'disabled', 'cleanup']), + name=dict(type='str'), resource_type=dict(type='dict', options=dict( resource_name=dict(type='str'), resource_standard=dict(type='str'), @@ -160,7 +161,12 @@ class PacemakerResource(StateModuleHelper): )), wait=dict(type='int', default=300), ), - required_if=[('state', 'present', ['resource_type', 'resource_option'])], + required_if=[ + ('state', 'present', ['resource_type', 'resource_option', 'name']), + ('state', 'absent', ['name']), + ('state', 'enabled', ['name']), + ('state', 'disabled', ['name']), + ], supports_check_mode=True, ) @@ -208,6 +214,13 @@ class PacemakerResource(StateModuleHelper): with self.runner('cli_action state name', output_process=self._process_command_output(True, "Stopped"), check_mode_skip=True) as ctx: ctx.run(cli_action='resource') + def state_cleanup(self): + runner_args = ['cli_action', 'state'] + if self.module.params['name']: + runner_args.append('name') + with self.runner(runner_args, output_process=self._process_command_output(True, "Clean"), check_mode_skip=True) as ctx: + ctx.run(cli_action='resource') + def main(): PacemakerResource.execute() diff --git a/tests/unit/plugins/modules/test_pacemaker_resource.yaml b/tests/unit/plugins/modules/test_pacemaker_resource.yaml index bb56ec1bbe..3a6c74a7eb 100644 --- a/tests/unit/plugins/modules/test_pacemaker_resource.yaml +++ b/tests/unit/plugins/modules/test_pacemaker_resource.yaml @@ -11,7 +11,7 @@ test_cases: input: {} output: failed: true - msg: "missing required arguments: name" + msg: "state is present but all of the following are missing: resource_type, name" - id: test_present_minimal_input_resource_not_exist input: state: present @@ -423,3 +423,76 @@ test_cases: rc: 0 out: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Stopped (disabled)" err: "" + - id: test_cleanup_minimal_input_initial_resources_not_exist + input: + state: cleanup + output: + changed: false + previous_value: "NO resources configured" + value: "NO resources configured" + mocks: + run_command: + - command: [/testbin/pcs, resource, status] + environ: *env-def + rc: 0 + out: "NO resources configured" + err: "" + - command: [/testbin/pcs, resource, cleanup] + environ: *env-def + rc: 0 + out: "Cleaned up all resources on all nodes" + err: "" + - command: [/testbin/pcs, resource, status] + environ: *env-def + rc: 0 + out: "NO resources configured" + err: "" + - id: test_cleanup_minimal_input_initial_resources_exists + input: + state: cleanup + output: + changed: true + previous_value: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started" + value: "NO resources configured" + mocks: + run_command: + - command: [/testbin/pcs, resource, status] + environ: *env-def + rc: 0 + out: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started" + err: "" + - command: [/testbin/pcs, resource, cleanup] + environ: *env-def + rc: 0 + out: "Cleaned up all resources on all nodes" + err: "" + - command: [/testbin/pcs, resource, status] + environ: *env-def + rc: 0 + out: "NO resources configured" + err: "" + - id: test_cleanup_specific_minimal_input_initial_resources_exists + input: + state: cleanup + name: virtual-ip + output: + changed: true + previous_value: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started" + value: "NO resources configured" + mocks: + run_command: + - command: [/testbin/pcs, resource, status, virtual-ip] + environ: *env-def + rc: 0 + out: " * virtual-ip\t(ocf:heartbeat:IPAddr2):\t Started" + err: "" + - command: [/testbin/pcs, resource, cleanup, virtual-ip] + environ: *env-def + rc: 0 + out: "Cleaned up virtual-ip on X" + err: "" + - command: [/testbin/pcs, resource, status, virtual-ip] + environ: *env-def + rc: 0 + out: "NO resources configured" + err: ""