如何使用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
相关文章