首页 > 解决方案 > Ansible playbook 检查服务是否启动 - 安装一些东西

问题描述

我需要在我的 linux 系统上安装 Symantec 端点安全,并且我正在尝试编写一个剧本来这样做

当我想安装程序时,我使用 ./install.sh -i 但是在安装之后,当我再次运行安装时,我得到了这个消息:

root@TestKubuntu:/usr/SEP# ./install.sh -i
Starting to install Symantec Endpoint Protection for Linux
Downgrade is not supported. Please make sure the target version is newer than the original one.

这就是我在剧本中安装它的方式

 - name: Install_SEP
     command: bash /usr/SEP/install.sh -i

我想是否可以检查服务是否已启动,如果没有服务则安装它,或者可能有更好的方法来执行此操作。

非常感谢您的宝贵时间

标签: ansibleansible-inventory

解决方案


问:“我想检查服务是否启动,如果没有服务则安装它。”

可以使用service_facts。例如检查服务是否正在运行

vars:
  my_service: "<name-of-my-service>"
tasks:
  - name: Collect service facts
    service_facts:
  - name: Install service when not running
    command: "<install-service>"
    when: "my_service not in ansible_facts.services|
                             dict2items|
                             json_query('[?value.state == `running`].key')"

要检查已安装的服务,请使用

                             json_query('[].key') }}" 

(未测试)


推荐阅读