1- name: use python-apt
2  set_fact:
3    python_apt: python-apt
4  when: ansible_python_version is version('3', '<')
5
6- name: use python3-apt
7  set_fact:
8    python_apt: python3-apt
9  when: ansible_python_version is version('3', '>=')
10
11- name: use Debian mirror
12  set_fact:
13    distro_mirror: http://ftp.debian.org/debian
14  when: ansible_distribution == 'Debian'
15
16- name: use Ubuntu mirror
17  set_fact:
18    distro_mirror: http://archive.ubuntu.com/ubuntu
19  when: ansible_distribution == 'Ubuntu'
20
21# UNINSTALL 'python-apt'
22#  The `apt` module has the smarts to auto-install `python-apt`.  To test, we
23#  will first uninstall `python-apt`.
24- name: check {{ python_apt }} with dpkg
25  shell: dpkg -s {{ python_apt }}
26  register: dpkg_result
27  ignore_errors: true
28
29- name: uninstall {{ python_apt }} with apt
30  apt: pkg={{ python_apt }} state=absent purge=yes
31  register: apt_result
32  when: dpkg_result is successful
33
34# In check mode, auto-install of `python-apt` must fail
35- name: test fail uninstall hello without required apt deps in check mode
36  apt:
37    pkg: hello
38    state: absent
39    purge: yes
40  register: apt_result
41  check_mode: yes
42  ignore_errors: yes
43
44- name: verify fail uninstall hello without required apt deps in check mode
45  assert:
46    that:
47    - apt_result is failed
48    - '"If run normally this module can auto-install it." in apt_result.msg'
49
50- name: check {{ python_apt }} with dpkg
51  shell: dpkg -s {{ python_apt }}
52  register: dpkg_result
53  ignore_errors: true
54
55# UNINSTALL 'hello'
56#   With 'python-apt' uninstalled, the first call to 'apt' should install
57#   python-apt without updating the cache.
58- name: uninstall hello with apt and prevent updating the cache
59  apt:
60    pkg: hello
61    state: absent
62    purge: yes
63    update_cache: no
64  register: apt_result
65
66- name: check hello with dpkg
67  shell: dpkg-query -l hello
68  failed_when: False
69  register: dpkg_result
70
71- name: verify uninstall hello with apt and prevent updating the cache
72  assert:
73    that:
74    - "'changed' in apt_result"
75    - apt_result is not changed
76    - "dpkg_result.rc == 1"
77    - "'Auto-installing missing dependency without updating cache: {{ python_apt }}' in apt_result.warnings"
78
79- name: Test installing fnmatch package
80  apt:
81    name:
82      - hel?o
83      - he?lo
84  register: apt_install_fnmatch
85
86- name: Test uninstalling fnmatch package
87  apt:
88    name:
89      - hel?o
90      - he?lo
91    state: absent
92  register: apt_uninstall_fnmatch
93
94- name: verify fnmatch
95  assert:
96    that:
97      - apt_install_fnmatch is changed
98      - apt_uninstall_fnmatch is changed
99
100- name: Test update_cache 1
101  apt:
102    update_cache: true
103    cache_valid_time: 10
104  register: apt_update_cache_1
105
106- name: Test update_cache 2
107  apt:
108    update_cache: true
109    cache_valid_time: 10
110  register: apt_update_cache_2
111
112- name: verify update_cache
113  assert:
114    that:
115      - apt_update_cache_1 is changed
116      - apt_update_cache_2 is not changed
117
118- name: uninstall {{ python_apt }} with apt again
119  apt:
120    pkg: "{{ python_apt }}"
121    state: absent
122    purge: yes
123
124# UNINSTALL 'hello'
125#   With 'python-apt' uninstalled, the first call to 'apt' should install
126#   python-apt.
127- name: uninstall hello with apt
128  apt: pkg=hello state=absent purge=yes
129  register: apt_result
130  until: apt_result is success
131
132- name: check hello with dpkg
133  shell: dpkg-query -l hello
134  failed_when: False
135  register: dpkg_result
136
137- name: verify uninstallation of hello
138  assert:
139    that:
140    - "'changed' in apt_result"
141    - apt_result is not changed
142    - "dpkg_result.rc == 1"
143    - "'Updating cache and auto-installing missing dependency: {{ python_apt }}' in apt_result.warnings"
144
145# UNINSTALL AGAIN
146- name: uninstall hello with apt
147  apt: pkg=hello state=absent purge=yes
148  register: apt_result
149
150- name: verify no change on re-uninstall
151  assert:
152    that:
153        - "not apt_result.changed"
154
155# INSTALL
156- name: install hello with apt
157  apt: name=hello state=present
158  register: apt_result
159
160- name: check hello with dpkg
161  shell: dpkg-query -l hello
162  failed_when: False
163  register: dpkg_result
164
165- name: verify installation of hello
166  assert:
167    that:
168        - "apt_result.changed"
169        - "dpkg_result.rc == 0"
170
171- name: verify apt module outputs
172  assert:
173    that:
174        - "'changed' in apt_result"
175        - "'stderr' in apt_result"
176        - "'stdout' in apt_result"
177        - "'stdout_lines' in apt_result"
178
179# INSTALL AGAIN
180- name: install hello with apt
181  apt: name=hello state=present
182  register: apt_result
183
184- name: verify no change on re-install
185  assert:
186    that:
187        - "not apt_result.changed"
188
189# UNINSTALL AGAIN
190- name: uninstall hello with apt
191  apt: pkg=hello state=absent purge=yes
192  register: apt_result
193
194# INSTALL WITH VERSION WILDCARD
195- name: install hello with apt
196  apt: name=hello=2.* state=present
197  register: apt_result
198
199- name: check hello with wildcard with  dpkg
200  shell: dpkg-query -l hello
201  failed_when: False
202  register: dpkg_result
203
204- name: verify installation of hello
205  assert:
206    that:
207        - "apt_result.changed"
208        - "dpkg_result.rc == 0"
209
210- name: check hello version
211  shell: dpkg -s hello | grep Version | awk '{print $2}'
212  register: hello_version
213
214- name: check hello architecture
215  shell: dpkg -s hello | grep Architecture | awk '{print $2}'
216  register: hello_architecture
217
218- name: uninstall hello with apt
219  apt: pkg=hello state=absent purge=yes
220
221- name: install deb file
222  apt: deb="/var/cache/apt/archives/hello_{{ hello_version.stdout }}_{{ hello_architecture.stdout }}.deb"
223  register: apt_initial
224
225- name: install deb file again
226  apt: deb="/var/cache/apt/archives/hello_{{ hello_version.stdout }}_{{ hello_architecture.stdout }}.deb"
227  register: apt_secondary
228
229- name: verify installation of hello
230  assert:
231    that:
232        - "apt_initial.changed"
233        - "not apt_secondary.changed"
234
235- name: uninstall hello with apt
236  apt: pkg=hello state=absent purge=yes
237
238- name: install deb file from URL
239  apt: deb="{{ distro_mirror }}/pool/main/h/hello/hello_{{ hello_version.stdout }}_{{ hello_architecture.stdout }}.deb"
240  register: apt_url
241
242- name: verify installation of hello
243  assert:
244    that:
245        - "apt_url.changed"
246
247- name: uninstall hello with apt
248  apt: pkg=hello state=absent purge=yes
249
250- name: force install of deb
251  apt: deb="/var/cache/apt/archives/hello_{{ hello_version.stdout }}_{{ hello_architecture.stdout }}.deb" force=true
252  register: dpkg_force
253
254- name: verify installation of hello
255  assert:
256    that:
257        - "dpkg_force.changed"
258
259# NEGATIVE: upgrade all packages while providing additional packages to install
260- name: provide additional packages to install while upgrading all installed packages
261  apt: pkg=*,test state=latest
262  ignore_errors: True
263  register: apt_result
264
265- name: verify failure of upgrade packages and install
266  assert:
267    that:
268        - "not apt_result.changed"
269        - "apt_result.failed"
270
271- name: autoclean during install
272  apt: pkg=hello state=present autoclean=yes
273
274- name: undo previous install
275  apt: pkg=hello state=absent
276
277# https://github.com/ansible/ansible/issues/23155
278- name: create a repo file
279  copy:
280    dest: /etc/apt/sources.list.d/non-existing.list
281    content: deb http://ppa.launchpad.net/non-existing trusty main
282
283- name: test for sane error message
284  apt:
285    update_cache: yes
286  register: apt_result
287  ignore_errors: yes
288
289- name: verify sane error message
290  assert:
291    that:
292      - "'Failed to fetch' in apt_result['msg']"
293      - "'403' in apt_result['msg']"
294
295- name: Clean up
296  file:
297    name: /etc/apt/sources.list.d/non-existing.list
298    state: absent
299
300# https://github.com/ansible/ansible/issues/28907
301- name: Install parent package
302  apt:
303    name: libcaca-dev
304
305- name: Install child package
306  apt:
307    name: libslang2-dev
308
309- shell: apt-mark showmanual | grep libcaca-dev
310  ignore_errors: yes
311  register: parent_output
312
313- name: Check that parent package is marked as installed manually
314  assert:
315    that:
316      - "'libcaca-dev' in parent_output.stdout"
317
318- shell: apt-mark showmanual | grep libslang2-dev
319  ignore_errors: yes
320  register: child_output
321
322- name: Check that child package is marked as installed manually
323  assert:
324    that:
325      - "'libslang2-dev' in child_output.stdout"
326
327- name: Clean up
328  apt:
329    name: "{{ pkgs }}"
330    state: absent
331  vars:
332    pkgs:
333      - libcaca-dev
334      - libslang2-dev
335
336# https://github.com/ansible/ansible/issues/38995
337- name: build-dep for a package
338  apt:
339    name: tree
340    state: build-dep
341  register: apt_result
342
343- name: Check the result
344  assert:
345    that:
346      - apt_result is changed
347
348- name: build-dep for a package (idempotency)
349  apt:
350    name: tree
351    state: build-dep
352  register: apt_result
353
354- name: Check the result
355  assert:
356    that:
357      - apt_result is not changed
358
359# check policy_rc_d parameter
360
361- name: Install unscd but forbid service start
362  apt:
363    name: unscd
364    policy_rc_d: 101
365
366- name: Stop unscd service
367  service:
368    name: unscd
369    state: stopped
370  register: service_unscd_stop
371
372- name: unscd service shouldn't have been stopped by previous task
373  assert:
374    that: service_unscd_stop is not changed
375
376- name: Uninstall unscd
377  apt:
378    name: unscd
379    policy_rc_d: 101
380
381- name: Create incorrect /usr/sbin/policy-rc.d
382  copy:
383    dest: /usr/sbin/policy-rc.d
384    content: apt integration test
385    mode: 0755
386
387- name: Install unscd but forbid service start
388  apt:
389    name: unscd
390    policy_rc_d: 101
391
392- name: Stop unscd service
393  service:
394    name: unscd
395    state: stopped
396  register: service_unscd_stop
397
398- name: unscd service shouldn't have been stopped by previous task
399  assert:
400    that: service_unscd_stop is not changed
401
402- name: Create incorrect /usr/sbin/policy-rc.d
403  copy:
404    dest: /usr/sbin/policy-rc.d
405    content: apt integration test
406    mode: 0755
407  register: policy_rc_d
408
409- name: Check if /usr/sbin/policy-rc.d was correctly backed-up during unscd install
410  assert:
411    that: policy_rc_d is not changed
412
413- name: Delete /usr/sbin/policy-rc.d
414  file:
415    path: /usr/sbin/policy-rc.d
416    state: absent
417