From 7d95237ebca3b6a7b515ad47f804cd762a2d31f7 Mon Sep 17 00:00:00 2001 From: Roberto Alfieri Date: Tue, 21 Nov 2023 20:03:33 +0100 Subject: [PATCH] Recreate stopped containers if `recreate` flag is enabled If `recreate` flag is enabled we should be able to (force) recreate stopped containers even if they're configuration isn't changed. Signed-off-by: Roberto Alfieri --- .../podman/podman_container_lib.py | 3 ++- .../targets/podman_container/tasks/main.yml | 20 +++++++++++++++++++ 2 files changed, 22 insertions(+), 1 deletion(-) diff --git a/plugins/module_utils/podman/podman_container_lib.py b/plugins/module_utils/podman/podman_container_lib.py index 0433619a..77e44fd6 100644 --- a/plugins/module_utils/podman/podman_container_lib.py +++ b/plugins/module_utils/podman/podman_container_lib.py @@ -1654,7 +1654,8 @@ def make_started(self): self.results['actions'].append('started %s' % self.container.name) self.update_container_result() return - elif self.container.stopped and self.container.different: + elif self.container.stopped and \ + (self.container.different or self.recreate): self.container.recreate_run() self.results['actions'].append('recreated %s' % self.container.name) diff --git a/tests/integration/targets/podman_container/tasks/main.yml b/tests/integration/targets/podman_container/tasks/main.yml index ef651d62..8b6e9caf 100644 --- a/tests/integration/targets/podman_container/tasks/main.yml +++ b/tests/integration/targets/podman_container/tasks/main.yml @@ -235,6 +235,26 @@ fail_msg: "Creating stopped container test failed!" success_msg: "Creating stopped container test passed!" + - name: Force recreate stopped container + containers.podman.podman_container: + executable: "{{ test_executable | default('podman') }}" + name: container + image: alpine:3.7 + state: started + command: sleep 1d + recreate: true + register: recreate_stopped + + - name: Check output is correct + assert: + that: + - recreate_stopped is changed + - recreate_stopped.container is defined + - recreate_stopped.container['State']['Running']|bool + - "'recreated container' in recreate_stopped.actions" + fail_msg: Force recreate stopped test failed! + success_msg: Force recreate stopped test passed! + - name: Delete created container containers.podman.podman_container: executable: "{{ test_executable | default('podman') }}"