├── LICENSE ├── README.md ├── handlers └── main.yml ├── meta └── main.yml ├── tasks └── main.yml └── templates ├── service.j2 └── timer.j2 /LICENSE: -------------------------------------------------------------------------------- 1 | GNU LESSER GENERAL PUBLIC LICENSE 2 | Version 3, 29 June 2007 3 | 4 | Copyright (C) 2007 Free Software Foundation, Inc. 5 | Everyone is permitted to copy and distribute verbatim copies 6 | of this license document, but changing it is not allowed. 7 | 8 | 9 | This version of the GNU Lesser General Public License incorporates 10 | the terms and conditions of version 3 of the GNU General Public 11 | License, supplemented by the additional permissions listed below. 12 | 13 | 0. Additional Definitions. 14 | 15 | As used herein, "this License" refers to version 3 of the GNU Lesser 16 | General Public License, and the "GNU GPL" refers to version 3 of the GNU 17 | General Public License. 18 | 19 | "The Library" refers to a covered work governed by this License, 20 | other than an Application or a Combined Work as defined below. 21 | 22 | An "Application" is any work that makes use of an interface provided 23 | by the Library, but which is not otherwise based on the Library. 24 | Defining a subclass of a class defined by the Library is deemed a mode 25 | of using an interface provided by the Library. 26 | 27 | A "Combined Work" is a work produced by combining or linking an 28 | Application with the Library. The particular version of the Library 29 | with which the Combined Work was made is also called the "Linked 30 | Version". 31 | 32 | The "Minimal Corresponding Source" for a Combined Work means the 33 | Corresponding Source for the Combined Work, excluding any source code 34 | for portions of the Combined Work that, considered in isolation, are 35 | based on the Application, and not on the Linked Version. 36 | 37 | The "Corresponding Application Code" for a Combined Work means the 38 | object code and/or source code for the Application, including any data 39 | and utility programs needed for reproducing the Combined Work from the 40 | Application, but excluding the System Libraries of the Combined Work. 41 | 42 | 1. Exception to Section 3 of the GNU GPL. 43 | 44 | You may convey a covered work under sections 3 and 4 of this License 45 | without being bound by section 3 of the GNU GPL. 46 | 47 | 2. Conveying Modified Versions. 48 | 49 | If you modify a copy of the Library, and, in your modifications, a 50 | facility refers to a function or data to be supplied by an Application 51 | that uses the facility (other than as an argument passed when the 52 | facility is invoked), then you may convey a copy of the modified 53 | version: 54 | 55 | a) under this License, provided that you make a good faith effort to 56 | ensure that, in the event an Application does not supply the 57 | function or data, the facility still operates, and performs 58 | whatever part of its purpose remains meaningful, or 59 | 60 | b) under the GNU GPL, with none of the additional permissions of 61 | this License applicable to that copy. 62 | 63 | 3. Object Code Incorporating Material from Library Header Files. 64 | 65 | The object code form of an Application may incorporate material from 66 | a header file that is part of the Library. You may convey such object 67 | code under terms of your choice, provided that, if the incorporated 68 | material is not limited to numerical parameters, data structure 69 | layouts and accessors, or small macros, inline functions and templates 70 | (ten or fewer lines in length), you do both of the following: 71 | 72 | a) Give prominent notice with each copy of the object code that the 73 | Library is used in it and that the Library and its use are 74 | covered by this License. 75 | 76 | b) Accompany the object code with a copy of the GNU GPL and this license 77 | document. 78 | 79 | 4. Combined Works. 80 | 81 | You may convey a Combined Work under terms of your choice that, 82 | taken together, effectively do not restrict modification of the 83 | portions of the Library contained in the Combined Work and reverse 84 | engineering for debugging such modifications, if you also do each of 85 | the following: 86 | 87 | a) Give prominent notice with each copy of the Combined Work that 88 | the Library is used in it and that the Library and its use are 89 | covered by this License. 90 | 91 | b) Accompany the Combined Work with a copy of the GNU GPL and this license 92 | document. 93 | 94 | c) For a Combined Work that displays copyright notices during 95 | execution, include the copyright notice for the Library among 96 | these notices, as well as a reference directing the user to the 97 | copies of the GNU GPL and this license document. 98 | 99 | d) Do one of the following: 100 | 101 | 0) Convey the Minimal Corresponding Source under the terms of this 102 | License, and the Corresponding Application Code in a form 103 | suitable for, and under terms that permit, the user to 104 | recombine or relink the Application with a modified version of 105 | the Linked Version to produce a modified Combined Work, in the 106 | manner specified by section 6 of the GNU GPL for conveying 107 | Corresponding Source. 108 | 109 | 1) Use a suitable shared library mechanism for linking with the 110 | Library. A suitable mechanism is one that (a) uses at run time 111 | a copy of the Library already present on the user's computer 112 | system, and (b) will operate properly with a modified version 113 | of the Library that is interface-compatible with the Linked 114 | Version. 115 | 116 | e) Provide Installation Information, but only if you would otherwise 117 | be required to provide such information under section 6 of the 118 | GNU GPL, and only to the extent that such information is 119 | necessary to install and execute a modified version of the 120 | Combined Work produced by recombining or relinking the 121 | Application with a modified version of the Linked Version. (If 122 | you use option 4d0, the Installation Information must accompany 123 | the Minimal Corresponding Source and Corresponding Application 124 | Code. If you use option 4d1, you must provide the Installation 125 | Information in the manner specified by section 6 of the GNU GPL 126 | for conveying Corresponding Source.) 127 | 128 | 5. Combined Libraries. 129 | 130 | You may place library facilities that are a work based on the 131 | Library side by side in a single library together with other library 132 | facilities that are not Applications and are not covered by this 133 | License, and convey such a combined library under terms of your 134 | choice, if you do both of the following: 135 | 136 | a) Accompany the combined library with a copy of the same work based 137 | on the Library, uncombined with any other library facilities, 138 | conveyed under the terms of this License. 139 | 140 | b) Give prominent notice with the combined library that part of it 141 | is a work based on the Library, and explaining where to find the 142 | accompanying uncombined form of the same work. 143 | 144 | 6. Revised Versions of the GNU Lesser General Public License. 145 | 146 | The Free Software Foundation may publish revised and/or new versions 147 | of the GNU Lesser General Public License from time to time. Such new 148 | versions will be similar in spirit to the present version, but may 149 | differ in detail to address new problems or concerns. 150 | 151 | Each version is given a distinguishing version number. If the 152 | Library as you received it specifies that a certain numbered version 153 | of the GNU Lesser General Public License "or any later version" 154 | applies to it, you have the option of following the terms and 155 | conditions either of that published version or of any later version 156 | published by the Free Software Foundation. If the Library as you 157 | received it does not specify a version number of the GNU Lesser 158 | General Public License, you may choose any version of the GNU Lesser 159 | General Public License ever published by the Free Software Foundation. 160 | 161 | If the Library as you received it specifies that a proxy can decide 162 | whether future versions of the GNU Lesser General Public License shall 163 | apply, that proxy's public statement of acceptance of any version is 164 | permanent authorization for you to choose that version for the 165 | Library. 166 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 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_precommand: /bin/bash -c '! /usr/bin/systemctl is-active --quiet other-service.service' 18 | timer_command: /home/telegrambot/sendMessage.pl 19 | timer_user: telegrambot 20 | timer_OnCalendar: "*-*-* 13:37:00 CET" 21 | timer_AccuracySec: 5s 22 | ``` 23 | 24 | That's all the magic. 25 | 26 | ### Existing variables per timer 27 | 28 | | Variable | Required | Default value / Explanation | 29 | |----------|----------|------------------------------| 30 | | timer_precommand | no | Pre-command before command | 31 | | timer_command | yes | Which command or script to execute | 32 | | timer_envfile | no | Add environment file | 33 | | timer_user | no | Under which users the timer_command is executed. Default: root | 34 | | 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) | 35 | | timer_workingdir | no | Set [WorkingDirectory=](https://www.freedesktop.org/software/systemd/man/systemd.exec.html#WorkingDirectory=) for the timer 36 | | timer_OnActiveSec | no | Relative time after the timer unit was last activated | 37 | | timer_OnBootSec | no | Relative time after the computer was booted | 38 | | timer_OnStartupSec | no | Relative time after systemd was started | 39 | | timer_OnUnitActiveSec | no | Relative time after the service unit was last activated | 40 | | timer_OnUnitInactiveSec | no | Relative time after the service unit was last deactivated | 41 | | timer_OnCalendar | no | Absolute time when to call activate the unit | 42 | | timer_AccuracySec | no | Timer have a default accuracy of round about one minute. You can set the accuracy with this var. Default: 15s | 43 | 44 | You can chain every timer_On* variable. Example: 45 | 46 | ``` 47 | timers: 48 | updateDNS: 49 | timer_command: /home/dnsupdate/updateMe.pl 50 | timer_user: dnsupdate 51 | timer_OnStartupSec: 20s 52 | timer_OnUnitActiveSec: 5m 53 | ``` 54 | 55 | The timer unit will be triggered 20 seconds after systemd was started and then every 5 minutes. 56 | 57 | More about timers: https://www.freedesktop.org/software/systemd/man/systemd.timer.html 58 | 59 | More about timespans: https://www.freedesktop.org/software/systemd/man/systemd.time.html 60 | 61 | ### Existing variables globally, for the role 62 | | Variable | Required | Default value / Explanation | 63 | |----------|----------|--------------------------------------------------------------------------------------------------------------------------------------------------------| 64 | | systemd_scope | no | Create system or user units. Default: `system`. | 65 | | 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`. | 66 | 67 | You can create user timers for non-root services in combination with `become_user: '{{ my_user }}'`. Example: 68 | ``` 69 | systemd_base_path: ~/.config/systemd/user 70 | systemd_scope: user 71 | timers: 72 | timer-one: 73 | timer_command: ... 74 | timer_OnCalendar: ... 75 | timer_user: '{{ my_user }}' 76 | ... 77 | ``` 78 | 79 | ## Working with shell redirection 80 | 81 | Shell redirection does not work out of the box. You have to work around that by calling `sh` or `bash`. 82 | This won't work: `echo hello > /var/log/hello.log` 83 | This will work: `/usr/bin/bash -c \"echo hello > /var/log/hello.log\"` 84 | 85 | Tip: Always use full paths. To see where `sh` or `bash` is stored on your system you have to use `which`: 86 | 87 | ``` 88 | [root@pizza ~]# which bash 89 | /usr/bin/bash 90 | ``` 91 | -------------------------------------------------------------------------------- /handlers/main.yml: -------------------------------------------------------------------------------- 1 | - name: Reload systemd 2 | systemd: 3 | daemon_reload: true 4 | -------------------------------------------------------------------------------- /meta/main.yml: -------------------------------------------------------------------------------- 1 | --- 2 | galaxy_info: 3 | author: Josef 'veloc1ty' Stautner 4 | description: Creates systemd timers 5 | company: veloc1ty networks 6 | # If the issue tracker for your role is not on github, uncomment the 7 | # next line and provide a value 8 | # issue_tracker_url: http://example.com/issue/tracker 9 | # Some suggested licenses: 10 | # - BSD (default) 11 | # - MIT 12 | # - GPLv2 13 | # - GPLv3 14 | # - Apache 15 | # - CC-BY 16 | license: MIT 17 | min_ansible_version: 2.4 18 | # 19 | # Below are all platforms currently available. Just uncomment 20 | # the ones that apply to your role. If you don't see your 21 | # platform on this list, let us know and we'll get it added! 22 | # 23 | platforms: 24 | - name: Archlinux 25 | versions: 26 | - all 27 | # Below are all categories currently available. Just as with 28 | # the platforms above, uncomment those that apply to your role. 29 | # 30 | categories: 31 | #- backup 32 | #- cloud 33 | #- cloud:ec2 34 | #- cloud:gce 35 | #- cloud:rax 36 | #- clustering 37 | #- database 38 | #- database:nosql 39 | #- database:sql 40 | #- development 41 | #- monitoring 42 | #- networking 43 | #- packaging 44 | - system 45 | - systemd-timers 46 | #- web 47 | dependencies: [] 48 | # List your role dependencies here, one per line. 49 | # Be sure to remove the '[]' above if you add dependencies 50 | # to this list. 51 | -------------------------------------------------------------------------------- /tasks/main.yml: -------------------------------------------------------------------------------- 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: "{{ systemd_base_path | default('/etc/systemd/system') }}/{{ item.key }}.service" 14 | owner: "{{ item.value.timer_user | default('root') }}" 15 | group: "{{ item.value.timer_user | default('root') }}" 16 | mode: 0644 17 | with_dict: "{{ timers }}" 18 | notify: Reload systemd 19 | 20 | - name: Uploading timer file 21 | template: 22 | src: timer.j2 23 | dest: "{{ systemd_base_path | default('/etc/systemd/system') }}/{{ item.key }}.timer" 24 | owner: "{{ item.value.timer_user | default('root') }}" 25 | group: "{{ item.value.timer_user | default('root') }}" 26 | mode: 0644 27 | with_dict: "{{ timers }}" 28 | notify: Reload systemd 29 | 30 | - name: Enabling timers 31 | systemd: 32 | name: "{{ item.key }}.timer" 33 | state: restarted 34 | enabled: true 35 | masked: false 36 | daemon_reload: true 37 | scope: "{{ systemd_scope | default('system') }}" 38 | with_dict: "{{ timers }}" 39 | 40 | when: timers is defined 41 | -------------------------------------------------------------------------------- /templates/service.j2: -------------------------------------------------------------------------------- 1 | # 2 | # Managed by ansible 3 | # Role: systemd-timer 4 | # 5 | [Unit] 6 | Description={{ item.key }} Service 7 | 8 | [Service] 9 | Type=oneshot 10 | {% if item.value.timer_envfile is defined %} 11 | EnvironmentFile={{ item.value.timer_envfile }} 12 | {% endif %} 13 | {% if item.value.timer_precommand is defined %} 14 | ExecStartPre={{ item.value.timer_precommand }} 15 | {% endif %} 16 | ExecStart={{ item.value.timer_command }} 17 | {% if item.value.timer_user is defined and (systemd_scope is not defined or systemd_scope != 'user') %} 18 | User={{ item.value.timer_user }} 19 | {% endif %} 20 | {% if item.value.timer_workingdir is defined %} 21 | WorkingDirectory={{ item.value.timer_workingdir }} 22 | {% endif %} 23 | -------------------------------------------------------------------------------- /templates/timer.j2: -------------------------------------------------------------------------------- 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 32 | --------------------------------------------------------------------------------