From 2b17fe73e95c7e57f3ca9415a9fcbb6388a22cc6 Mon Sep 17 00:00:00 2001 From: Rich Megginson Date: Tue, 12 Apr 2022 14:03:45 -0600 Subject: [PATCH] support playbooks which use gather_facts: false Some users prefer to use `gather_facts: false` in their playbooks. However, the network role requires certain ansible_facts to be set. If the user wants to use the network role with `gather_facts: false`, the role will gather the minimum subset of facts required. If the user does not want the role to gather facts, the user can either not use the network role, or ensure that all required facts are in the facts cache. Signed-off-by: Rich Megginson --- defaults/main.yml | 8 ++++++++ tasks/main.yml | 7 +++++++ tests/tests_default.yml | 1 + 3 files changed, 16 insertions(+) diff --git a/defaults/main.yml b/defaults/main.yml index 6d3362a..6c3754d 100644 --- a/defaults/main.yml +++ b/defaults/main.yml @@ -121,3 +121,11 @@ network_packages: "{{ __network_provider_setup[network_provider]['packages'] }}" network_service_name: "{{ __network_provider_setup[network_provider]['service_name'] }}" + +# ansible_facts required by the role +__network_required_facts: + - distribution + - distribution_major_version + - distribution_version + - os_family + - python diff --git a/tasks/main.yml b/tasks/main.yml index 4e94d5c..78881f7 100644 --- a/tasks/main.yml +++ b/tasks/main.yml @@ -1,6 +1,13 @@ # SPDX-License-Identifier: BSD-3-Clause # get service facts, used in defaults/main.yml --- +- name: Ensure ansible_facts used by role + setup: + gather_subset: min + when: not ansible_facts.keys() | list | + intersect(__network_required_facts) == __network_required_facts + no_log: true + - name: Check which services are running service_facts: no_log: true diff --git a/tests/tests_default.yml b/tests/tests_default.yml index e196314..534c634 100644 --- a/tests/tests_default.yml +++ b/tests/tests_default.yml @@ -2,6 +2,7 @@ --- - name: Test executing the role with default parameters hosts: all + gather_facts: false roles: - linux-system-roles.network tasks: