Skip to content

Commit 8fd7cbf

Browse files
committed
Initial commit
1 parent 89a9af6 commit 8fd7cbf

File tree

4 files changed

+136
-0
lines changed

4 files changed

+136
-0
lines changed

README.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
# ansible-systemd-timer
2+
3+
## About
4+
This roles enables you to create systemd timers which call scripts or execute commands.
5+
6+
## Usage
7+
8+
Define a variable ```timers```. This variable is a dictionary. Every key is a new timer.
9+
10+
### Example
11+
12+
Here is an example for my 1337 Telegram Bot. The Timer "calls" a script which sends the message "It's now 13:37" in one of my Telegram chats every day at 13:37 GMT o'Clock.
13+
14+
```
15+
timers:
16+
1337TelegramBot:
17+
timer_command: /home/telegrambot/sendMessage.pl
18+
timer_user: telegrambot
19+
timer_OnCalendar: "*-*-* 13:37:00 CET"
20+
timer_AccuracySec: 5s
21+
```
22+
23+
That's all the magic.
24+
25+
### Existing variables
26+
27+
| Variable | Required | Default value / Explanation |
28+
|----------|----------|------------------------------|
29+
| timer_command | yes | Which command or script to execute |
30+
| timer_user | no | Under which users the timer_command is executed. Default: root |
31+
| timer_persistent | no | Takes a boolean argument. If true, the time when the service unit was last triggered is stored on disk. When the timer is activated, the service unit is triggered immediately if it would have been triggered at least once during the time when the timer was inactive. This is useful to catch up on missed runs of the service when the machine was off. Note that this setting only has an effect on timers configured with OnCalendar=. Defaults to false. [Source](https://www.freedesktop.org/software/systemd/man/systemd.timer.html) |
32+
| timer_OnActiveSec | no | Relative time after the timer unit was last activated |
33+
| timer_OnBootSec | no | Relative time after the computer was booted |
34+
| timer_OnStartupSec | no | Relative time after systemd was started |
35+
| timer_OnUnitActiveSec | no | Relative time after the service unit was last activated |
36+
| timer_OnUnitInactiveSec | no | Relative time after the service unit was last deactivated |
37+
| timer_OnCalendar | no | Absolute time when to call activate the unit |
38+
| timer_AccuracySec | no | Timer have a default accuracy of round about one minute. You can set the accuracy with this var. Default: 15s |
39+
40+
You can chain every timer_On* variable. Example:
41+
42+
```
43+
timers:
44+
updateDNS:
45+
timer_command: /home/dnsupdate/updateMe.pl
46+
timer_user: dnsupdate
47+
timer_OnStartupSec: 20s
48+
timer_OnUnitActiveSec: 5m
49+
```
50+
51+
The timer unit will be triggered 20 seconds after systemd was started and then every 5 minutes.
52+
53+
More about timers: https://www.freedesktop.org/software/systemd/man/systemd.timer.html
54+
55+
More about timespans: https://www.freedesktop.org/software/systemd/man/systemd.time.html

tasks/main.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
---
2+
- block:
3+
4+
- name: Failing when timer_command is undefined
5+
fail:
6+
msg: Varible timer_command is not defined
7+
when: item.value.timer_command is undefined
8+
with_dict: "{{ timers }}"
9+
10+
- name: Uploading service file
11+
template:
12+
src: service.j2
13+
dest: "/etc/systemd/system/{{ item.key }}.service"
14+
owner: root
15+
group: root
16+
mode: 0750
17+
with_dict: "{{ timers }}"
18+
19+
- name: Uploading timer file
20+
template:
21+
src: timer.j2
22+
dest: "/etc/systemd/system/{{ item.key }}.timer"
23+
owner: root
24+
group: root
25+
mode: 0750
26+
with_dict: "{{ timers }}"
27+
28+
- name: Enabling timers
29+
systemd:
30+
name: "{{ item.key }}.timer"
31+
state: started
32+
enabled: true
33+
masked: false
34+
daemon_reload: true
35+
with_dict: "{{ timers }}"
36+
37+
when: timers is defined

templates/service.j2

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
#
2+
# Managed by ansible
3+
# Role: systemd-timer
4+
#
5+
[Unit]
6+
Description={{ item.key }} Service
7+
8+
[Service]
9+
Type=oneshot
10+
ExecStart={{ item.value.timer_command }}
11+
{% if item.value.timer_user is defined %}
12+
User={{ item.value.timer_user }}
13+
{% endif %}

templates/timer.j2

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
#
2+
# Managed by ansible
3+
# Role: systemd-timer
4+
#
5+
[Unit]
6+
Description={{ item.key }} Timer
7+
8+
[Timer]
9+
{% if item.value.timer_OnActiveSec is defined %}
10+
OnActiveSec={{ item.value.timer_OnActiveSec }}
11+
{% endif %}
12+
{% if item.value.timer_OnBootSec is defined %}
13+
OnBootSec={{ item.value.timer_OnBootSec }}
14+
{% endif %}
15+
{% if item.value.timer_OnStartupSec is defined %}
16+
OnStartupSec={{ item.value.timer_OnStartupSec }}
17+
{% endif %}
18+
{% if item.value.timer_OnUnitActiveSec is defined %}
19+
OnUnitActiveSec={{ item.value.timer_OnUnitActiveSec }}
20+
{% endif %}
21+
{% if item.value.timer_OnUnitInactiveSec is defined %}
22+
OnUnitInactiveSec={{ item.value.timer_OnUnitInactiveSec }}
23+
{% endif %}
24+
{% if item.value.timer_OnCalendar is defined %}
25+
OnCalendar={{ item.value.timer_OnCalendar }}
26+
Persistent={{ item.value.timer_persistent | default('false') }}
27+
{% endif %}
28+
AccuracySec={{ item.value.timer_AccuracySec | default('15s') }}
29+
30+
[Install]
31+
WantedBy=timers.target

0 commit comments

Comments
 (0)