Ansible playbook 检查服务是否启动 - 安装一些东西

Ansible playbook check if service is up if not - install something

我需要在我的 linux 系统上安装 Symantec endpoint security,我正在尝试编写一个剧本来这样做

当我想安装我使用的程序时./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.

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

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

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

非常感谢您的宝贵时间

请尝试如下操作。

- name: Check if service is up
  command: <command to check if service is up>
  register: output

- name: Install_SEP
  command: bash /usr/SEP/install.sh -i  
  when: "'running' not in output.stdout"

注意:我在 when 条件下使用了 运行:如果服务命令 returns 有特定内容,请包含它而不是 运行。

Q: "I would like to check if the service is up and if there is no service then install it."

可以使用 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') }}" 

(未测试)