containers.podman.podman_generate_systemd (1.12.1) — module

Generate systemd unit from a pod or a container

Authors: Sébastien Gendre (@CyberFox001)

Install collection

Install with ansible-galaxy collection install containers.podman:==1.12.1


Add to requirements.yml

  collections:
    - name: containers.podman
      version: 1.12.1

Description

Generate systemd .service unit file(s) from a pod or a container

Support Ansible check mode


Requirements

Usage examples

  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Example of creating a container and systemd unit file.
# When using podman_generate_systemd with new:true then
# the container needs rm:true for idempotence.
- name: Create postgres container
  containers.podman.podman_container:
    name: postgres
    image: docker.io/library/postgres:latest
    rm: true
    state: created
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Generate systemd unit file for postgres container
  containers.podman.podman_generate_systemd:
    name: postgres
    new: true
    no_header: true
    dest: /etc/systemd/system
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Ensure postgres container is started and enabled
  ansible.builtin.systemd:
    name: container-postgres
    daemon_reload: true
    state: started
    enabled: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

# Example of creating a container and integrate it into systemd
- name: A postgres container must exist, stopped
  containers.podman.podman_container:
    name: postgres_local
    image: docker.io/library/postgres:latest
    state: stopped
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Systemd unit files for postgres container must exist
  containers.podman.podman_generate_systemd:
    name: postgres_local
    dest: ~/.config/systemd/user/
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
- name: Postgres container must be started and enabled on systemd
  ansible.builtin.systemd:
    name: container-postgres_local
    scope: user
    daemon_reload: true
    state: started
    enabled: true
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.

# Generate the unit files, but store them on an Ansible variable
# instead of writing them on target host
- name: Systemd unit files for postgres container must be generated
  containers.podman.podman_generate_systemd:
    name: postgres_local
  register: postgres_local_systemd_unit
  • Success
    Steampunk Spotter scan finished with no errors, warnings or hints.
# Generate the unit files with environment variables sets
- name: Systemd unit files for postgres container must be generated
  containers.podman.podman_generate_systemd:
    name: postgres_local
    env:
      POSTGRES_USER: my_app
      POSTGRES_PASSWORD: example
  register: postgres_local_systemd_unit

Inputs

    
env:
    description:
    - Set environment variables to the systemd unit files.
    - Keys are the environment variable names, and values are the environment variable
      values
    - Only with Podman 4.3.0 and above
    type: dict

new:
    default: false
    description:
    - Generate unit files that create containers and pods, not only start them.
    - Refer to podman-generate-systemd(1) man page for more information.
    type: bool

dest:
    description:
    - Destination of the generated systemd unit file(s).
    - 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

name:
    description:
    - Name of the pod or container to export
    required: true
    type: str

after:
    description:
    - Add the systemd unit after (C(After=)) option, that ordering dependencies between
      the list of dependencies and this service.
    - This option may be specified more than once.
    - User-defined dependencies will be appended to the generated unit file
    - But any existing options such as needed or defined by default (e.g. C(online.target))
      will not be removed or overridden.
    - Only with Podman 4.0.0 and above
    elements: str
    type: list

force:
    default: false
    description:
    - Replace the systemd unit file(s) even if it already exists.
    - This works with dest option.
    type: bool

wants:
    description:
    - Add the systemd unit wants (C(Wants=)) option, that this service is (weak) dependent
      on.
    - This option may be specified more than once.
    - This option does not influence the order in which services are started or stopped.
    - User-defined dependencies will be appended to the generated unit file
    - But any existing options such as needed or defined by default (e.g. C(online.target))
      will not be removed or overridden.
    - Only with Podman 4.0.0 and above
    elements: str
    type: list

requires:
    description:
    - Set the systemd unit requires (Requires=) option.
    - Similar to wants, but declares a stronger requirement dependency.
    - Only with Podman 4.0.0 and above
    elements: str
    type: list

no_header:
    default: false
    description:
    - Do not generate the header including meta data such as the Podman version and the
      timestamp.
    type: bool

separator:
    description:
    - Systemd unit name separator between the name/id of a container/pod and the prefix.
    - If not set, use the default defined by podman, C(-).
    - Refer to podman-generate-systemd(1) man page for more information.
    type: str

use_names:
    default: true
    description:
    - Use name of the containers for the start, stop, and description in the unit file.
    type: bool

executable:
    default: podman
    description:
    - C(Podman) executable name or full path
    type: str

pod_prefix:
    description:
    - Set the systemd unit name prefix for pods.
    - If not set, use the default defined by podman, C(pod).
    - Refer to podman-generate-systemd(1) man page for more information.
    type: str

restart_sec:
    description:
    - Configures the time to sleep before restarting a service (as configured with restart-policy).
    - Takes a value in seconds.
    - Only with Podman 4.0.0 and above
    type: int

stop_timeout:
    description:
    - Override the default stop timeout for the container with the given value in seconds.
    type: int

start_timeout:
    description:
    - Override the default start timeout for the container with the given value in seconds.
    - Only with Podman 4.0.0 and above
    type: int

restart_policy:
    choices:
    - no-restart
    - on-success
    - on-failure
    - on-abnormal
    - on-watchdog
    - on-abort
    - always
    description:
    - Restart policy of the service
    type: str

container_prefix:
    description:
    - Set the systemd unit name prefix for containers.
    - If not set, use the default defined by podman, C(container).
    - Refer to podman-generate-systemd(1) man page for more information.
    type: str

Outputs

podman_command:
  description: A copy of the podman command used to generate the systemd unit(s)
  returned: always
  sample: podman generate systemd my_webapp
  type: str
systemd_units:
  description: A copy of the generated systemd .service unit(s)
  returned: always
  sample:
    container-postgres_local: ' #Content of the systemd .servec unit for postgres_local
      container'
    pod-my_webapp: ' #Content of the systemd .servec unit for my_webapp pod'
  type: dict