I'm wondering whether there is a way to find out which service manager is used on the target system through Ansible facts.
Yes, it seems that Ansible Facts are providing already the information
I can only find modules to gather facts about running services but not about the type of service manager.
namely via the key:value "ansible_service_mgr": "systemd"
.
Another option could be the service_facts
module. According the Returned Facts the key
source
: Init system of the service. One of rcctl
, systemd
, sysv
, upstart
, src
. Returned: always
By checking the value of one of it you may be able to implement your conditional dependency management.
According fact naming options Why should I remove the ansible_
prefix when refering to an Ansible fact? it would lead to Conditionals based on ansible_facts
in example like
when: ansible_facts.service_mgr == "systemd"
Minimal Example
---
- hosts: localhost
become: false
gather_facts: true
gather_subset:
- "min"
tasks:
- name: Show Facts
debug:
msg: "{{ ansible_facts.service_mgr }}"
will result into an output of
TASK [Show Facts] ******
ok: [localhost] =>
msg: systemd
Further Q&A