![](/img/trans.png)
[英]How to gather facts after installing Python in one Ansible playbook?
[英]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.