TL;博士
Ansible 服务事实中的服务状态
not-found
意味着什么?
更详细:
使用 Ansible,您可以通过以下方式列出主机上的所有服务及其状态:
ansible -m service_facts myhost
输出示例:
myhost | SUCCESS => {
"ansible_facts": {
"services": {
"ModemManager.service": {
"name": "ModemManager.service",
"source": "systemd",
"state": "running",
"status": "enabled"
},
"NetworkManager-dispatcher.service": {
"name": "NetworkManager-dispatcher.service",
"source": "systemd",
"state": "inactive",
"status": "enabled"
},
...
}
}
},
"changed": false
}
到目前为止还不错,但是这项服务怎么样:
"systemd-timesyncd.service": {
"name": "systemd-timesyncd.service",
"source": "systemd",
"state": "stopped",
"status": "not-found"
}
状态
not-found
是什么意思?
出现的问题是我有一个 Ansible 剧本,它会禁用此服务。为了防止在要停止的服务根本不存在时出现任何错误,我添加了一个检查(请参阅
when
属性):
- name: Populate service facts
ansible.builtin.service_facts: ~
- name: Disable systemd-timesyncd service (replaced with chrony as NTP server)
ansible.builtin.systemd:
name: "{{ item }}"
state: stopped
enabled: false
when: "item in services"
with_items:
- systemd-timesyncd.service
become: true
这个剧本片段会抛出一个错误,提示“service systemd-timesyncd.service not found”。所以,我意识到支票是不够的。相反,我必须更换以下内容:
when: "item in services"
与:
when: "(item in services) and (services[item].status != 'not-found')"
但我想了解为什么会这样。为什么不存在的服务会被报告(即包含在 Ansible 的“服务”事实中),尽管它们被报告为
not-found
?
这是 Ansible 的“功能”吗?或者这种奇怪的行为来自于当时的潜在
systemd
?
这是 Ansible 的“功能”吗?
据我所知。
或者这...来自当时的底层
?systemd
service_facts
模块 – 将服务状态信息作为事实数据返回几乎执行了
systemctl --full --type service --all
systemctl --full --type service --all | grep not-found
● display-manager.service not-found inactive dead display-manager.service
● sntp.service not-found inactive dead sntp.service
● syslog.service not-found inactive dead syslog.service
● systemd-sysusers.service not-found inactive dead systemd-sysusers.service
● ypbind.service not-found inactive dead ypbind.service
● yppasswdd.service not-found inactive dead yppasswdd.service
● ypserv.service not-found inactive dead ypserv.service
● ypxfrd.service not-found inactive dead ypxfrd.service
因为人们可以通过最小的示例剧本来仔细检查
---
- hosts: localhost
become: false
gather_facts: false
tasks:
- service_facts:
- debug:
var: (ansible_facts.services).keys() | sort
报告的服务密钥和状态(几乎)相同。
如果对到底执行了哪些代码或更深入的信息和细节感兴趣,只需查看
ansible/modules/service_facts.py
的源代码
感谢
状态
是什么意思?not-found
这似乎是 unix.stackexchange.com 的问题。