1- block:
2  - name: Install foo package version 1.0.0
3    apt:
4      name: foo=1.0.0
5      allow_unauthenticated: yes
6    register: apt_result
7
8  - name: Check install with dpkg
9    shell: dpkg-query -l foo
10    register: dpkg_result
11
12  - name: Check if install was successful
13    assert:
14      that:
15        - "apt_result is success"
16        - "dpkg_result is success"
17        - "'1.0.0' in dpkg_result.stdout"
18
19  - name: Update to foo version 1.0.1
20    apt:
21      name: foo
22      state: latest
23      allow_unauthenticated: yes
24    register: apt_result
25
26  - name: Check install with dpkg
27    shell: dpkg-query -l foo
28    register: dpkg_result
29
30  - name: Check if install was successful
31    assert:
32      that:
33        - "apt_result is success"
34        - "dpkg_result is success"
35        - "'1.0.1' in dpkg_result.stdout"
36  always:
37    - name: Clean up
38      apt:
39        name: foo
40        state: absent
41        allow_unauthenticated: yes
42
43
44# https://github.com/ansible/ansible/issues/30638
45- block:
46  - name: Fail to install foo=1.0.1 since foo is not installed and only_upgrade is set
47    apt:
48      name: foo=1.0.1
49      state: present
50      only_upgrade: yes
51      allow_unauthenticated: yes
52    ignore_errors: yes
53    register: apt_result
54
55  - name: Check that foo was not upgraded
56    assert:
57      that:
58        - "apt_result is not changed"
59
60  - apt:
61      name: foo=1.0.0
62      allow_unauthenticated: yes
63
64  - name: Upgrade foo to 1.0.1
65    apt:
66      name: foo=1.0.1
67      state: present
68      only_upgrade: yes
69      allow_unauthenticated: yes
70    register: apt_result
71
72  - name: Check install with dpkg
73    shell: dpkg-query -l foo
74    register: dpkg_result
75
76  - name: Check if install was successful
77    assert:
78      that:
79        - "apt_result is success"
80        - "dpkg_result is success"
81        - "'1.0.1' in dpkg_result.stdout"
82  always:
83    - name: Clean up
84      apt:
85        name: foo
86        state: absent
87        allow_unauthenticated: yes
88
89
90# https://github.com/ansible/ansible/issues/35900
91- block:
92  - name: Disable ubuntu repos so system packages are not upgraded and do not change testing env
93    command: mv /etc/apt/sources.list /etc/apt/sources.list.backup
94
95  - name: Install foobar, installs foo as a dependency
96    apt:
97      name: foobar=1.0.0
98      allow_unauthenticated: yes
99
100  - name: Upgrade foobar to a version which does not depend on foo, autoremove should remove foo
101    apt:
102      upgrade: dist
103      autoremove: yes
104      allow_unauthenticated: yes
105
106  - name: Check foo with dpkg
107    shell: dpkg-query -l foo
108    register: dpkg_result
109    ignore_errors: yes
110
111  - name: Check that foo was removed by autoremove
112    assert:
113      that:
114        - "dpkg_result is failed"
115
116  always:
117    - name: Clean up
118      apt:
119        pkg: foo,foobar
120        state: absent
121        autoclean: yes
122
123    - name: Restore ubuntu repos
124      command: mv /etc/apt/sources.list.backup /etc/apt/sources.list
125
126
127# https://github.com/ansible/ansible/issues/26298
128- block:
129  - name: Disable ubuntu repos so system packages are not upgraded and do not change testing env
130    command: mv /etc/apt/sources.list /etc/apt/sources.list.backup
131
132  - name: Install foobar, installs foo as a dependency
133    apt:
134      name: foobar=1.0.0
135      allow_unauthenticated: yes
136
137  - name: Upgrade foobar to a version which does not depend on foo
138    apt:
139      upgrade: dist
140      force: yes  # workaround for --allow-unauthenticated used along with upgrade
141
142  - name: autoremove should remove foo
143    apt:
144      autoremove: yes
145    register: autoremove_result
146
147  - name: Check that autoremove correctly reports changed=True
148    assert:
149      that:
150        - "autoremove_result is changed"
151
152  - name: Check foo with dpkg
153    shell: dpkg-query -l foo
154    register: dpkg_result
155    ignore_errors: yes
156
157  - name: Check that foo was removed by autoremove
158    assert:
159      that:
160        - "dpkg_result is failed"
161
162  - name: Nothing to autoremove
163    apt:
164      autoremove: yes
165    register: autoremove_result
166
167  - name: Check that autoremove correctly reports changed=False
168    assert:
169      that:
170        - "autoremove_result is not changed"
171
172  - name: Create a fake .deb file for autoclean to remove
173    file:
174      name: /var/cache/apt/archives/python3-q_2.4-1_all.deb
175      state: touch
176
177  - name: autoclean fake .deb file
178    apt:
179      autoclean: yes
180    register: autoclean_result
181
182  - name: Check if the .deb file exists
183    stat:
184      path: /var/cache/apt/archives/python3-q_2.4-1_all.deb
185    register: stat_result
186
187  - name: Check that autoclean correctly reports changed=True and file was removed
188    assert:
189      that:
190        - "autoclean_result is changed"
191        - "not stat_result.stat.exists"
192
193  - name: Nothing to autoclean
194    apt:
195      autoclean: yes
196    register: autoclean_result
197
198  - name: Check that autoclean correctly reports changed=False
199    assert:
200      that:
201        - "autoclean_result is not changed"
202
203  always:
204    - name: Clean up
205      apt:
206        pkg: foo,foobar
207        state: absent
208        autoclean: yes
209
210    - name: Restore ubuntu repos
211      command: mv /etc/apt/sources.list.backup /etc/apt/sources.list
212
213
214- name: Upgrades
215  block:
216    - include: "upgrade.yml aptitude_present={{ True | bool }} upgrade_type=dist force_apt_get={{ False | bool }}"
217
218    - name: Check if aptitude is installed
219      command: dpkg-query --show --showformat='${db:Status-Abbrev}' aptitude
220      register: aptitude_status
221
222    - name: Remove aptitude, if installed, to test fall-back to apt-get
223      apt:
224        pkg: aptitude
225        state: absent
226      when:
227        - aptitude_status.stdout.find('ii') != -1
228
229    - include: "upgrade.yml aptitude_present={{ False | bool }} upgrade_type={{ item.upgrade_type }} force_apt_get={{ item.force_apt_get }}"
230      with_items:
231        - { upgrade_type: safe, force_apt_get: False }
232        - { upgrade_type: full, force_apt_get: False }
233        - { upgrade_type: safe, force_apt_get: True }
234        - { upgrade_type: full, force_apt_get: True }
235
236    - name: (Re-)Install aptitude, run same tests again
237      apt:
238        pkg: aptitude
239        state: present
240
241    - include: "upgrade.yml aptitude_present={{ True | bool }} upgrade_type={{ item.upgrade_type }} force_apt_get={{ item.force_apt_get }}"
242      with_items:
243        - { upgrade_type: safe, force_apt_get: False }
244        - { upgrade_type: full, force_apt_get: False }
245        - { upgrade_type: safe, force_apt_get: True }
246        - { upgrade_type: full, force_apt_get: True }
247
248    - name: Remove aptitude if not originally present
249      apt:
250        pkg: aptitude
251        state: absent
252      when:
253        - aptitude_status.stdout.find('ii') == -1
254