无法启用生成的 podman systemd 文件
Generated podman systemd file can not be enabled
我正在尝试编写一个 ansible 剧本,它将 nginx 部署为 podman 容器,生成与所述容器相关的 systemd 单元并启用并启动该 systemd 单元。容器本身创建良好并且正在 运行ning,但系统无法识别创建的单元文件。容器创建后ansible playbook相关部分如下
---
- 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
最后一步失败并显示消息 "msg": "Could not find the requested service container-nginx.service: host"
。
单元文件可以在主机上找到
$ 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
并且有内容
# 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
同样,如果我 运行 systemctl daemon-reload 并尝试在主机上手动启用该服务,它会失败并显示消息 Failed to enable unit: Unit file container-nginx.service does not exist.
如何让系统识别新创建的单元文件并启用服务?我正在使用 Podman 3.2.3 和 RHEL 8.4。
我无法弄清楚为什么最初的方法不起作用,而是首先将 systemd 单元作为文件生成到用户的主目录然后移动它,直接将文件写入 /etc/systemd/system/
成功了。
---
- 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
我正在尝试编写一个 ansible 剧本,它将 nginx 部署为 podman 容器,生成与所述容器相关的 systemd 单元并启用并启动该 systemd 单元。容器本身创建良好并且正在 运行ning,但系统无法识别创建的单元文件。容器创建后ansible playbook相关部分如下
---
- 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
最后一步失败并显示消息 "msg": "Could not find the requested service container-nginx.service: host"
。
单元文件可以在主机上找到
$ 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
并且有内容
# 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
同样,如果我 运行 systemctl daemon-reload 并尝试在主机上手动启用该服务,它会失败并显示消息 Failed to enable unit: Unit file container-nginx.service does not exist.
如何让系统识别新创建的单元文件并启用服务?我正在使用 Podman 3.2.3 和 RHEL 8.4。
我无法弄清楚为什么最初的方法不起作用,而是首先将 systemd 单元作为文件生成到用户的主目录然后移动它,直接将文件写入 /etc/systemd/system/
成功了。
---
- 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