summaryrefslogtreecommitdiffstats
path: root/test/integration/targets/cron/tasks/main.yml
blob: 32e345d32cead8b4e3b6731bd8c517ee030054ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
- name: Include distribution specific variables
  include_vars: "{{ lookup('first_found', search) }}"
  vars:
    search:
      files:
        - '{{ ansible_distribution | lower }}.yml'
        - '{{ ansible_os_family | lower }}.yml'
        - '{{ ansible_system | lower }}.yml'
        - default.yml
      paths:
        - vars

# https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=726661
- name: Work around vixie-cron/PAM issue on old distros
  command: sed -i '/pam_loginuid/ s/^/#/' /etc/pam.d/crond
  when:
    - ansible_distribution in ('RedHat', 'CentOS')
    - ansible_distribution_major_version is version('6', '==')

- name: add cron task (check mode enabled, cron task not already created)
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
  check_mode: yes
  register: check_mode_enabled_state_present

- assert:
    that: check_mode_enabled_state_present is changed

- name: add cron task (check mode disabled, task hasn't already been created)
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
  register: add_cron_task

- assert:
    that: add_cron_task is changed

- name: add cron task (check mode enabled, cron task already exists)
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
  check_mode: yes
  register: check_mode_enabled_state_present_cron_task_already_exists

- assert:
    that: check_mode_enabled_state_present_cron_task_already_exists is not changed

- name: add cron task (check mode disabled, cron task already created)
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
  register: cron_task_already_created

- assert:
    that: cron_task_already_created is not changed

- block:
    - name: wait for canary creation
      wait_for:
        path: '{{ remote_dir }}/cron_canary1'
        timeout: '{{ 20 if faketime_pkg else 70 }}'
      register: wait_canary
  always:
    - name: display some logs in case of failure
      command: 'journalctl -u {{ cron_service }}'
      when: wait_canary is failed and ansible_service_mgr == 'systemd'

- debug:
    msg: 'elapsed time waiting for canary: {{ wait_canary.elapsed }}'

- name: Check check_mode
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
    state: absent
  check_mode: yes
  register: check_check_mode

- assert:
    that: check_check_mode is changed

- name: Remove a cron task
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
    state: absent
  register: remove_task

- assert:
    that: remove_task is changed

- name: 'cron task missing: check idempotence (check mode enabled, state=absent)'
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
    state: absent
  register: check_mode_enabled_remove_task_idempotence

- assert:
    that: check_mode_enabled_remove_task_idempotence is not changed

- name: 'cron task missing: check idempotence (check mode disabled, state=absent)'
  cron:
    name: test cron task
    job: 'date > {{ remote_dir }}/cron_canary1'
    state: absent
  register: remove_task_idempotence

- assert:
    that: remove_task_idempotence is not changed

- name: Check that removing a cron task with cron_file and without specifying a user is allowed (#58493)
  cron:
    name: test cron task
    cron_file: unexistent_cron_file
    state: absent
  register: remove_cron_file

- assert:
    that: remove_cron_file is not changed

- name: Non regression test - cron file should not be empty after adding var (#71207)
  when: ansible_distribution != 'Alpine'
  block:
  - name: Cron file creation
    cron:
      cron_file: cron_filename
      name: "simple cron job"
      job: 'echo "_o/"'
      user: root

  - name: Add var to the cron file
    cron:
      cron_file: cron_filename
      env: yes
      name: FOO
      value: bar
      user: root

  - name: "Ensure cron_file still contains job string"
    replace:
      path: /etc/cron.d/cron_filename
      regexp: "_o/"
      replace: "OK"
    register: find_chars
    failed_when: (find_chars is not changed) or (find_chars is failed)

# BusyBox does not have /etc/cron.d
- name: Removing a cron file when the name is specified is allowed (#57471)
  when: ansible_distribution != 'Alpine'
  block:
  - name: Check file does not exist
    stat:
      path: /etc/cron.d/cron_remove_name
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

  - name: Cron file creation
    cron:
      cron_file: cron_remove_name
      name: "integration test cron"
      job: 'ls'
      user: root

  - name: Cron file deletion
    cron:
      cron_file: cron_remove_name
      name: "integration test cron"
      state: absent

  - name: Check file succesfull deletion
    stat:
      path: /etc/cron.d/cron_remove_name
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

# BusyBox does not have /etc/cron.d
- name: Removing a cron file, which contains only whitespace
  when: ansible_distribution != 'Alpine'
  block:
  - name: Check file does not exist
    stat:
      path: /etc/cron.d/cron_remove_whitespace
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

  - name: Cron file creation
    cron:
      cron_file: cron_remove_whitespace
      name: "integration test cron"
      job: 'ls'
      user: root

  - name: Add whitespace to cron file
    shell: 'printf "\n    \n\t\n" >> /etc/cron.d/cron_remove_whitespace'

  - name: Cron file deletion
    cron:
      cron_file: cron_remove_whitespace
      name: "integration test cron"
      state: absent

  - name: Check file succesfull deletion
    stat:
      path: /etc/cron.d/cron_remove_whitespace
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

- name: System cron tab can not be managed
  when: ansible_distribution != 'Alpine'
  block:
  - name: Add cron job
    cron:
      cron_file: "{{ system_crontab }}"
      user: root
      name: "integration test cron"
      job: 'ls'
    ignore_errors: yes
    register: result

  - assert:
      that: "result.msg == 'Will not manage /etc/crontab via cron_file, see documentation.'"

# TODO: restrict other root crontab locations
- name: System cron tab does not get removed
  when: ansible_distribution == 'Alpine'
  block:
  - name: Add cron job
    cron:
      cron_file: "{{ system_crontab }}"
      user: root
      name: "integration test cron"
      job: 'ls'

  - name: Remove cron job
    cron:
      cron_file: "{{ system_crontab }}"
      name: "integration test cron"
      state: absent

  - name: Check system crontab still exists
    stat:
      path: "{{ system_crontab }}"
    register: cron_file_stats

  - assert:
      that: cron_file_stats.stat.exists

- name: Allow non-ascii chars in job (#69492)
  when: ansible_distribution != 'Alpine'
  block:
  - name: Check file does not exist
    stat:
      path: /etc/cron.d/cron_nonascii
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

  - name: Cron file creation
    cron:
      cron_file: cron_nonascii
      name: "cron job that contain non-ascii chars in job (これは日本語です; This is Japanese)"
      job: 'echo "うどんは好きだがお化け👻は苦手である。"'
      user: root

  - name: "Ensure cron_file contains job string"
    replace:
      path: /etc/cron.d/cron_nonascii
      regexp: "うどんは好きだがお化け👻は苦手である。"
      replace: "それは機密情報🔓です。"
    register: find_chars
    failed_when: (find_chars is not changed) or (find_chars is failed)

  - name: Cron file deletion
    cron:
      cron_file: cron_nonascii
      name: "cron job that contain non-ascii chars in job (これは日本語です; This is Japanese)"
      state: absent

  - name: Check file succesfull deletion
    stat:
      path: /etc/cron.d/cron_nonascii
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists

- name: Allow non-ascii chars in cron_file (#69492)
  when: ansible_distribution != 'Alpine'
  block:
  - name: Cron file creation with non-ascii filename (これは日本語です; This is Japanese)
    cron:
      cron_file: 'なせば大抵なんとかなる👊'
      name: "integration test cron"
      job: 'echo "Hello, ansible!"'
      user: root

  - name: Check file exists
    stat:
      path: "/etc/cron.d/なせば大抵なんとかなる👊"
    register: cron_file_stats

  - assert:
      that: cron_file_stats.stat.exists

  - name: Cron file deletion
    cron:
      cron_file: 'なせば大抵なんとかなる👊'
      name: "integration test cron"
      state: absent

  - name: Check file succesfull deletion
    stat:
      path: "/etc/cron.d/なせば大抵なんとかなる👊"
    register: cron_file_stats

  - assert:
      that: not cron_file_stats.stat.exists