diff --git a/README.md b/README.md index 88af9ad..95b5610 100644 --- a/README.md +++ b/README.md @@ -222,6 +222,89 @@ podman_pods: restart_policy: "always" ``` +### Systemd Service Generation + +The role can automatically generate systemd service files for containers and pods. This functionality helps in managing container lifecycle through systemd. + +* `podman_generate_systemd`: Enable systemd service generation (default: `true`) +* `podman_systemd_dir`: Directory for generated service files (default: `/etc/systemd/system`) + +**Global Systemd Options** (`podman_systemd_options`): + +```yaml +podman_systemd_options: + new: true # Generate new service files + force: true # Overwrite existing files + restart_policy: unless-stopped # Default restart policy + time: 120 # Stop timeout in seconds + no_header: false # Include header in service files + wants: [] # Systemd unit Wants + after: [] # Systemd unit After + requires: [] # Systemd unit Requires + container_prefix: "" # Prefix for container service names + pod_prefix: "" # Prefix for pod service names + restart_sec: 30 # Restart delay in seconds +``` + +**Per-Container/Pod Configuration:** + +You can override global systemd options for individual containers or pods: + +```yaml +podman_containers: + - name: webapp + image: nginx:latest + systemd: + restart_policy: always + after: ["network.target"] + wants: ["network-online.target"] + restart_sec: 10 + +podman_pods: + - name: database + systemd: + restart_policy: on-failure + requires: ["network.target"] + time: 180 +``` + +When `systemd` is defined for a container or pod, the role will: +1. Generate a systemd service file +2. Place it in the specified directory +3. Reload systemd daemon +4. (Optional) Enable and start the service + +**Note:** Container/pod-specific options take precedence over global options defined in `podman_systemd_options`. + +### Resource Pruning + +The role can automatically clean up unused Podman resources to free up disk space and maintain system hygiene. + +* `podman_prune_enabled`: Enable automatic pruning of unused resources (default: `true`) +* `podman_prune_options`: Configuration for what should be pruned + +```yaml +podman_prune_options: + container: true # Remove stopped containers + image: true # Remove unused images + network: true # Remove unused networks + system: true # Prune all unused data + system_all: true # Prune all unused data including build cache + volume: true # Remove unused volumes +``` + +You can selectively disable certain types of pruning by setting their values to `false`: + +```yaml +podman_prune_options: + container: true # Still remove containers + image: false # Keep all images + network: true # Remove unused networks + system: false # Keep system data + system_all: false # Keep build cache + volume: false # Keep all volumes +``` + Dependencies ------------ diff --git a/defaults/main.yml b/defaults/main.yml index 7ee6eaf..e4787b5 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -601,3 +601,41 @@ podman_pods: [] # generate_systemd: # path: "/etc/systemd/system" # restart_policy: "always" +# stop_timeout: 120 +# names: true +# container_prefix: "container" +# new: false +# no_header: false +# wants: +# - "network-online.target" +# after: +# - "network-online.target" +# requires: +# - "postgresql.service" + +# Systemd service generation configuration +podman_generate_systemd: true +podman_systemd_dir: /etc/systemd/system +podman_systemd_options: + new: true + force: true + restart_policy: unless-stopped + time: 120 + no_header: false + separator: "" + wants: [] + after: [] + requires: [] + container_prefix: "" + pod_prefix: "" + restart_sec: 30 + +### Prune Configuration +podman_prune_enabled: true +podman_prune_options: + container: true # Remove stopped containers + image: true # Remove unused images + network: true # Remove unused networks + system: true # Prune all unused data + system_all: true # Prune all unused data including build cache + volume: true # Remove unused volumes diff --git a/handlers/main.yml b/handlers/main.yml index 370d646..e44081e 100644 --- a/handlers/main.yml +++ b/handlers/main.yml @@ -6,3 +6,7 @@ name: podman state: restarted listen: restart podman + +- name: Reload systemd + ansible.builtin.systemd: + daemon_reload: true diff --git a/tasks/main.yml b/tasks/main.yml index a560e4f..bd561a2 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -46,3 +46,16 @@ tags: - podman - podman-containers + +- name: Generate systemd services + ansible.builtin.include_tasks: systemd.yml + when: podman_generate_systemd | bool + tags: + - podman + - podman-systemd + +- name: Prune unused resources + ansible.builtin.include_tasks: prune.yml + tags: + - podman + - podman-prune diff --git a/tasks/prune.yml b/tasks/prune.yml new file mode 100644 index 0000000..24230e7 --- /dev/null +++ b/tasks/prune.yml @@ -0,0 +1,12 @@ +--- +# Prune unused Podman resources + +- name: Prune Podman resources + containers.podman.podman_prune: + container: "{{ podman_prune_options.container }}" + image: "{{ podman_prune_options.image }}" + network: "{{ podman_prune_options.network }}" + system: "{{ podman_prune_options.system }}" + system_all: "{{ podman_prune_options.system_all }}" + volume: "{{ podman_prune_options.volume }}" + when: podman_prune_enabled | bool diff --git a/tasks/systemd.yml b/tasks/systemd.yml new file mode 100644 index 0000000..4d7b3f1 --- /dev/null +++ b/tasks/systemd.yml @@ -0,0 +1,50 @@ +--- +# Generate systemd service files for Podman containers and pods + +- name: Generate systemd service files for containers + containers.podman.podman_generate_systemd: + name: "{{ item.name }}" + dest: "{{ podman_systemd_dir }}" + new: "{{ podman_systemd_options.new }}" + force: "{{ podman_systemd_options.force }}" + restart_policy: "{{ item.systemd.restart_policy | default(podman_systemd_options.restart_policy) }}" + time: "{{ item.systemd.time | default(podman_systemd_options.time) }}" + no_header: "{{ item.systemd.no_header | default(podman_systemd_options.no_header) }}" + separator: "{{ item.systemd.separator | default(podman_systemd_options.separator) }}" + wants: "{{ item.systemd.wants | default(podman_systemd_options.wants) }}" + after: "{{ item.systemd.after | default(podman_systemd_options.after) }}" + requires: "{{ item.systemd.requires | default(podman_systemd_options.requires) }}" + container_prefix: "{{ item.systemd.container_prefix | default(podman_systemd_options.container_prefix) }}" + pod_prefix: "{{ item.systemd.pod_prefix | default(podman_systemd_options.pod_prefix) }}" + loop: "{{ podman_containers | selectattr('systemd', 'defined') | list }}" + loop_control: + label: "{{ item.name }}" + when: + - podman_generate_systemd | bool + - podman_containers is defined + - podman_containers | length > 0 + notify: Reload systemd + +- name: Generate systemd service files for pods + containers.podman.podman_generate_systemd: + name: "{{ item.name }}" + dest: "{{ podman_systemd_dir }}" + new: "{{ podman_systemd_options.new }}" + force: "{{ podman_systemd_options.force }}" + restart_policy: "{{ item.systemd.restart_policy | default(podman_systemd_options.restart_policy) }}" + time: "{{ item.systemd.time | default(podman_systemd_options.time) }}" + no_header: "{{ item.systemd.no_header | default(podman_systemd_options.no_header) }}" + separator: "{{ item.systemd.separator | default(podman_systemd_options.separator) }}" + wants: "{{ item.systemd.wants | default(podman_systemd_options.wants) }}" + after: "{{ item.systemd.after | default(podman_systemd_options.after) }}" + requires: "{{ item.systemd.requires | default(podman_systemd_options.requires) }}" + container_prefix: "{{ item.systemd.container_prefix | default(podman_systemd_options.container_prefix) }}" + pod_prefix: "{{ item.systemd.pod_prefix | default(podman_systemd_options.pod_prefix) }}" + loop: "{{ podman_pods | selectattr('systemd', 'defined') | list }}" + loop_control: + label: "{{ item.name }}" + when: + - podman_generate_systemd | bool + - podman_pods is defined + - podman_pods | length > 0 + notify: Reload systemd