项目作者: vlcty

项目描述 :
Create systemd-timers with ansible
高级语言:
项目地址: git://github.com/vlcty/ansible-systemd-timers.git
创建时间: 2017-12-05T19:24:30Z
项目社区:https://github.com/vlcty/ansible-systemd-timers

开源协议:GNU Lesser General Public License v3.0

下载


ansible-systemd-timer

About

This roles enables you to create systemd timers which call scripts or execute commands.

Usage

Define a variable timers. This variable is a dictionary. Every key is a new timer.

Example

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.

  1. timers:
  2. 1337TelegramBot:
  3. timer_precommand: /bin/bash -c '! /usr/bin/systemctl is-active --quiet other-service.service'
  4. timer_command: /home/telegrambot/sendMessage.pl
  5. timer_user: telegrambot
  6. timer_OnCalendar: "*-*-* 13:37:00 CET"
  7. timer_AccuracySec: 5s

That’s all the magic.

Existing variables per timer

Variable Required Default value / Explanation
timer_precommand no Pre-command before command
timer_command yes Which command or script to execute
timer_envfile no Add environment file
timer_user no Under which users the timer_command is executed. Default: root
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
timer_workingdir no Set WorkingDirectory= for the timer
timer_OnActiveSec no Relative time after the timer unit was last activated
timer_OnBootSec no Relative time after the computer was booted
timer_OnStartupSec no Relative time after systemd was started
timer_OnUnitActiveSec no Relative time after the service unit was last activated
timer_OnUnitInactiveSec no Relative time after the service unit was last deactivated
timer_OnCalendar no Absolute time when to call activate the unit
timer_AccuracySec no Timer have a default accuracy of round about one minute. You can set the accuracy with this var. Default: 15s

You can chain every timer_On* variable. Example:

  1. timers:
  2. updateDNS:
  3. timer_command: /home/dnsupdate/updateMe.pl
  4. timer_user: dnsupdate
  5. timer_OnStartupSec: 20s
  6. timer_OnUnitActiveSec: 5m

The timer unit will be triggered 20 seconds after systemd was started and then every 5 minutes.

More about timers: https://www.freedesktop.org/software/systemd/man/systemd.timer.html

More about timespans: https://www.freedesktop.org/software/systemd/man/systemd.time.html

Existing variables globally, for the role

Variable Required Default value / Explanation
systemd_scope no Create system or user units. Default: system.
systemd_base_path no Where to generate the systemd unit files. Set this to e.g. ~/.config/systemd/user when using systemd_scope=user. Default: /etc/systemd/system.

You can create user timers for non-root services in combination with become_user: '{{ my_user }}'. Example:

  1. systemd_base_path: ~/.config/systemd/user
  2. systemd_scope: user
  3. timers:
  4. timer-one:
  5. timer_command: ...
  6. timer_OnCalendar: ...
  7. timer_user: '{{ my_user }}'
  8. ...

Working with shell redirection

Shell redirection does not work out of the box. You have to work around that by calling sh or bash.
This won’t work: echo hello > /var/log/hello.log
This will work: /usr/bin/bash -c \"echo hello > /var/log/hello.log\"

Tip: Always use full paths. To see where sh or bash is stored on your system you have to use which:

  1. [root@pizza ~]# which bash
  2. /usr/bin/bash