From 1533505e71a00c7e2d2b17bd22537440e6372bb2 Mon Sep 17 00:00:00 2001 From: Erik Berg Date: Sun, 4 Sep 2022 08:22:09 +0200 Subject: [PATCH] Refactor use of include_vars Move all these os-default files to vars directory, where the include_vars expects them to be. Use a first_found lookup instead of a with_first_found loop so that the 'paths' parameter can be used. This ensures that only vars from the role are included, and not vars from a parent calling role. This can happen when a parent role has a higher priority vars file available for inclusion than the role it calls. The lookup order was taken from the openstack-ansible project, and can be extended by copying the family vars file to a more specific distro or versioned file and making necessary the changes, should the need to complicate things ever arise again. Change-Id: Ic3c0a072fc8d4cb10f6431c118f14afba3a3da1f --- .../defaults/dummy-defaults.yml | 3 -- .../bifrost-ironic-install/tasks/main.yml | 33 +++++++------------ .../debian.yml} | 0 .../redhat.yml} | 0 4 files changed, 12 insertions(+), 24 deletions(-) delete mode 100644 playbooks/roles/bifrost-ironic-install/defaults/dummy-defaults.yml rename playbooks/roles/bifrost-ironic-install/{defaults/required_defaults_Debian_family.yml => vars/debian.yml} (100%) rename playbooks/roles/bifrost-ironic-install/{defaults/required_defaults_RedHat_family.yml => vars/redhat.yml} (100%) diff --git a/playbooks/roles/bifrost-ironic-install/defaults/dummy-defaults.yml b/playbooks/roles/bifrost-ironic-install/defaults/dummy-defaults.yml deleted file mode 100644 index c1a20659d..000000000 --- a/playbooks/roles/bifrost-ironic-install/defaults/dummy-defaults.yml +++ /dev/null @@ -1,3 +0,0 @@ ---- -# NOTE(cinerama) This file is intentionally left blank - do not -# add variables here. diff --git a/playbooks/roles/bifrost-ironic-install/tasks/main.yml b/playbooks/roles/bifrost-ironic-install/tasks/main.yml index a65d98168..94f2a610d 100644 --- a/playbooks/roles/bifrost-ironic-install/tasks/main.yml +++ b/playbooks/roles/bifrost-ironic-install/tasks/main.yml @@ -13,27 +13,18 @@ # See the License for the specific language governing permissions and # limitations under the License. --- -# NOTE(cinerama) dummy-defaults.yml is an empty defaults file. We use it -# here to ensure that with_first_found won't fail should we not have -# defaults for a particular distribution, version, etc. -- name: Include OS family-specific defaults - include_vars: "{{ item }}" - with_first_found: - - "../defaults/required_defaults_{{ ansible_os_family }}_family.yml" - - "../defaults/dummy-defaults.yml" - -- name: Include OS distribution-specific defaults - include_vars: "{{ item }}" - with_first_found: - - "../defaults/required_defaults_{{ ansible_distribution | regex_replace(' ', '_') }}.yml" - - "../defaults/dummy-defaults.yml" - -- name: Include OS version-specific defaults - include_vars: "{{ item }}" - with_first_found: - - "../defaults/required_defaults_{{ ansible_distribution | regex_replace(' ', '_') }}_{{ ansible_distribution_release }}.yml" - - "../defaults/required_defaults_{{ ansible_distribution | regex_replace(' ', '_') }}_{{ ansible_distribution_version }}.yml" - - "../defaults/dummy-defaults.yml" +- name: Gather variables for each operating system + include_vars: "{{ lookup('first_found', params) }}" + vars: + params: + files: + - "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_version'] | lower }}.yml" + - "{{ ansible_facts['distribution'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml" + - "{{ ansible_facts['os_family'] | lower }}-{{ ansible_facts['distribution_major_version'] | lower }}.yml" + - "{{ ansible_facts['distribution'] | lower }}.yml" + - "{{ ansible_facts['os_family'] | lower }}.yml" + paths: + - "{{ role_path }}/vars" - name: "Install Ironic deps" include_tasks: install.yml diff --git a/playbooks/roles/bifrost-ironic-install/defaults/required_defaults_Debian_family.yml b/playbooks/roles/bifrost-ironic-install/vars/debian.yml similarity index 100% rename from playbooks/roles/bifrost-ironic-install/defaults/required_defaults_Debian_family.yml rename to playbooks/roles/bifrost-ironic-install/vars/debian.yml diff --git a/playbooks/roles/bifrost-ironic-install/defaults/required_defaults_RedHat_family.yml b/playbooks/roles/bifrost-ironic-install/vars/redhat.yml similarity index 100% rename from playbooks/roles/bifrost-ironic-install/defaults/required_defaults_RedHat_family.yml rename to playbooks/roles/bifrost-ironic-install/vars/redhat.yml