Ansible role for configuring a Systemd timer and service
Go to file
Jakub Sokołowski 50575b23eb
service: drop RestartMode, added in Systemd 254
Long way away, Jammy is on 249.

Signed-off-by: Jakub Sokołowski <jakub@status.im>
2024-03-15 13:51:37 +01:00
defaults service: drop RestartMode, added in Systemd 254 2024-03-15 13:51:37 +01:00
meta meta: use full names of Ansible roles 2024-03-10 13:41:03 +01:00
tasks check: fix incorrect name of service 2024-03-10 14:24:38 +01:00
templates service: drop RestartMode, added in Systemd 254 2024-03-15 13:51:37 +01:00
LICENSE add MIT license 2020-02-17 13:39:33 +01:00
README.md add option to use DynamicUser=yes 2022-08-02 15:08:21 +02:00

README.md

Description

This role configures a systemd timer along with a oneshot service for running a provided script.

Configuration

systemd_timer_name: my-test
systemd_timer_description: 'Just a timer creation test.'
systemd_timer_user: 'root'
systemd_timer_frequency: 'hourly'
systemd_timer_timeout_sec: 120
systemd_timer_nice_priority: 10
systemd_timer_requires_extra: 'network.target'
systemd_timer_after_extra: 'network.target'
systemd_timer_work_dir: '/tmp/somedir'
systemd_timer_environment:
  MY_ENV_VAR: 'some_value'
systemd_timer_script_content: |
  #!/usr/bin/env bash
  echo "My Timer Script!"  

The frequency accepts systemd time specification format.

Service User

By default nobody is used, but a dynamic user is a more secure option:

systemd_timer_dynamic_user: yes

State Directory

A state directory in /var/lib can be created using:

systemd_timer_state_dir_create: false
systemd_timer_state_dir: '{{ systemd_timer_name }}'
systemd_timer_state_dir_perms: 0750

Which will be named same as the timer itself.

Automatic Restarts

Automatic restarts with limited retries can also be configured:

systemd_timer_restart: 'on-failure'
systemd_timer_restart_delay: 60
systemd_timer_restart_retries: 5

Consul Metadata

You can also customize the Consul service definition:

systemd_timer_consul_service_id: 'my-test-abc'
systemd_timer_consul_service_name: 'my-test'
systemd_timer_consul_extra_tags: ['test', 'abc']
systemd_timer_consul_meta: { my_meta: 'metadata' }

The service runs the check.sh script to verify timer health..

Usage

The the timer starts the service with configured frequency. Assuming our backup target is called database you can do:

 $ sudo systemctl status my-test.service
● my-test.service - Just a timer creation test.
   Loaded: loaded (/lib/systemd/system/my-test.service; static; vendor preset: enabled)
   Active: inactive (dead) since Mon 2020-02-17 13:14:53 UTC; 6s ago
     Docs: https://github.com/status-im/infra-role-systemd-timer
  Process: 26001 ExecStart=/usr/local/bin/my-test (code=exited, status=0/SUCCESS)
 Main PID: 26001 (code=exited, status=0/SUCCESS)

Feb 17 13:14:53 host.example.org systemd[1]: Starting Just a timer creation test....
Feb 17 13:14:53 host.example.org systemd[1]: Started Just a timer creation test..
Feb 17 13:14:53 host.example.org my-test[26001]: My Timer Script!

You can check the timer status too:

 $ sudo systemctl list-timers my-test.timer     
NEXT                         LEFT       LAST PASSED UNIT          ACTIVATES
Mon 2020-02-17 14:00:00 UTC  44min left n/a  n/a    my-test.timer my-test.service