Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a force field to podman_generate_systemd #624

Merged
merged 4 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 18 additions & 2 deletions plugins/modules/podman_generate_systemd.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,12 @@
- 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.
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -488,6 +499,11 @@ def run_module():
'required': False,
'default': False,
},
'force': {
'type': 'bool',
'required': False,
'default': False,
},
'restart_policy': {
'type': 'str',
'required': False,
Expand Down
42 changes: 42 additions & 0 deletions tests/integration/targets/podman_generate_systemd/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down