繁体   English   中英

如何使用 Ansible 剧本中的 service_facts 模块检查服务是否存在且未安装在服务器中?

[英]How to check service exists and is not installed in the server using service_facts module in an Ansible playbook?

我已经使用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存在且正确注册时才能在service_facts下可用。 如果您喜欢在不知道是否存在的服务上执行任务, 条件语句可能适合您。 例如

- 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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM