ansible.builtin.sysvinit module – Manage SysV services.

Note

This module is part of ansible-core and included in all Ansible installations. In most cases, you can use the short module name sysvinit even without specifying the collections keyword. However, we recommend you use the Fully Qualified Collection Name (FQCN) ansible.builtin.sysvinit for easy linking to the module documentation and to avoid conflicting with other collections that may have the same module name.

Synopsis

  • Controls services on target hosts that use the SysV init system.

Requirements

The below requirements are needed on the host that executes this module.

  • That the service managed has a corresponding init script.

Parameters

Parameter

Comments

arguments

aliases: args

string

Additional arguments provided on the command line that some init scripts accept.

daemonize

boolean

Have the module daemonize as the service itself might not do so properly.

This is useful with badly written init scripts or daemons, which commonly manifests as the task hanging as it is still holding the tty or the service dying when the task is over as the connection closes the session.

Choices:

  • false ← (default)

  • true

enabled

boolean

Whether the service should start on boot. At least one of state and enabled are required.

Choices:

  • false

  • true

name

aliases: service

string / required

Name of the service.

pattern

string

A substring to look for as would be found in the output of the ps command as a stand-in for a status result.

If the string is found, the service will be assumed to be running.

This option is mainly for use with init scripts that don’t support the status option.

runlevels

list / elements=string

The runlevels this script should be enabled/disabled from.

Use this to override the defaults set by the package or init script itself.

sleep

integer

If the service is being restarted or reloaded then sleep this many seconds between the stop and start command. This helps to workaround badly behaving services.

Default: 1

state

string

started/stopped are idempotent actions that will not run commands unless necessary. Not all init scripts support restarted nor reloaded natively, so these will both trigger a stop and start as needed.

Choices:

  • "started"

  • "stopped"

  • "restarted"

  • "reloaded"

Attributes

Attribute

Support

Description

check_mode

Support: full

Can run in check_mode and return changed status prediction without modifying target, if not supported the action will be skipped.

diff_mode

Support: none

Will return details on what has changed (or possibly needs changing in check_mode), when in diff mode

platform

Platform: posix

Target OS/families that can be operated against

Notes

Note

  • One option other than name is required.

  • The service names might vary by specific OS/distribution.

Examples

- name: Make sure apache2 is started
  ansible.builtin.sysvinit:
      name: apache2
      state: started
      enabled: yes

- name: Sleep for 5 seconds between stop and start command of badly behaving service
  ansible.builtin.sysvinit:
    name: apache2
    state: restarted
    sleep: 5

- name: Make sure apache2 is started on runlevels 3 and 5
  ansible.builtin.sysvinit:
      name: apache2
      state: started
      enabled: yes
      runlevels:
        - 3
        - 5

Return Values

Common return values are documented here, the following are the fields unique to this module:

Key

Description

results

complex

results from actions taken

Returned: always

name

string

Name of the service

Returned: always

Sample: "apache2"

status

dictionary

Status of the service

Returned: changed

Sample: {"enabled": {"changed": true, "rc": 0, "stderr": "", "stdout": ""}, "stopped": {"changed": true, "rc": 0, "stderr": "", "stdout": "Stopping web server: apache2.\n"}}

Authors

  • Ansible Core Team