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

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

问题描述

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

- service: name={{ item }} enabled=no state=stopped only_if_present=yeswith_items:- avahi 守护进程- abrtd- abrt-ccpp

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

解决方案

我不知道你的情况下包名是什么,但你可以做类似的事情:

- shell: dpkg-query -W 'avahi'ignore_errors: 真注册:is_avahi时间: ansible_distribution == 'Debian' 或 ansible_distribution == 'Ubuntu'- 外壳:rpm -q 'avahi'ignore_errors: 真注册:is__avahi时间: ansible_distribution == 'CentOS' 或 ansible_distribution == 'Red Hat Enterprise Linux'- 服务:名称=avahi-daemon 启用=无状态=停止时间:is_avahi|失败

<块引用>

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

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

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天全站免登陆