This role manages the deployment and configuration of a custom systemd service.
Find a file
2025-10-08 12:13:32 +02:00
defaults add loop_var and defaults to template 2025-08-16 12:02:33 +02:00
handlers drop item not in 2025-10-08 12:13:32 +02:00
meta fix trailing spaces 2025-08-16 12:29:38 +02:00
tasks add loop_var and defaults to template 2025-08-16 12:02:33 +02:00
templates add EnvironmentFile 2025-10-03 16:53:14 +02:00
.gitignore add namespace 2025-06-21 11:34:38 +02:00
LICENSE Initial commit 2025-06-02 21:47:08 +02:00
README.md update readme 2025-10-08 11:56:11 +02:00

systemd_management Role

This role manages the deployment and configuration of custom systemd services and timers. It ensures the service and timer files are properly installed, systemd is reloaded, and the services and timers are enabled and started.

Description

The systemd_management role simplifies the process of managing custom systemd services and timers. It copies systemd service and timer file templates to the appropriate directory, reloads the systemd daemon, and ensures the services and timers are enabled and running. This role is ideal for deploying and scheduling custom services on Linux systems.

Installation

  1. Ensure that Ansible is installed on your system. The recommended version is at least 2.17.
  2. Clone this repository or add the role to your existing project.
  3. No additional dependencies are required.

Variables

The role uses the following variables to configure services and timers:

Service Variables

Variable Default Value Description
name required Name of the service file and the systemd unit
description required Description of the service (shown in [Unit] section)
working_directory required Working directory for the service process
exec_start required Command executed to start the service
exec_stop "" Command executed to stop the service
type simple Service type (e.g., simple, forking, oneshot)
user root User under which the service runs
group root Group under which the service runs
restart always Restart policy for the service
restart_sec 5 Seconds to wait before restarting after failure
environment "" Environment variables (e.g., VAR=value)
env_file "" Environment file path

Timer Variables

Variable Default Value Description
name required Name of the timer file and the systemd timer unit
description required Description of the timer (shown in [Unit] section)
timer_on_calendar daily When the timer triggers (e.g., daily, monthly, specific time)
timer_persistent false Whether missed events are triggered when the system is back online (Persistent=true)

Usage

Example Playbook

- name: Include systemd role
  ansible.builtin.include_role:
    name: systemd_management
  vars:
    systemd_management_services:
      - name: my_service
        description: 'My Custom Service'
        type: simple
        user: my_user
        group: my_group
        working_directory: /usr/bin/my_command
        exec_start: /usr/bin/my_command
        exec_stop: /usr/bin/my_command --stop
        restart: always
        restart_sec: 10
        environment: foo
        env_file: /foo/bar
    systemd_management_timers:
      - name: my_service
        description: Timer for My Custom Service
        timer_on_calendar: daily
        timer_persistent: true

Advanced Configuration

You can customize the service and timer file templates (my_service.service.j2 and my_service.timer.j2) to include additional directives or modify the behavior of the services and timers.

Features

  • Template-Based Service and Timer Deployment: Deploys systemd service and timer files based on customizable templates.
  • Systemd Integration: Reloads the systemd daemon after deploying the service and timer files.
  • Service and Timer Management: Ensures the services and timers are enabled and started.
  • Support for Multiple Services and Timers: Manage multiple services and their associated timers in a single playbook.
  • Customizable Variables: Allows you to define the service name, description, working directory, execution commands, and timer schedules.

Handlers

The role includes the following handlers:

  • Reload systemd: Reloads the systemd daemon to apply changes to service and timer files.
  • Enable and start service: Enables and starts the specified services.
  • Enable and start timer: Enables and starts the specified timers.

Contributing

Contributions are welcome! Please follow these steps:

  1. Fork the repository.
  2. Create a new branch for your changes.
  3. Submit a pull request describing your changes.

License

This project is licensed under the AGPL License. The AGPL license requires that modifications to the software be disclosed when distributed or made available over a network. For more information, see the LICENSE file.