Skip to content

Commit 3bd8983

Browse files
committed
refactor: improve support for ostree systems
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 <[email protected]>
1 parent b91af7f commit 3bd8983

13 files changed

+45
-55
lines changed

.ansible-lint

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,5 +22,3 @@ exclude_paths:
2222
- examples/roles/
2323
mock_roles:
2424
- linux-system-roles.ssh
25-
mock_modules:
26-
- ansible.utils.update_fact

.ostree/get_ostree_data.sh

Lines changed: 20 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
1-
#!/bin/bash
1+
#!/usr/bin/env bash
22

33
set -euo pipefail
44

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

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

3130
get_rolepath() {
32-
local ostree_dir role rolesdir roles_parent_dir
31+
local ostree_dir role rolesdir roles_parent_dir coll_path pth
3332
ostree_dir="$1"
3433
role="$2"
3534
roles_parent_dir="$(dirname "$(dirname "$ostree_dir")")"
@@ -47,16 +46,22 @@ get_rolepath() {
4746
fi
4847
done
4948
# look elsewhere
50-
if [ -n "${ANSIBLE_COLLECTIONS_PATHS:-}" ]; then
51-
for pth in ${ANSIBLE_COLLECTIONS_PATHS//:/ }; do
52-
rolesdir="$pth/ansible_collections/$role_collection_dir/roles/$role/.ostree"
53-
if [ -d "$rolesdir" ]; then
54-
echo "$rolesdir"
55-
return 0
56-
fi
49+
coll_path="${ANSIBLE_COLLECTIONS_PATH:-}"
50+
if [ -z "$coll_path" ]; then
51+
coll_path="${ANSIBLE_COLLECTIONS_PATHS:-}"
52+
fi
53+
if [ -n "${coll_path}" ]; then
54+
for pth in ${coll_path//:/ }; do
55+
for rolesdir in "$pth"/ansible_collections/*/*_system_roles/roles/"$role"/.ostree; do
56+
if [ -d "$rolesdir" ]; then
57+
echo "$rolesdir"
58+
return 0
59+
fi
60+
done
5761
done
5862
fi
59-
return 1
63+
1>&2 echo ERROR - could not find role "$role" - please use ANSIBLE_COLLECTIONS_PATH
64+
exit 2
6065
}
6166

6267
get_packages() {
@@ -75,6 +80,10 @@ get_packages() {
7580
roles="$(cat "$rolefile")"
7681
for role in $roles; do
7782
rolepath="$(get_rolepath "$ostree_dir" "$role")"
83+
if [ -z "$rolepath" ]; then
84+
1>&2 echo ERROR - could not find role "$role" - please use ANSIBLE_COLLECTIONS_PATH
85+
exit 2
86+
fi
7887
get_packages "$rolepath"
7988
done
8089
fi

.sanity-ansible-ignore-2.12.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.sanity-ansible-ignore-2.13.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.sanity-ansible-ignore-2.14.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.sanity-ansible-ignore-2.15.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

.sanity-ansible-ignore-2.9.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

meta/collection-requirements.yml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,3 @@
22
---
33
collections:
44
- ansible.posix
5-
- ansible.utils

tasks/main.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
package:
77
name: "{{ __ssh_packages + ssh_additional_packages }}"
88
state: present
9+
use: "{{ (__ssh_is_ostree | d(false)) |
10+
ternary('ansible.posix.rhel_rpm_ostree', omit) }}"
911

1012
- name: Gather information about the user for user configuration
1113
getent:

tasks/set_vars.yml

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,23 +5,17 @@
55
when: __ssh_required_facts |
66
difference(ansible_facts.keys() | list) | length > 0
77

8-
- name: Ensure correct package manager for ostree systems
9-
vars:
10-
ostree_pkg_mgr: ansible.posix.rhel_rpm_ostree
11-
ostree_booted_file: /run/ostree-booted
12-
when: ansible_facts.pkg_mgr | d("") != ostree_pkg_mgr
8+
- name: Determine if system is ostree and set flag
9+
when: not __ssh_is_ostree is defined
1310
block:
1411
- name: Check if system is ostree
1512
stat:
16-
path: "{{ ostree_booted_file }}"
13+
path: /run/ostree-booted
1714
register: __ostree_booted_stat
1815

19-
- name: Set package manager to use for ostree
20-
ansible.utils.update_fact:
21-
updates:
22-
- path: ansible_facts.pkg_mgr
23-
value: "{{ ostree_pkg_mgr }}"
24-
when: __ostree_booted_stat.stat.exists
16+
- name: Set flag to indicate system is ostree
17+
set_fact:
18+
__ssh_is_ostree: "{{ __ostree_booted_stat.stat.exists }}"
2519

2620
- name: Set platform/version specific variables
2721
include_vars: "{{ __vars_file }}"

0 commit comments

Comments
 (0)