Ansible:仅在存在时禁用服务 [英] Ansible: disable service only if present

查看:86
本文介绍了Ansible:仅在存在时禁用服务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么好方法可以禁用和停止服务,但前提是该服务必须安装在服务器上?像这样:

Is there any nice way to do disable and stop a service, but only if it's installed on server? Something like this:

- service: name={{ item }} enabled=no state=stopped only_if_present=yes
  with_items:
  - avahi-daemon
  - abrtd
  - abrt-ccpp

请注意,"only_if_present"是Ansible中目前不存在的关键字,但是我想我的目标很明显.

Note that "only_if_present" is a keyword that doesn't exist right now in Ansible, but I suppose my goal is obvious.

推荐答案

我不知道您所用的软件包名称是什么,但是您可以执行以下操作:

I don't know what is the package name in your case, but you can do something similar to this:

- shell: dpkg-query -W 'avahi'
  ignore_errors: True
  register: is_avahi
  when: ansible_distribution == 'Debian' or ansible_distribution == 'Ubuntu'

- shell: rpm -q 'avahi'
  ignore_errors: True
  register: is__avahi
  when: ansible_distribution == 'CentOS' or ansible_distribution == 'Red Hat Enterprise Linux'

- service: name=avahi-daemon enabled=no state=stopped
  when: is_avahi|failed

更新:我添加了条件,以便当您有多个不同的发行版时,剧本都可以使用,您可能需要对其进行调整以满足自己的要求.

Update: I have added conditions so that the playbook works when you have multiple different distros, you might need to adapt it to fit your requirements.

这篇关于Ansible:仅在存在时禁用服务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆