如何使用Ansible攻略中的SERVICE_FACTS模块检查服务器中是否存在服务? [英] How to check service exists and is not installed in the server using service_facts module in an Ansible playbook?

查看:99
本文介绍了如何使用Ansible攻略中的SERVICE_FACTS模块检查服务器中是否存在服务?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已使用service_facts检查服务是否正在运行并已启用。在某些服务器中,未安装特定软件包。 现在,我如何使用service_facts module知道此特定软件包未安装在该特定服务器上?

在Ansible攻略中,它显示以下错误:

localhost | SUCCESS => {
    "ansible_facts": {
        "services": {
            "acpid.service": {
                "name": "acpid.service", 
                "source": "systemd", 
                "state": "running", 
                "status": "enabled"
            }, 
            "agent_installation.service": {
                "name": "agent_installation.service", 
                "source": "systemd", 
                "state": "stopped", 
                "status": "unknown"
            }, "changed": false, 
    "msg": "WARNING: Could not find status for all services. Sometimes this is due to insufficient privileges."
}

推荐答案

特定服务只有在存在且已正确注册的情况下才能在service_facts下可用。如果您想在不知道是否存在的服务上执行任务,Conditionals可能适合您。在示例中

- name: Gathering Service Facts
  service_facts:
  tags: remove,stop,disable

- name: Make sure {{ SERVICE }} is stopped and disabled
  systemd:
    name: {{ SERVICE }}
    state: stopped
    enabled: no
  when: ("{{ SERVICE }}" in services)
  tags: remove,stop,disable

- name: Make sure {{ SERVICE }} is removed
  yum:
    name: {{ SERVICE }}
    state: absent
  tags: remove

您可以这样做

- name: Set facts
  set_fact:
    SERVICE: "postgresql-10.service" for my working test environment
  tags: facts

- name: Gathering service facts
  service_facts:
  tags: facts

- name: Get facts
  debug:
    msg:
      - "{{ ansible_facts.services[SERVICE].status }}"
  tags: facts

这篇关于如何使用Ansible攻略中的SERVICE_FACTS模块检查服务器中是否存在服务?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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