TL;DR
What does a service status of not-found mean in Ansible's service facts?
More in detail:
With Ansible you can list all services on a host along with their states by:
ansible -m service_facts myhost
Example output:
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
}
So far so good, but what about this service:
"systemd-timesyncd.service": {
"name": "systemd-timesyncd.service",
"source": "systemd",
"state": "stopped",
"status": "not-found"
}
What does the status not-found mean?
The problem arising is that I have an Ansible playbook which would disable this service. To prevent any error if the service to be stopped does not exist at all, I added a check (see when attribute):
- 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
This playbook snippet would throw an error saying "service systemd-timesyncd.service not found". So, I realized that the check is not enough. Instead, I had to replace the following:
when: "item in services"
with:
when: "(item in services) and (services[item].status != 'not-found')"
But I would like to understand why this is the case. Why are non-existing services reported (i.e., included in Ansible's "service" facts) although they are reported as not-found?
Is this an Ansible "feature"? Or does this strange behavior come from the underlying systemd?
It seems that the Ansible
service_factsmodule – Return service state information as fact data only performs anas one could double check via the minimal example playbook
The reported service keys and states are (almost) the same.
If interested in which code exactly is executed or in more depth information and details, just have a look at the source of
ansible/modules/service_facts.py, line #253 and #281.Thanks To
That seems to be more a question for unix.stackexchange.com or superuser.com, like in Cleaning/Debugging services