| defaults | ||
| handlers | ||
| meta | ||
| tasks | ||
| templates | ||
| .gitignore | ||
| LICENSE | ||
| README.md | ||
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
- Ensure that Ansible is installed on your system. The recommended version is at least
2.17. - Clone this repository or add the role to your existing project.
- 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:
- Fork the repository.
- Create a new branch for your changes.
- 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.