Debian Codegraph @

test/integration/targets/cron/tasks/main.yml

raw
1 - name: Include distribution specific variables
2 include_vars: "{{ lookup('first_found', search) }}"
3 vars:
4 search:
5 files:
6 - '{{ ansible_distribution | lower }}.yml'
7 - '{{ ansible_os_family | lower }}.yml'
8 - '{{ ansible_system | lower }}.yml'
9 - default.yml
10 paths:
11 - vars
12
13 - name: add cron task (check mode enabled, cron task not already created)
14 cron:
15 name: test cron task
16 job: 'date > {{ remote_dir }}/cron_canary1'
17 check_mode: yes
18 register: check_mode_enabled_state_present
19
20 - assert:
21 that: check_mode_enabled_state_present is changed
22
23 - name: add cron task (check mode disabled, task hasn't already been created)
24 cron:
25 name: test cron task
26 job: 'date > {{ remote_dir }}/cron_canary1'
27 register: add_cron_task
28
29 - assert:
30 that: add_cron_task is changed
31
32 - name: add cron task (check mode enabled, cron task already exists)
33 cron:
34 name: test cron task
35 job: 'date > {{ remote_dir }}/cron_canary1'
36 check_mode: yes
37 register: check_mode_enabled_state_present_cron_task_already_exists
38
39 - assert:
40 that: check_mode_enabled_state_present_cron_task_already_exists is not changed
41
42 - name: add cron task (check mode disabled, cron task already created)
43 cron:
44 name: test cron task
45 job: 'date > {{ remote_dir }}/cron_canary1'
46 register: cron_task_already_created
47
48 - assert:
49 that: cron_task_already_created is not changed
50
51 - block:
52 - name: wait for canary creation
53 wait_for:
54 path: '{{ remote_dir }}/cron_canary1'
55 timeout: '{{ 20 if faketime_pkg else 70 }}'
56 register: wait_canary
57 always:
58 - name: display some logs in case of failure
59 command: 'journalctl -u {{ cron_service }}'
60 when: wait_canary is failed and ansible_service_mgr == 'systemd'
61
62 - debug:
63 msg: 'elapsed time waiting for canary: {{ wait_canary.elapsed }}'
64
65 - name: Check check_mode
66 cron:
67 name: test cron task
68 job: 'date > {{ remote_dir }}/cron_canary1'
69 state: absent
70 check_mode: yes
71 register: check_check_mode
72
73 - assert:
74 that: check_check_mode is changed
75
76 - name: Remove a cron task
77 cron:
78 name: test cron task
79 job: 'date > {{ remote_dir }}/cron_canary1'
80 state: absent
81 register: remove_task
82
83 - assert:
84 that: remove_task is changed
85
86 - name: 'cron task missing: check idempotence (check mode enabled, state=absent)'
87 cron:
88 name: test cron task
89 job: 'date > {{ remote_dir }}/cron_canary1'
90 state: absent
91 register: check_mode_enabled_remove_task_idempotence
92
93 - assert:
94 that: check_mode_enabled_remove_task_idempotence is not changed
95
96 - name: 'cron task missing: check idempotence (check mode disabled, state=absent)'
97 cron:
98 name: test cron task
99 job: 'date > {{ remote_dir }}/cron_canary1'
100 state: absent
101 register: remove_task_idempotence
102
103 - assert:
104 that: remove_task_idempotence is not changed
105
106 - name: Check that removing a cron task with cron_file and without specifying a user is allowed (#58493)
107 cron:
108 name: test cron task
109 cron_file: unexistent_cron_file
110 state: absent
111 register: remove_cron_file
112
113 - assert:
114 that: remove_cron_file is not changed
115
116 - name: Non regression test - cron file should not be empty after adding var (#71207)
117 when: ansible_distribution != 'Alpine'
118 block:
119 - name: Cron file creation
120 cron:
121 cron_file: cron_filename
122 name: "simple cron job"
123 job: 'echo "_o/"'
124 user: root
125
126 - name: Add var to the cron file
127 cron:
128 cron_file: cron_filename
129 env: yes
130 name: FOO
131 value: bar
132 user: root
133
134 - name: "Ensure cron_file still contains job string"
135 replace:
136 path: /etc/cron.d/cron_filename
137 regexp: "_o/"
138 replace: "OK"
139 register: find_chars
140 failed_when: (find_chars is not changed) or (find_chars is failed)
141
142 # BusyBox does not have /etc/cron.d
143 - name: Removing a cron file when the name is specified is allowed (#57471)
144 when: ansible_distribution != 'Alpine'
145 block:
146 - name: Check file does not exist
147 stat:
148 path: /etc/cron.d/cron_remove_name
149 register: cron_file_stats
150
151 - assert:
152 that: not cron_file_stats.stat.exists
153
154 - name: Cron file creation
155 cron:
156 cron_file: cron_remove_name
157 name: "integration test cron"
158 job: 'ls'
159 user: root
160
161 - name: Cron file deletion
162 cron:
163 cron_file: cron_remove_name
164 name: "integration test cron"
165 state: absent
166
167 - name: Check file successful deletion
168 stat:
169 path: /etc/cron.d/cron_remove_name
170 register: cron_file_stats
171
172 - assert:
173 that: not cron_file_stats.stat.exists
174
175 # BusyBox does not have /etc/cron.d
176 - name: Removing a cron file, which contains only whitespace
177 when: ansible_distribution != 'Alpine'
178 block:
179 - name: Check file does not exist
180 stat:
181 path: /etc/cron.d/cron_remove_whitespace
182 register: cron_file_stats
183
184 - assert:
185 that: not cron_file_stats.stat.exists
186
187 - name: Cron file creation
188 cron:
189 cron_file: cron_remove_whitespace
190 name: "integration test cron"
191 job: 'ls'
192 user: root
193
194 - name: Add whitespace to cron file
195 shell: 'printf "\n \n\t\n" >> /etc/cron.d/cron_remove_whitespace'
196
197 - name: Cron file deletion
198 cron:
199 cron_file: cron_remove_whitespace
200 name: "integration test cron"
201 state: absent
202
203 - name: Check file successful deletion
204 stat:
205 path: /etc/cron.d/cron_remove_whitespace
206 register: cron_file_stats
207
208 - assert:
209 that: not cron_file_stats.stat.exists
210
211 - name: System cron tab can not be managed
212 when: ansible_distribution != 'Alpine'
213 block:
214 - name: Add cron job
215 cron:
216 cron_file: "{{ system_crontab }}"
217 user: root
218 name: "integration test cron"
219 job: 'ls'
220 ignore_errors: yes
221 register: result
222
223 - assert:
224 that: "result.msg == 'Will not manage /etc/crontab via cron_file, see documentation.'"
225
226 # TODO: restrict other root crontab locations
227 - name: System cron tab does not get removed
228 when: ansible_distribution == 'Alpine'
229 block:
230 - name: Add cron job
231 cron:
232 cron_file: "{{ system_crontab }}"
233 user: root
234 name: "integration test cron"
235 job: 'ls'
236
237 - name: Remove cron job
238 cron:
239 cron_file: "{{ system_crontab }}"
240 name: "integration test cron"
241 state: absent
242
243 - name: Check system crontab still exists
244 stat:
245 path: "{{ system_crontab }}"
246 register: cron_file_stats
247
248 - assert:
249 that: cron_file_stats.stat.exists
250
251 - name: Allow non-ascii chars in job (#69492)
252 when: ansible_distribution != 'Alpine'
253 block:
254 - name: Check file does not exist
255 stat:
256 path: /etc/cron.d/cron_nonascii
257 register: cron_file_stats
258
259 - assert:
260 that: not cron_file_stats.stat.exists
261
262 - name: Cron file creation
263 cron:
264 cron_file: cron_nonascii
265 name: "cron job that contain non-ascii chars in job (これは日本語です; This is Japanese)"
266 job: 'echo "うどんは好きだがお化け👻は苦手である。"'
267 user: root
268
269 - name: "Ensure cron_file contains job string"
270 replace:
271 path: /etc/cron.d/cron_nonascii
272 regexp: "うどんは好きだがお化け👻は苦手である。"
273 replace: "それは機密情報🔓です。"
274 register: find_chars
275 failed_when: (find_chars is not changed) or (find_chars is failed)
276
277 - name: Cron file deletion
278 cron:
279 cron_file: cron_nonascii
280 name: "cron job that contain non-ascii chars in job (これは日本語です; This is Japanese)"
281 state: absent
282
283 - name: Check file successful deletion
284 stat:
285 path: /etc/cron.d/cron_nonascii
286 register: cron_file_stats
287
288 - assert:
289 that: not cron_file_stats.stat.exists
290
291 - name: Allow non-ascii chars in cron_file (#69492)
292 when: ansible_distribution != 'Alpine'
293 block:
294 - name: Cron file creation with non-ascii filename (これは日本語です; This is Japanese)
295 cron:
296 cron_file: 'なせば大抵なんとかなる👊'
297 name: "integration test cron"
298 job: 'echo "Hello, ansible!"'
299 user: root
300
301 - name: Check file exists
302 stat:
303 path: "/etc/cron.d/なせば大抵なんとかなる👊"
304 register: cron_file_stats
305
306 - assert:
307 that: cron_file_stats.stat.exists
308
309 - name: Cron file deletion
310 cron:
311 cron_file: 'なせば大抵なんとかなる👊'
312 name: "integration test cron"
313 state: absent
314
315 - name: Check file successful deletion
316 stat:
317 path: "/etc/cron.d/なせば大抵なんとかなる👊"
318 register: cron_file_stats
319
320 - assert:
321 that: not cron_file_stats.stat.exists