ikke-t / podman-container-systemd

creates systemd files and creates containers using podman
118 stars 44 forks source link

podman-container-systemd

NOTE: While this hopefully still works, note that further development happens in the new linux system roles podman project. Try that, it is under active development. BR Ikke, thanks for all the contributors here throughout the years!

Role sets up container(s) to be run on host with help of systemd. Podman implements container events but does not control or keep track of the life-cycle. That's job of external tool as Kubernetes in clusters, and systemd in local installs.

I wrote this role in order to help managing podman containers life-cycle on my personal server which is not a cluster. Thus I want to use systemd for keeping them enabled and running over reboots.

What role does:

For reference, see these two blogs about the role:

Blogs describe how you can single container, or several containers as one pod using this module.

Note for running rootless containers:

Requirements

Requires system which is capable of running podman, and that podman is found from package repositories. Role installs podman. Role also installs firewalld if user has defined container_firewall_ports -variable. Installs kubeval for a pod if container_pod_yaml_template_validation: true.

Role Variables

Role uses variables that are required to be passed while including it. As there is option to run one container separately or multiple containers in pod, note that some options apply only to other method.

This playbook doesn't have python module to parse parameters for podman command. Until that you just need to pass all parameters as you would use podman from command line. See man podman or podman tutorials for info.

If you want your images to be automatically updated, add this label to container_cmd_args: --label "io.containers.autoupdate=image"

Never use ansible.builtin.import_role to execute this role if you intend to use it more than once per playbook, or you will fall in this anti-pattern.

Dependencies

Example Playbook

See the tests/main.yml for sample. In short, include role with vars.

Root container:

- name: tests container
  vars:
    container_image_list: 
      - sebp/lighttpd:latest
    container_name: lighttpd
    container_run_args: >-
      --rm
      -v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
      --label "io.containers.autoupdate=image"
      -p 8080:80
    #container_state: absent
    container_state: running
    container_firewall_ports:
      - 8080/tcp
      - 8443/tcp
  ansible.builtin.include_role:
    name: podman-container-systemd

Rootless container:

- name: ensure user
  user:
    name: rootless_user
    comment: I run sample container

- name: tests container
  vars:
    container_run_as_user: rootless_user
    container_run_as_group: rootless_user
    container_image_list: 
      - sebp/lighttpd:latest
    container_name: lighttpd
    container_run_args: >-
      --rm
      -v /tmp/podman-container-systemd:/var/www/localhost/htdocs:Z,U
      -p 8080:80
    #container_state: absent
    container_state: running
    container_firewall_ports:
      - 8080/tcp
      - 8443/tcp
  ansible.builtin.include_role:
    name: podman-container-systemd

Rootless Pod:

- name: ensure user
  user:
    name: rootless_user
    comment: I run sample container

- name: tests pod
  vars:
    container_run_as_user: rootless_user
    container_run_as_group: rootless_user
    container_image_list:
      - sebp/lighttpd:latest
    container_name: lighttpd-pod
    container_pod_yaml: /home/rootless_user/lighttpd-pod.yml
    container_pod_yaml_deploy: true
    container_pod_yaml_template_validation: true
    container_pod_labels:
      app: "{{ container_name }}"
      io.containers.autoupdate: 'image(1)'
    container_pod_volumes:
      - name: htdocs
        hostPath:
          path: /tmp/podman-container-systemd
          type: DirectoryOrCreate
    container_pod_containers:
      - name: lighttpd
        image: sebp/lighttpd:latest
        volumeMounts:
          - name: htdocs
            mountPath: /var/www/localhost/htdocs:Z
        ports:
          - containerPort: 80
            hostPort: 8080
    container_state: running
    container_firewall_ports:
      - 8080/tcp
      - 8443/tcp
  ansible.builtin.include_role:
    name: podman-container-systemd

License

GPLv3

Author Information

Ilkka Tengvall ilkka.tengvall@iki.fi