Skip to content

Commit

Permalink
fix: facts being gathered unnecessarily
Browse files Browse the repository at this point in the history
Cause: The comparison of the present facts with the required facts is
being done on unsorted lists.

Consequence: The comparison may fail if the only difference is the
order.  Facts are gathered unnecessarily.

Fix: Use `difference` which works no matter what the order is.  Ensure
that the fact gathering subsets used are the absolute minimum required.

Result: The role gathers only the facts it requires, and does
not unnecessarily gather facts.

Signed-off-by: Rich Megginson <[email protected]>
  • Loading branch information
richm committed Jul 13, 2023
1 parent 21383dc commit 99652c3
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 4 deletions.
6 changes: 3 additions & 3 deletions tasks/set_vars.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
- name: Ensure ansible_facts used by role
setup:
gather_subset: min
when: not ansible_facts.keys() | list |
intersect(__template_required_facts) == __template_required_facts
gather_subset: "{{ __systemd_required_facts_subsets }}"
when: __systemd_required_facts |
difference(ansible_facts.keys() | list) | length > 0

- name: Set platform/version specific variables
include_vars: "{{ __vars_file }}"
Expand Down
7 changes: 6 additions & 1 deletion vars/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,13 @@ __template_foo_config: foo.conf
__template_packages: []
__template_services: []
# ansible_facts required by the role
__template_required_facts:
__systemd_required_facts:
- distribution
- distribution_major_version
- distribution_version
- os_family
# the subsets of ansible_facts that need to be gathered in case any of the
# facts in required_facts is missing; see the documentation of
# the 'gather_subset' parameter of the 'setup' module
__systemd_required_facts_subsets: "{{ ['!all', '!min'] +
__systemd_required_facts }}"

0 comments on commit 99652c3

Please sign in to comment.