0

I'm wondering whether there is a way to find out which service manager is used on the target system through Ansible facts. I can only find modules to gather facts about running services but not about the type of service manager.

I need to install some services on a system and in case there is systemd available, I would like to make use of systemd's dependency management and composition feature explicitly.

U880D
  • 8,601
  • 6
  • 24
  • 40
roehrijn
  • 1,387
  • 1
  • 11
  • 20
  • 1
    You can just [look for the directory `/run/systemd/system`](https://www.freedesktop.org/software/systemd/man/sd_booted.html). If it exists, the system was booted using systemd. – larsks Dec 10 '22 at 21:04

1 Answers1

1

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

U880D
  • 8,601
  • 6
  • 24
  • 40