Skip to content

Commit 43a9afe

Browse files
authored
fix(roles/init): add assertion for package manager (#207)
* fix(roles/init): add assertion for package manager * add changelog frag
1 parent 70d6190 commit 43a9afe

4 files changed

Lines changed: 62 additions & 1 deletion

File tree

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
minor_changes:
2+
- init role - improve error-handling when no package manager is found (https://github.com/ansible-collections/community.openwrt/issues/204, https://github.com/ansible-collections/community.openwrt/pull/207).
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Copyright (c) 2026, Alexei Znamensky (@russoz)
2+
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
- name: Remove package manager
6+
hosts: all
7+
gather_facts: false
8+
tasks:
9+
- name: Block for init role (ssh failure)
10+
block:
11+
- name: Invoke init role
12+
ansible.builtin.include_role:
13+
name: community.openwrt.init
14+
vars:
15+
ansible_host: 192.168.255.255
16+
openwrt_install_recommended_packages: false
17+
rescue:
18+
- name: Assert role fails
19+
ansible.builtin.assert:
20+
that:
21+
- ansible_failed_result is failed
22+
- >-
23+
'Could not find package manager:' in ansible_failed_result.msg
24+
25+
- name: Remove package managers
26+
community.openwrt.command:
27+
cmd: mv /bin/opkg /bin/opkg.orig || mv /usr/bin/apk /usr/bin/apk.orig
28+
uses_shell: true
29+
30+
- name: Block for init role (package manager is not there)
31+
block:
32+
- name: Invoke init role
33+
ansible.builtin.include_role:
34+
name: community.openwrt.init
35+
vars:
36+
openwrt_install_recommended_packages: false
37+
rescue:
38+
- name: Assert role fails
39+
ansible.builtin.assert:
40+
that:
41+
- ansible_failed_result is failed
42+
- >-
43+
'Could not find package manager:' in ansible_failed_result.msg
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
# Copyright (c) 2026, Alexei Znamensky (@russoz)
2+
# GNU General Public License v3.0+ (see LICENSE or https://www.gnu.org/licenses/gpl-3.0.txt)
3+
# SPDX-License-Identifier: GPL-3.0-or-later
4+
5+
driver:
6+
name: docker
7+
scenario:
8+
test_sequence:
9+
- create
10+
- converge
11+
- destroy

roles/init/tasks/main.yml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,13 +32,18 @@
3232
changed_when: false
3333
failed_when: false
3434

35+
- name: Assert package manager is found
36+
ansible.builtin.assert:
37+
fail_msg: "Could not find package manager: neither `apk` nor `opkg` is available."
38+
that:
39+
- _apk_available.rc | default(1) == 0 or _opkg_available.rc | default(1) == 0
40+
3541
- name: Check for package manager availability
3642
vars:
3743
has_apk: "{{ _apk_available.rc == 0 }}"
3844
has_opkg: "{{ _opkg_available.rc == 0 }}"
3945
ansible.builtin.set_fact:
4046
openwrt_package_manager: "{{ 'apk' if has_apk else ('opkg' if has_opkg else None) }}"
41-
failed_when: not openwrt_package_manager
4247
check_mode: false
4348

4449
- name: Install recommended packages for module compatibility

0 commit comments

Comments
 (0)