Skip to content

Commit 212f044

Browse files
committed
fix: Skip runtime operations in non-systemd environments
Cause: Both the actual role and the tests were previously assuming that the system was booted with systemd and could run services. But this is not the case when running the role during container builds. Consequence: The role did not work during bootc container builds. Fix: Detect if the system is booted (with systemd), and skip all runtime operations and checks if not. Result: The role now works during container builds. Call our firewall modules with `online: false` to select their `firewall-offline-cmd` modes when not booted. Add a new "online" parameter to get_files_checksums.sh that skips the reload when offline. Adjust tests_firewall_fact.yml to ensure that "detailed" mode fact collection fails in container mode (as that is currently unsupported, see previous commit). https://issues.redhat.com/browse/RHEL-88425
1 parent 75bc291 commit 212f044

File tree

11 files changed

+189
-39
lines changed

11 files changed

+189
-39
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -628,6 +628,9 @@ A list of PCI devices with their IDs can be retrieved using `lcpci -nn`.
628628
For more information on PCI device IDs, see the linux man page at:
629629
<https://man7.org/linux/man-pages/man5/pci.ids.5.html>
630630

631+
This option requires running the role on the actual target machine, i.e. it is
632+
not supported during container builds.
633+
631634
### icmp_block
632635

633636
String or list of ICMP type strings to block. The ICMP type names needs to be

files/get_files_checksums.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ firewall_conf_root="${2:-/etc/firewalld}"
77
remove="${3:-false}"
88
package="${4:-}"
99
firewall_usr_lib="${5:-}"
10+
booted="${6:-}"
1011

1112
listfile=$(mktemp)
1213
firewallconf=$(mktemp)
@@ -66,7 +67,7 @@ fc.filename=sys.argv[1] # Change target firewalld.conf write target
6667
fc.write() # update firewalld.conf
6768
' "$orig_conf" 2>/dev/null
6869
fi
69-
if [ -s "$listfile" ] ; then
70+
if [ -s "$listfile" ] && [ -n "$booted" ] ; then
7071
firewall-cmd --reload > /dev/null
7172
fi
7273
fi

library/firewall_lib.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,6 +1503,9 @@ def get_interface_pci():
15031503

15041504

15051505
def parse_pci_id(module, item):
1506+
if not module.params["online"]:
1507+
module.fail_json(msg="interface_pci_id is not supported in offline mode.")
1508+
15061509
if PCI_REGEX.search(item):
15071510
global pci_ids
15081511
if not pci_ids:

tasks/firewalld.yml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
difference(ansible_facts.keys() | list) | length > 0
77

88
- name: Determine if system is ostree and set flag
9-
when: not __firewall_is_ostree is defined
9+
when: __firewall_is_ostree is not defined
1010
block:
1111
- name: Check if system is ostree
1212
stat:
@@ -29,6 +29,26 @@
2929
set_fact:
3030
__firewall_is_transactional: "{{ __transactional_update_stat.stat.exists }}"
3131

32+
- name: Determine if system is booted with systemd
33+
when: __firewall_is_booted is not defined
34+
block:
35+
- name: Run systemctl
36+
# noqa command-instead-of-module
37+
command: systemctl is-system-running
38+
register: __is_system_running
39+
changed_when: false
40+
failed_when: false
41+
42+
- name: Require installed systemd
43+
fail:
44+
msg: "Error: This role requires systemd to be installed."
45+
when: '"No such file or directory" in __is_system_running.msg | d("")'
46+
47+
- name: Set flag to indicate that systemd runtime operations are available
48+
set_fact:
49+
# see https://www.man7.org/linux/man-pages/man1/systemctl.1.html#:~:text=is-system-running%20output
50+
__firewall_is_booted: "{{ __is_system_running.stdout != 'offline' }}"
51+
3252
- name: Install firewalld
3353
package:
3454
name: "{{ __firewall_packages_base }}"

tasks/main.yml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
- name: Attempt to stop and disable conflicting services
1515
service:
1616
name: "{{ item.item }}"
17-
state: stopped
17+
state: "{{ 'stopped' if __firewall_is_booted else omit }}"
1818
enabled: false
1919
loop: "{{ __firewall_conflicting_services_status.results }}"
2020
when:
@@ -30,8 +30,8 @@
3030
- name: Enable and start firewalld service
3131
service:
3232
name: "{{ __firewall_service }}"
33-
state: started
3433
enabled: true
34+
state: "{{ 'started' if __firewall_is_booted else omit }}"
3535

3636
- name: Check if previous replaced is defined
3737
set_fact:
@@ -54,6 +54,7 @@
5454
{{ ansible_check_mode | ternary('false', 'true') }}
5555
{{ __firewall_package_with_conf | quote }}
5656
{{ __firewall_usr_lib_dir | quote }}
57+
{{ "booted" if __firewall_is_booted else "" }}
5758
register: __firewall_config_files_before
5859
changed_when: false
5960
check_mode: false
@@ -111,10 +112,11 @@
111112
protocol: "{{ item.protocol | default(omit) }}"
112113
helper_module: "{{ item.helper_module | default(omit) }}"
113114
permanent: "{{ item.permanent | default(True) }}"
114-
runtime: "{{ item.runtime | default(True) }}"
115+
runtime: "{{ item.runtime | default(__firewall_is_booted) }}"
115116
state: "{{ item.state | default(omit) }}"
116117
includes: "{{ item.includes | default(omit) }}"
117118
__report_changed: "{{ __firewall_report_changed }}"
119+
online: "{{ __firewall_is_booted }}"
118120
loop: "{{ firewall is mapping | ternary([firewall], firewall) |
119121
map('dict2items') | map('difference', __previous) |
120122
map('difference', __detailed) | select |
@@ -136,6 +138,7 @@
136138
value: replaced
137139
firewall_lib_facts:
138140
detailed: "{{ item.detailed | default(False) }}"
141+
online: "{{ __firewall_is_booted }}"
139142
loop: "{{ fw | map('dict2items') | map('difference', __previous) |
140143
select | map('items2dict') | list }}"
141144
register: __firewalld_facts
@@ -151,6 +154,7 @@
151154
block:
152155
- name: Gather firewall config if no arguments
153156
firewall_lib_facts:
157+
online: "{{ __firewall_is_booted }}"
154158
detailed: false
155159
register: __firewalld_facts
156160

@@ -170,6 +174,7 @@
170174
{{ __firewall_firewalld_dir | quote }} false
171175
{{ __firewall_package_with_conf | quote }}
172176
{{ __firewall_usr_lib_dir | quote }}
177+
{{ __firewall_is_booted | quote }}
173178
register: __firewall_config_files_after
174179
changed_when: false
175180

0 commit comments

Comments
 (0)