Skip to content

Commit

Permalink
refactor: improve support for ostree systems
Browse files Browse the repository at this point in the history
The dependency on `ansible.utils.update_fact` is causing issue with
some users who now must install that collection in order to run
the role, even if they do not care about ostree.

The fix is to stop trying to set `ansible_facts.pkg_mgr`, and instead
force the use of the ostree package manager with the `package:` module
`use:` option.  The strategy is - on ostree systems, set the flag
`__$ROLENAME_is_ostree` if the system is an ostree system.  The flag
will either be undefined or `false` on non-ostree systems.
Then, change every invocation of the `package:` module like this:

```yaml
- name: Ensure required packages are present
  package:
    name: "{{ __$ROLENAME_packages }}"
    state: present
    use: "{{ (__$ROLENAME_is_ostree | d(false)) |
      ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
```

This should ensure that the `use:` parameter is not used if the system
is non-ostree.  The goal is to make the ostree support as unobtrusive
as possible for non-ostree systems.
The user can also set `__$ROLENAME_is_ostree: true` in the inventory or play
if the user knows that ostree is being used and wants to skip the check.
Or, the user is concerned about the performance hit for ostree detection
on non-ostree systems, and sets `__$ROLENAME_is_ostree: false` to skip
the check.
The flag `__$ROLENAME_is_ostree` can also be used in the role or tests to
include or exclude tasks from being run on ostree systems.

This fix also improves error reporting in the `get_ostree_data.sh` script
when included roles cannot be found.

Signed-off-by: Rich Megginson <rmeggins@redhat.com>
  • Loading branch information
richm committed Nov 29, 2023
1 parent c7a31e7 commit 0c590cd
Show file tree
Hide file tree
Showing 34 changed files with 111 additions and 55 deletions.
2 changes: 0 additions & 2 deletions .ansible-lint
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,3 @@ exclude_paths:
- examples/roles/
mock_roles:
- linux-system-roles.network
mock_modules:
- ansible.utils.update_fact
29 changes: 19 additions & 10 deletions .ostree/get_ostree_data.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

set -euo pipefail

role_collection_dir="${ROLE_COLLECTION_DIR:-fedora/linux_system_roles}"
ostree_dir="${OSTREE_DIR:-"$(dirname "$(realpath "$0")")"}"

if [ -z "${4:-}" ] || [ "${1:-}" = help ] || [ "${1:-}" = -h ]; then
Expand All @@ -29,7 +28,7 @@ if [ "$pkgtype" = testing ]; then
fi

get_rolepath() {
local ostree_dir role rolesdir roles_parent_dir
local ostree_dir role rolesdir roles_parent_dir coll_path pth
ostree_dir="$1"
role="$2"
roles_parent_dir="$(dirname "$(dirname "$ostree_dir")")"
Expand All @@ -47,16 +46,22 @@ get_rolepath() {
fi
done
# look elsewhere
if [ -n "${ANSIBLE_COLLECTIONS_PATHS:-}" ]; then
for pth in ${ANSIBLE_COLLECTIONS_PATHS//:/ }; do
rolesdir="$pth/ansible_collections/$role_collection_dir/roles/$role/.ostree"
if [ -d "$rolesdir" ]; then
echo "$rolesdir"
return 0
fi
coll_path="${ANSIBLE_COLLECTIONS_PATH:-}"
if [ -z "$coll_path" ]; then
coll_path="${ANSIBLE_COLLECTIONS_PATHS:-}"
fi
if [ -n "${coll_path}" ]; then
for pth in ${coll_path//:/ }; do
for rolesdir in "$pth"/ansible_collections/*/*_system_roles/roles/"$role"/.ostree; do
if [ -d "$rolesdir" ]; then
echo "$rolesdir"
return 0
fi
done
done
fi
return 1
1>&2 echo ERROR - could not find role "$role" - please use ANSIBLE_COLLECTIONS_PATH
exit 2
}

get_packages() {
Expand All @@ -75,6 +80,10 @@ get_packages() {
roles="$(cat "$rolefile")"
for role in $roles; do
rolepath="$(get_rolepath "$ostree_dir" "$role")"
if [ -z "$rolepath" ]; then
1>&2 echo ERROR - could not find role "$role" - please use ANSIBLE_COLLECTIONS_PATH
exit 2
fi
get_packages "$rolepath"
done
fi
Expand Down
1 change: 0 additions & 1 deletion meta/collection-requirements.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
---
collections:
- name: ansible.posix
- name: ansible.utils
6 changes: 6 additions & 0 deletions tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
package:
name: "{{ network_packages }}"
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- not network_packages is subset(ansible_facts.packages.keys())
register: __network_package_install
Expand All @@ -41,6 +43,8 @@
- NetworkManager
- nmstate
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- network_state is defined
- ansible_distribution == 'Fedora' and
Expand All @@ -53,6 +57,8 @@
name:
- python3-libnmstate
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- network_state is defined
- ansible_distribution == 'Fedora' and
Expand Down
18 changes: 6 additions & 12 deletions tasks/set_facts.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,17 @@
difference(ansible_facts.keys() | list) | length > 0
no_log: true

- name: Ensure correct package manager for ostree systems
vars:
ostree_pkg_mgr: ansible.posix.rhel_rpm_ostree
ostree_booted_file: /run/ostree-booted
when: ansible_facts.pkg_mgr | d("") != ostree_pkg_mgr
- name: Determine if system is ostree and set flag
when: not __network_is_ostree is defined
block:
- name: Check if system is ostree
stat:
path: "{{ ostree_booted_file }}"
path: /run/ostree-booted
register: __ostree_booted_stat

- name: Set package manager to use for ostree
ansible.utils.update_fact:
updates:
- path: ansible_facts.pkg_mgr
value: "{{ ostree_pkg_mgr }}"
when: __ostree_booted_stat.stat.exists
- name: Set flag to indicate system is ostree
set_fact:
__network_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

- name: Check which services are running
service_facts:
Expand Down
2 changes: 2 additions & 0 deletions tests/ensure_provider_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package:
name: NetworkManager
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Get package info
package_facts:
- name: Get NetworkManager version
Expand Down
4 changes: 4 additions & 0 deletions tests/playbooks/integration_pytest_python3.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
package:
state: present
name: "{{ rpmdependencies }}"
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Install Pytest
command: "pip3 install pytest"
Expand Down Expand Up @@ -151,6 +153,8 @@
package:
name: network-scripts
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Run pytest with initscripts
command: >
pytest
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/manual_test_ethtool_coalesce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
package:
name: ethtool
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Test ethtool coalesce settings
block:
- name: >-
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_802_1x.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@
package:
name: iputils
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: "TEST: I can ping the EAP server"
command: ping -c1 203.0.113.1
changed_when: false
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_802_1x_updated.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
package:
name: NetworkManager
state: latest # noqa package-latest
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Restart NetworkManager
service:
name: NetworkManager
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_checkpoint_cleanup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@
package:
name: dbus-tools
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
# create test profile
- name: Include network role
include_role:
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_ethtool_coalesce.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
package:
name: ethtool
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Test ethtool coalesce settings
block:
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_ethtool_features.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
package:
name: ethtool
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"


- name: Test ethtool features settings
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_ethtool_ring.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
package:
name: ethtool
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Test ethtool ring settings
block:
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_ipv6.yml
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,8 @@
package:
name: iputils
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
- name: Test gateway can be pinged
command: ping6 -c1 2001:db8::1
when:
Expand Down
2 changes: 2 additions & 0 deletions tests/playbooks/tests_network_state.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@
package:
name: systemd-resolved
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- ansible_distribution_major_version | int > 8

Expand Down
2 changes: 1 addition & 1 deletion tests/playbooks/tests_team_plugin_installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
tasks:
- name: Check if rpm ostree system - cannot test
meta: end_host
when: ansible_facts.pkg_mgr == "ansible.posix.rhel_rpm_ostree"
when: __network_is_ostree | d(false)

- name: Remove the NetworkManager-team package
package:
Expand Down
2 changes: 1 addition & 1 deletion tests/playbooks/tests_wireless_plugin_installation.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
tasks:
- name: Check if rpm ostree system - cannot test
meta: end_host
when: ansible_facts.pkg_mgr == "ansible.posix.rhel_rpm_ostree"
when: __network_is_ostree | d(false)

- name: Remove the NetworkManager-wifi package
package:
Expand Down
6 changes: 6 additions & 0 deletions tests/tasks/create_test_interfaces_with_dhcp.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,15 @@
package:
name: dnsmasq
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Install pgrep, sysctl
package:
name: procps
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version is version('6', '<=')
Expand All @@ -17,6 +21,8 @@
package:
name: procps-ng
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
when:
- ansible_os_family == 'RedHat'
- ansible_distribution_major_version is version('7', '>=')
Expand Down
20 changes: 7 additions & 13 deletions tests/tasks/el_repo_setup.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,17 @@
- distribution_version
- os_family

- name: Ensure correct package manager for ostree systems
vars:
ostree_pkg_mgr: ansible.posix.rhel_rpm_ostree
ostree_booted_file: /run/ostree-booted
when: ansible_facts.pkg_mgr | d("") != ostree_pkg_mgr
- name: Determine if system is ostree and set flag
when: not __network_is_ostree is defined
block:
- name: Check if system is ostree
stat:
path: "{{ ostree_booted_file }}"
path: /run/ostree-booted
register: __ostree_booted_stat

- name: Set package manager to use for ostree
ansible.utils.update_fact:
updates:
- path: ansible_facts.pkg_mgr
value: "{{ ostree_pkg_mgr }}"
when: __ostree_booted_stat.stat.exists
- name: Set flag to indicate system is ostree
set_fact:
__network_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"

- name: Fix CentOS6 Base repo
copy:
Expand Down Expand Up @@ -58,4 +52,4 @@
- ansible_distribution_major_version == '6'
- name: Include the task 'enable_epel.yml'
include_tasks: enable_epel.yml
when: ansible_facts["pkg_mgr"] != "ansible.posix.rhel_rpm_ostree"
when: not __network_is_ostree | d(false)
2 changes: 2 additions & 0 deletions tests/tasks/manage_test_interface.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@
package:
name: iproute
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

# veth
- name: Create veth interface {{ interface }}
Expand Down
8 changes: 7 additions & 1 deletion tests/tasks/setup_802_1x_server.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
# SPDX-License-Identifier: BSD-3-Clause
---
- name: Debug
debug:
msg: facts {{ ansible_facts | to_nice_json }}

# This task can be removed once the RHEL-8.5 is not tested anymore
- name: Install hostapd via CentOS Stream
command: dnf -y install http://mirror.centos.org/centos/8-stream/AppStream/x86_64/os/Packages/hostapd-2.10-1.el8.x86_64.rpm # noqa yaml[line-length]
when:
- ansible_distribution_version | float < 8.6
- ansible_distribution_version is version('8.6', '<')
- ansible_distribution_major_version == '8'
- ansible_distribution == 'RedHat'
changed_when: false
Expand All @@ -13,6 +17,8 @@
package:
name: hostapd
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Create directory for test certificates
file:
Expand Down
2 changes: 2 additions & 0 deletions tests/tasks/setup_mock_wifi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
- NetworkManager
- wpa_supplicant
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Ensure NetworkManager is running
service:
Expand Down
4 changes: 3 additions & 1 deletion tests/tasks/setup_mock_wifi_wpa3_owe.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- NetworkManager
- wpa_supplicant
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Check if can test on CentOS 8 and setup if possible
when:
Expand All @@ -16,7 +18,7 @@
# if using rpm ostree - so just skip this test
- name: Check if rpm ostree system - cannot test
meta: end_host
when: ansible_facts.pkg_mgr == "ansible.posix.rhel_rpm_ostree"
when: __network_is_ostree | d(false)

# yamllint disable rule:line-length
# Even though hostapd can be installed via EPEL 8, Opportunistic Wireless Encryption
Expand Down
4 changes: 3 additions & 1 deletion tests/tasks/setup_mock_wifi_wpa3_sae.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
- NetworkManager
- wpa_supplicant
state: present
use: "{{ (__network_is_ostree | d(false)) |
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"

- name: Configure CentOS 8 system for testing, if possible
when:
Expand All @@ -16,7 +18,7 @@
# if using rpm ostree - so just skip this test
- name: Check if rpm ostree system - cannot test
meta: end_host
when: ansible_facts.pkg_mgr == "ansible.posix.rhel_rpm_ostree"
when: __network_is_ostree | d(false)

# yamllint disable rule:line-length
# Even though hostapd can be installed via EPEL 8, Simultaneous Authentication
Expand Down
Loading

0 comments on commit 0c590cd

Please sign in to comment.