cimon-io.systemd-service
Ansible Systemd Service Role
This Ansible role creates and sets up systemd service unit files. It helps run services automatically in the background, allows you to enable or disable them for specific events, manage groups of processes, and configure dependencies with other units.
What This Role Does
- Creates a systemd service file at /etc/systemd/system/with a specified nameservice_name.servicefor eachsystemd_service.
- Sets important options in the Unit, Service, and Install sections.
- Notifies systemd about new service files and restarts the service.
- Enables the service to run automatically on boot if needed.
Requirements
This role needs root access, so either run it in a playbook with become: yes, or use it in your playbook like this:
- hosts: apps
  roles:
    - role: systemd_service
      become: yes
Role Variables
You can set necessary services using the systemd_service dictionary variable (see defaults/main.yml):
systemd_service: {}
For each service, you need to set service_name and the required parameters. For example, to specify if the service should start on boot, use the enabled parameter.
systemd_service:
  service:
    service_name:
    enabled:
Here are additional parameters you can set for a specific service_name.
Unit Section Options
This section includes general information about the unit.
description:   # A description of the unit
The following parameters define dependencies on other units. If the service starts, these units will too. If any requires unit fails to start, the service will stop. For wants, the service continues running even if some units are inactive.
requires:   # Units that must start with the service
wants:      # Units that should start with the service
To set the order of starting or stopping services:
after:   # Units that must start after the service
before:  # Units that must start before the service
Service Section Options
This section provides information about the service and the process it oversees. The type parameter defines how the service starts.
type:
It can have values like:
- simple: Service starts immediately and does not branch.
- forking: Service starts once and the process forks.
- oneshot: Service exits before other units start.
- dbus: Daemon acquires a name on the- D-Bus.
- Other similar types with different behavior.
Use pid_file for the forking type.
pid_file:    # Path to the PID file for this daemon
You can set the user and group under which the service runs:
user:
group:
Set a scheduling priority for the unit:
nice:    # Priority level for the service
Commands and Restart Behavior
You can specify commands to run based on the service state, such as starting or stopping the service:
exec_start:         # Command when the service starts
exec_start_pre:    # Commands before starting
exec_start_post:   # Commands after starting
exec_stop:         # Command to stop the service
exec_stop_post:    # Commands after stopping
exec_reload:       # Command to reload the service configuration
Specify when the service should restart based on exit conditions:
restart:           # When to restart the service
You can also set time delays for these commands:
restart_sec:      # Delay before restarting the service
timeout_sec:      # Time to wait for start/stop commands
Set environment variables for the process:
environment:       # Dictionary of environment variables
environment_file:  # Path to a file with environment variables
Specify the working directory:
working_directory:
Control file descriptor connections:
standard_input:
standard_output:
standard_error:
If the service should be in a specific state after creation or modification:
state:             # Can be restarted, reloaded, started, or stopped
Install Section Options
This section defines installation information. Use these parameters to specify:
wanted_by:        # Unit names that want this service
required_by:      # Unit names that require this service
Example Playbook
- hosts: app
  roles:
    - role: systemd_service
      systemd_service:
        railsapp: 
          service_name: "railsapp"
          enabled: Yes
          exec_start: "/bin/bash -lc 'puma -C config/puma.rb'"
          working_directory: "/var/www/myapp"
          environment: {ENVVAR: value}
          user: "deploy"
          group: "deploy"
          restart: "on-failure"
          wants: "redis.service"
          requires: "postgresql.service"
          wanted_by: "multi-user.target"
License
Licensed under the MIT License.
ansible-galaxy install cimon-io.systemd-service