diff --git a/plugins/modules/podman_generate_systemd.py b/plugins/modules/podman_generate_systemd.py index 41ee319..5f7474a 100644 --- a/plugins/modules/podman_generate_systemd.py +++ b/plugins/modules/podman_generate_systemd.py @@ -27,6 +27,12 @@ options: - Use C(/etc/systemd/system) for the system-wide systemd instance. - Use C(/etc/systemd/user) or C(~/.config/systemd/user) for use with per-user instances of systemd. type: path + force: + description: + - Replace the systemd unit file(s) even if it already exists. + - This works with dest option. + type: bool + default: false new: description: - Generate unit files that create containers and pods, not only start them. @@ -446,8 +452,13 @@ def generate_systemd(module): unit_file_name, ) - # See if we need to write the unit file, default yes - need_to_write_file = bool(compare_systemd_file_content(unit_file_full_path, unit_content)) + if module.params['force']: + # Force to replace the existing unit file + need_to_write_file = True + else: + # See if we need to write the unit file, default yes + need_to_write_file = bool(compare_systemd_file_content( + unit_file_full_path, unit_content)) # Write the file, if needed if need_to_write_file: @@ -488,6 +499,11 @@ def run_module(): 'required': False, 'default': False, }, + 'force': { + 'type': 'bool', + 'required': False, + 'default': False, + }, 'restart_policy': { 'type': 'str', 'required': False, diff --git a/tests/integration/targets/podman_generate_systemd/tasks/main.yml b/tests/integration/targets/podman_generate_systemd/tasks/main.yml index 6b0d18c..0b12234 100644 --- a/tests/integration/targets/podman_generate_systemd/tasks/main.yml +++ b/tests/integration/targets/podman_generate_systemd/tasks/main.yml @@ -39,6 +39,48 @@ path: "/tmp/podman_generate_systemd/{{ item.key }}.service" loop: "{{ postgres_local_systemd_unit.systemd_units | dict2items }}" +- name: Try to create a systemd unit file on the same path + containers.podman.podman_generate_systemd: + name: postgres_local + dest: /tmp/podman_generate_systemd + register: generate1 + +- name: Check the unit files exists + ansible.builtin.stat: + path: "/tmp/podman_generate_systemd/{{ item.key }}.service" + loop: "{{ generate1.systemd_units | dict2items }}" + register: unitfile1 + +- name: Get checksum value + set_fact: + checksum1: "{{ item.stat.checksum }}" + with_items: "{{ unitfile1.results }}" + +- name: Force to create a systemd unit file on the same path + containers.podman.podman_generate_systemd: + name: postgres_local + dest: /tmp/podman_generate_systemd + force: true + register: generate2 + +- name: Check the unit files exists again + ansible.builtin.stat: + path: "/tmp/podman_generate_systemd/{{ item.key }}.service" + loop: "{{ generate2.systemd_units | dict2items }}" + register: unitfile2 + +- name: Get checksum value again + set_fact: + checksum2: "{{ item.stat.checksum }}" + with_items: "{{ unitfile2.results }}" + +- name: Check if the sytemd unit files are as expected + assert: + that: + - generate1 is not changed + - generate2 is changed + - checksum1 != checksum2 + - name: Regenerate the systemd units with all the options containers.podman.podman_generate_systemd: name: postgres_local