11

From the Ansible documentation, the service module:

Controls services on remote hosts. Supported init systems include BSD init, OpenRC, SysV, Solaris SMF, systemd, upstart.

For a given machine, how can I tell which init system Ansible is using? A system may have init and systemd on it, for example.

Kevin Burke
  • 61,194
  • 76
  • 188
  • 305

2 Answers2

22

The init system on a host is available as Ansible fact ansible_service_mgr.

Henrik Pingel
  • 3,083
  • 1
  • 19
  • 27
  • 1
    This is inconsistent between ansible versions. I have Ubuntu trusty systems where Ansible 2.2.1 reports "upstart" and Ansible 2.4 reports "service". – bschlueter Oct 24 '17 at 00:01
  • Great. Now the next newbie question...how to do the right ansible task based on this fact... – mlissner May 03 '18 at 05:31
  • 1
    You need to add an [when conditional](https://docs.ansible.com/ansible/latest/user_guide/playbooks_conditionals.html#the-when-statement) to the task like this: ′when: ansible_service_mgr == "systemd"′ – Henrik Pingel May 07 '18 at 07:09
  • 1
    Incorrect ansible_service_mgr issue is fixed: https://github.com/ansible/ansible/issues/30753 – Chih-Hsuan Yen Aug 26 '19 at 07:58
7

Let's look into the modules' code:

Inside def main()::

# Find service management tools
service.get_service_tools()

Then to class LinuxService(Service): and to def get_service_tools(self):

    # Locate a tool to enable/disable a service
    if check_systemd():
        # service is managed by systemd
        ...
    elif location.get('initctl', False) and os.path.exists("/etc/init/%s.conf" % self.name):
        # service is managed by upstart
        ...
    elif location.get('rc-service', False):
        # service is managed by OpenRC
        ...
    elif self.svc_initscript:
        # service is managed by with SysV init scripts
        ...

I've cut some code, but this snippet should answer your question: what system Ansible is likely to choose if there are many.

Systemd is the first one to search, then upstart, etc...

Konstantin Suvorov
  • 65,183
  • 9
  • 162
  • 193
  • 1
    I think it is not a good idea. On my Ubuntu 14 server, I have systemd installed but not used for init. The result is that Ansible tries to use Systemd instead of Upstart (the system declared service manager) – Vincent Vidal Aug 16 '17 at 13:04