仅当安装了软件包时才禁用 systemd 服务 [英] Disable systemd service only if the package is installed
问题描述
这就是我所拥有的
- name: disable os firewall - firewalld
systemd:
name: firewalld
state: stopped
enabled: no
此任务在安装了 firewalld
软件包的主机上运行良好,但在未安装的主机上失败.
This task works fine on hosts where firewalld
package is installed but fails on hosts where it is not.
确保它可以处理未安装 firewalld
的主机的最佳简单方法是什么?我不想使用任何 CMDB,因为它是一个额外的设置.另外,我希望任务是幂等的;使用像 dpkg
或 rpm
这样的 shell 命令来查询是否安装了 firewalld
会使 ansible playbook 摘要报告发生变化,这是我不想要的.>
What is the best simple approach to make sure it can handle hosts which do not have firewalld
installed? I do not want to use any CMDB as it's an additional setup. Also, I want the task to be idempotent; using a shell command like dpkg
or rpm
to query if firewalld
is installed makes the ansible playbook summary report changes which I do not want.
推荐答案
以下是使用 failed_when:
的示例.虽然它每次都运行,但如果没有安装包,它会忽略失败:
Here is an example using failed_when:
. While it does run every time, it ignores the failure if the package is not installed:
- name: Stop and disable chrony service
service:
name: chronyd
enabled: no
state: stopped
register: chronyd_service_result
failed_when: "chronyd_service_result is failed and 'Could not find the requested service' not in chronyd_service_result.msg"
这篇关于仅当安装了软件包时才禁用 systemd 服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!