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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
|
# Test code for the unarchive module.
# (c) 2014, Richard Isaacson <richard.c.isaacson@gmail.com>
# This file is part of Ansible
#
# Ansible is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Ansible is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
# Make sure we start fresh
- name: Ensure zip is present to create test archive (yum)
yum: name=zip state=latest
when: ansible_pkg_mgr == 'yum'
#- name: Ensure zip is present to create test archive (dnf)
# dnf: name=zip state=latest
# when: ansible_pkg_mgr == 'dnf'
- name: Ensure zip is present to create test archive (apt)
apt: name=zip state=latest
when: ansible_pkg_mgr == 'apt'
- name: prep our file
copy: src=foo.txt dest={{output_dir}}/foo-unarchive.txt
- name: prep a tar file
shell: tar cvf test-unarchive.tar foo-unarchive.txt chdir={{output_dir}}
- name: prep a tar.gz file
shell: tar czvf test-unarchive.tar.gz foo-unarchive.txt chdir={{output_dir}}
- name: prep a zip file
shell: zip test-unarchive.zip foo-unarchive.txt chdir={{output_dir}}
- name: prep a subdirectory
file: path={{output_dir}}/unarchive-dir state=directory
- name: prep our file
copy: src=foo.txt dest={{output_dir}}/unarchive-dir/foo-unarchive.txt
- name: prep a tar.gz file with directory
shell: tar czvf test-unarchive-dir.tar.gz unarchive-dir chdir={{output_dir}}
- name: create our tar unarchive destination
file: path={{output_dir}}/test-unarchive-tar state=directory
- name: unarchive a tar file
unarchive: src={{output_dir}}/test-unarchive.tar dest="{{output_dir | expanduser}}/test-unarchive-tar" remote_src=yes
register: unarchive01
- name: verify that the file was marked as changed
assert:
that:
- "unarchive01.changed == true"
- name: verify that the file was unarchived
file: path={{output_dir}}/test-unarchive-tar/foo-unarchive.txt state=file
- name: remove our tar unarchive destination
file: path={{output_dir}}/test-unarchive-tar state=absent
- name: create our tar.gz unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive a tar.gz file
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest={{output_dir | expanduser}}/test-unarchive-tar-gz remote_src=yes
register: unarchive02
- name: verify that the file was marked as changed
assert:
that:
- "unarchive02.changed == true"
# Verify that no file list is generated
- "'files' not in unarchive02"
- name: verify that the file was unarchived
file: path={{output_dir}}/test-unarchive-tar-gz/foo-unarchive.txt state=file
- name: remove our tar.gz unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=absent
- name: create our tar.gz unarchive destination for creates
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive a tar.gz file with creates set
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest={{output_dir | expanduser}}/test-unarchive-tar-gz remote_src=yes creates={{output_dir}}/test-unarchive-tar-gz/foo-unarchive.txt
register: unarchive02b
- name: verify that the file was marked as changed
assert:
that:
- "unarchive02b.changed == true"
- name: verify that the file was unarchived
file: path={{output_dir}}/test-unarchive-tar-gz/foo-unarchive.txt state=file
- name: unarchive a tar.gz file with creates over an existing file
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest={{output_dir | expanduser}}/test-unarchive-tar-gz remote_src=yes creates={{output_dir}}/test-unarchive-tar-gz/foo-unarchive.txt
register: unarchive02c
- name: verify that the file was not marked as changed
assert:
that:
- "unarchive02c.changed == false"
- name: unarchive a tar.gz file with creates over an existing file using complex_args
unarchive:
src: "{{output_dir}}/test-unarchive.tar.gz"
dest: "{{output_dir | expanduser}}/test-unarchive-tar-gz"
remote_src: yes
creates: "{{output_dir}}/test-unarchive-tar-gz/foo-unarchive.txt"
register: unarchive02d
- name: verify that the file was not marked as changed
assert:
that:
- "unarchive02d.changed == false"
- name: remove our tar.gz unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=absent
- name: create our zip unarchive destination
file: path={{output_dir}}/test-unarchive-zip state=directory
- name: unarchive a zip file
unarchive: src={{output_dir}}/test-unarchive.zip dest={{output_dir | expanduser}}/test-unarchive-zip remote_src=yes list_files=True
register: unarchive03
- name: verify that the file was marked as changed
assert:
that:
- "unarchive03.changed == true"
# Verify that file list is generated
- "'files' in unarchive03"
- "{{unarchive03['files']| length}} == 1"
- "'foo-unarchive.txt' in unarchive03['files']"
- name: verify that the file was unarchived
file: path={{output_dir}}/test-unarchive-zip/foo-unarchive.txt state=file
- name: remove our zip unarchive destination
file: path={{output_dir}}/test-unarchive-zip state=absent
- name: remove our test file for the archive
file: path={{output_dir}}/foo-unarchive.txt state=absent
- name: check if /tmp/foo-unarchive.text exists
stat: path=/tmp/foo-unarchive.txt
ignore_errors: True
register: unarchive04
- name: fail if the proposed destination file exists for safey
fail: msg="/tmp/foo-unarchive.txt already exists, aborting"
when: unarchive04.stat.exists
- name: try unarchiving to /tmp
unarchive: src={{output_dir}}/test-unarchive.tar.gz dest=/tmp remote_src=yes
register: unarchive05
- name: verify that the file was marked as changed
assert:
that:
- "unarchive05.changed == true"
- name: verify that the file was unarchived
file: path=/tmp/foo-unarchive.txt state=file
- name: remove our unarchive destination
file: path=/tmp/foo-unarchive.txt state=absent
- name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive and set mode to 0600
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/test-unarchive-tar-gz"
remote_src: yes
mode: "u+rwX,g-rwx,o-rwx"
list_files: True
register: unarchive06
- name: Test that the file modes were changed
stat:
path: "{{ output_dir | expanduser }}/test-unarchive-tar-gz/foo-unarchive.txt"
register: unarchive06_stat
- name: Test that the file modes were changed
assert:
that:
- "unarchive06.changed == true"
- "unarchive06_stat.stat.mode == '0600'"
# Verify that file list is generated
- "'files' in unarchive06"
- "{{unarchive06['files']| length}} == 1"
- "'foo-unarchive.txt' in unarchive06['files']"
- name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
- name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive over existing extraction and set mode to 0644
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/test-unarchive-tar-gz"
remote_src: yes
mode: "u+rwX,g-wx,o-wx,g+r,o+r"
register: unarchive06_2
- name: Test that the file modes were changed
stat:
path: "{{ output_dir | expanduser }}/test-unarchive-tar-gz/foo-unarchive.txt"
register: unarchive06_2_stat
- debug: var=unarchive06_2_stat.stat.mode
- name: Test that the files were changed
assert:
that:
- "unarchive06_2.changed == true"
- "unarchive06_2_stat.stat.mode == '0644'"
- name: Repeat the last request to verify no changes
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/test-unarchive-tar-gz"
remote_src: yes
mode: "u+rwX,g-wx,o-wx,g+r,o+r"
list_files: True
register: unarchive07
- name: Test that the files were not changed
assert:
that:
- "unarchive07.changed == false"
# Verify that file list is generated
- "'files' in unarchive07"
- "{{unarchive07['files']| length}} == 1"
- "'foo-unarchive.txt' in unarchive07['files']"
- name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
- name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: create a directory with quotable chars
file: path="{{ output_dir }}/test-quotes~root" state=directory
- name: unarchive into directory with quotable chars
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/test-quotes~root"
remote_src: yes
register: unarchive08
- name: Test that unarchive succeeded
assert:
that:
- "unarchive08.changed == true"
- name: unarchive into directory with quotable chars a second time
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/test-quotes~root"
remote_src: yes
register: unarchive09
- name: Test that unarchive did nothing
assert:
that:
- "unarchive09.changed == false"
- name: remove quotable chars test
file: path="{{ output_dir }}/test-quotes~root" state=absent
- name: create our unarchive destination
file:
path: "{{ output_dir }}/test-unarchive-nonascii-くらとみ-tar-gz"
state: directory
- name: test that unarchive works with an archive that contains non-ascii filenames
unarchive:
# Both the filename of the tarball and the filename inside the tarball have
# nonascii chars
src: "test-unarchive-nonascii-くらとみ.tar.gz"
dest: "{{ output_dir }}/test-unarchive-nonascii-くらとみ-tar-gz"
mode: "u+rwX,go+rX"
remote_src: yes
register: nonascii_result0
- name: Check that file is really there
stat:
path: "{{ output_dir | expanduser }}/test-unarchive-nonascii-くらとみ-tar-gz/storage/àâæçéèïîôœ(copy)!@#$%^&-().jpg"
register: nonascii_stat0
- name: Assert that nonascii tests succeeded
assert:
that:
- "nonascii_result0.changed == true"
- "nonascii_stat0.stat.exists == true"
- name: remove nonascii test
file: path="{{ output_dir }}/test-unarchive-nonascii-くらとみ-tar-gz" state=absent
# Test that unarchiving is performed if files are missing
# https://github.com/ansible/ansible-modules-core/issues/1064
- name: create our unarchive destination
file: path={{output_dir}}/test-unarchive-tar-gz state=directory
- name: unarchive a tar that has directories
unarchive:
src: "{{ output_dir }}/test-unarchive-dir.tar.gz"
dest: "{{ output_dir }}/test-unarchive-tar-gz"
mode: "0700"
remote_src: yes
register: unarchive10
- name: Test that unarchive succeeded
assert:
that:
- "unarchive10.changed == true"
- name: Change the mode of the toplevel dir
file:
path: "{{ output_dir }}/test-unarchive-tar-gz/unarchive-dir"
mode: 0701
- name: Remove a file from the extraction point
file:
path: "{{ output_dir }}/test-unarchive-tar-gz/unarchive-dir/foo-unarchive.txt"
state: absent
- name: unarchive a tar that has directories
unarchive:
src: "{{ output_dir }}/test-unarchive-dir.tar.gz"
dest: "{{ output_dir }}/test-unarchive-tar-gz"
mode: "0700"
remote_src: yes
register: unarchive10_1
- name: Test that unarchive succeeded
assert:
that:
- "unarchive10_1.changed == true"
- name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
#
# Symlink tests
#
- name: Create a destination dir
file:
path: "{{ output_dir }}/test-unarchive-tar-gz"
state: directory
- name: Create a symlink to the detination dir
file:
path: "{{ output_dir }}/link-to-unarchive-dir"
src: "{{ output_dir }}/test-unarchive-tar-gz"
state: "link"
- name: test that unarchive works when dest is a symlink to a dir
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/link-to-unarchive-dir"
mode: "u+rwX,go+rX"
remote_src: yes
register: unarchive_11
- name: Check that file is really there
stat:
path: "{{ output_dir | expanduser }}/test-unarchive-tar-gz/foo-unarchive.txt"
register: unarchive11_stat0
- name: Assert that unarchive when dest is a symlink to a dir worked
assert:
that:
- "unarchive_11.changed == true"
- "unarchive11_stat0.stat.exists == true"
- name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
- name: Create a file
file:
path: "{{ output_dir }}/test-unarchive-tar-gz"
state: touch
- name: Create a symlink to the file
file:
path: "{{ output_dir }}/link-to-unarchive-file"
src: "{{ output_dir }}/test-unarchive-tar-gz"
state: "link"
- name: test that unarchive fails when dest is a link to a file
unarchive:
src: "{{ output_dir }}/test-unarchive.tar.gz"
dest: "{{ output_dir | expanduser }}/link-to-unarchive-file"
mode: "u+rwX,go+rX"
remote_src: yes
ignore_errors: True
register: unarchive_12
- name: Assert that unarchive when dest is a file failed
assert:
that:
- "unarchive_12.failed == true"
- name: remove our tar.gz unarchive destination
file: path={{ output_dir }}/test-unarchive-tar-gz state=absent
|