使用Ansible停止可能不存在的服务 [英] Using Ansible to stop service that might not exist

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

问题描述

我正在使用Ansible 2.6.1.

I am using Ansible 2.6.1.

我正在尝试确保某些服务未在目标主机上运行. 问题在于某些主机上可能根本不存在该服务.在这种情况下,Ansible由于缺少服务而失败,并显示错误.服务由Systemd运行.

I am trying to ensure that certain service is not running on target hosts. Problem is that the service might not exist at all on some hosts. If this is the case Ansible fails with error because of missing service. Services are run by Systemd.

使用服务模块:

  - name: Stop service
    service:
      name: '{{ target_service }}'
      state: stopped

失败,错误Could not find the requested service SERVICE: host

尝试使用命令模块:

 - name: Stop service
   command: service {{ target_service }} stop

给出错误:Failed to stop SERVICE.service: Unit SERVICE.service not loaded.

我知道我可以使用ignore_errors: yes,但是它也可能隐藏真正的错误.

I know I could use ignore_errors: yes but it might hide real errors too.

另一个解决方案是执行2个任务.一种检查服务是否存在,另一种则仅在第一个任务发现有服务但感觉很复杂时才运行.

An other solution would be having 2 tasks. One checking for existance of service and other that is run only when first task found service but feels complex.

是否有更简单的方法来确保服务停止并避免服务不存在的错误?

Is there simpler way to ensure that service is stopped and avoid errors if the service does not exists?

推荐答案

恕我直言,没有简单的方法来确保服务已停止. Ansible service 模块不检查服务的存在. (1)多于一个任务,或者(2)需要检查服务是否存在的命令.该命令将特定于操作系统.例如FreeBSD

IMHO there isn't simpler way to ensure that service is stopped. Ansible service module doesn't check service's existence. Either (1) more then one task, or (2) command that check service's existence is needed. The command would be OS specific. For example for FreeBSD

command: "service -e | grep {{ target_service }} && service {{ target_service }} stop"

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

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