ansiblepodmanrhel8

Generated podman systemd file can not be enabled


I'm trying to write an ansible playbook, which deploys nginx as a podman container, generates the systemd unit related to said container and enables & starts that systemd unit. The container itself is created fine and is up and running, but the system is unable to recognize the created unit file. The relevant part of the ansible playbook after the container is created is as follows

---
- name: Create systemd script for created container
  become: true
  shell: podman generate systemd -n nginx --files
  register: container_systemd_file

- name: Get service name
  set_fact: 
    service_name: "{{ container_systemd_file.stdout | basename }}"

- name: Move created systemd script to correct location
  become: true
  shell: mv {{ container_systemd_file.stdout }} /etc/systemd/system/

- name: Force systemd to reread configs
  become: true
  systemd: daemon_reload=yes

- name: Enable nginx container service
  become: true
  systemd:
    name: "{{ service_name }}"
    enabled: true
    state: started

The final step fails with message "msg": "Could not find the requested service container-nginx.service: host".

The unit file can be found on the host machine

$ ls -al /etc/systemd/system/container-nginx.service 
-rw-r--r--. 1 root root 701 Sep 24 15:19 /etc/systemd/system/container-nginx.service

and has contents

# container-nginx.service
# autogenerated by Podman 3.2.3
# Fri Sep 24 15:19:41 EEST 2021

[Unit]
Description=Podman container-nginx.service
Documentation=man:podman-generate-systemd(1)
Wants=network.target
After=network-online.target
RequiresMountsFor=/var/run/containers/storage

[Service]
Environment=PODMAN_SYSTEMD_UNIT=%n
Restart=on-failure
TimeoutStopSec=70
ExecStart=/usr/bin/podman start nginx
ExecStop=/usr/bin/podman stop -t 10 nginx
ExecStopPost=/usr/bin/podman stop -t 10 nginx
PIDFile=/var/run/containers/storage/overlay-containers/c5ad5d961913a8d48ea09557484eaf17749086581eb5a1750e65fb4cf8965272/userdata/conmon.pid
Type=forking

[Install]
WantedBy=multi-user.target default.target

Similarly if I run systemctl daemon-reload and try to enable the service manually on host, it fails with message Failed to enable unit: Unit file container-nginx.service does not exist.

How to make the system recognize the newly created unit file and enable the service? I'm using Podman 3.2.3 and RHEL 8.4.


Solution

  • I was unable to figure out why the initial approach didn't work, but instead of first generating the systemd unit as a file to user's home directory and then moving it, writing the file directly under /etc/systemd/system/ worked.

    ---
    - name: Create systemd script for created container
      become: true
      shell: podman generate systemd --new --name nginx > /etc/systemd/system/nginx.service
    
    - name: Force systemd to reread configs
      become: true
      systemd: daemon_reload=yes
    
    - name: Enable nginx container service
      become: true
      systemd:
        name: nginx.service
        enabled: true
        state: started