devoinc.systemd_service
Ansible Systemd Service Role
This Ansible role sets up and configures systemd service unit
files. It helps you run services automatically in the background, manage when they're enabled or disabled, handle groups of processes, and set up dependencies on other services.
Key Tasks
- Creates a systemd service file in
/etc/systemd/system/
namedservice_name.service
for each item insystemd_service
. - Configures important options in the Unit, Service, and Install sections.
- Informs systemd about new service files and restarts the service.
- Enables the service to start automatically on boot if needed.
This role works with all versions of Ubuntu.
Requirements
To run this role, you need root access. You can either run it in a playbook with a global become: yes
option, or include it in your playbook like this:
- hosts: apps
roles:
- role: systemd-service
become: yes
Role Variables
You can define services using the systemd_service
dictionary variable (see defaults/main.yml
):
systemd_service: {}
For each service, you need to specify service_name
and other necessary values. For example, to set the service to start on boot, use the enabled
parameter:
systemd_service:
service_name:
enabled:
Unit Section Options
This group includes general information about the unit.
description: # A description of the unit
The following parameters specify dependencies on other units. If the service starts, the listed units will start too. If a unit from requires
fails, the service will also stop. If a unit from wants
fails, the service will continue running. Use space-separated unit names for these parameters:
requires: # Units that must start with the service
wants: # Units that should start with the service
To set the order for starting or stopping services, use these parameters:
after: # Units to start after this service
before: # Units to start before this service
Service Section Options
This section includes details about the service and process it supervises. The type
parameter specifies how the process starts.
type:
Possible values:
simple
- service starts immediately and doesn't create child processes.forking
- service starts once, then forks a new process. This requires apid_file
parameter to identify the main process.
Other values include:
oneshot
,dbus
,notify
, andidle
, each with specific behaviors.
Set a path for the PID file if using the forking
type:
pid_file: # Path to the service's PID file
You can specify the user and group under which the service runs:
user:
group:
Set a scheduling priority:
nice: # The service's priority level
The oom_score_adjust
parameter adjusts memory killer behavior:
oom_score_adjust:
You can define commands to execute based on the service's state, using parameters for start, stop, and reload actions:
exec_start:
exec_start_pre:
exec_start_post:
exec_stop:
exec_stop_post:
exec_reload:
Control whether the service restarts based on specific conditions with the restart
parameter:
restart:
Set delays for restart and timeout with parameters:
restart_sec:
timeout_sec:
Use the environment
parameter for environment variables:
environment: # List of environment variables
environment_file: # Path to a file with environment variables
Specify the working directory:
working_directory:
Control where standard input and output connect:
standard_input:
standard_output:
standard_error:
Install Section Options
This section carries installation information for the unit. These parameters can be used to define which units refer to this service.
wanted_by:
required_by:
Dependencies
None.
Example Playbook
- hosts: app
roles:
- role: systemd-service
systemd_service:
# Service name
railsapp:
# Start the service on boot
enabled: Yes
# Command to run when starting the service
exec_start: "/bin/bash -lc 'puma -C config/puma.rb'"
# Current working directory
working_directory: "/var/www/myapp"
# User and group to run the service
user: "deploy"
group: "deploy"
# Restart settings for the service
restart: "on-failure"
# Activate 'redis' if possible
wants: "redis.service"
# Activate 'postgresql' or stop if failed
requires: "postgresql.service"
# Specify preferred unit to run the service
wanted_by: "multi-user.target"
License
Licensed under the MIT License.