From c0126806309d4d78d973c9a11061d55be03ea829 Mon Sep 17 00:00:00 2001 From: Bernd Finger Date: Thu, 19 Sep 2024 16:02:32 +0200 Subject: [PATCH] sap_swpm: Prioritize sap_swpm_templates_install_dictionary members Due to the use of global variables in sap_swpm, there appears to be no simple way of giving priority to top level vars over the same variables defined in sap_swpm_templates_install_dictionary. We can also assume that if using sap_swpm_templates_install_dictionary, this is done on purpose and with the intention to use its members with priority over top level variables. Signed-off-by: Bernd Finger --- roles/sap_swpm/README.md | 8 ++-- roles/sap_swpm/tasks/pre_install.yml | 66 ++++++++++++---------------- 2 files changed, 32 insertions(+), 42 deletions(-) diff --git a/roles/sap_swpm/README.md b/roles/sap_swpm/README.md index 6d033fb4..69acdc4d 100644 --- a/roles/sap_swpm/README.md +++ b/roles/sap_swpm/README.md @@ -111,12 +111,12 @@ previous `default_templates` mode. The dictionary named `sap_swpm_templates_install_dictionary` can hold all necessary variables needed by the role `sap_swpm` for different SAP products. See the `Ansible Playbooks for SAP` for examples. -The role `sap_swpm` redefines low level members of this dictionary to top level -variables. The dictionary may contain either the definitions for the previous -version of the role (used in the previous role modes `default_templates` and `advanced_templates`), +The role `sap_swpm` defines top level variables from low level members of this dictionary. +The dictionary may contain either the definitions for the previous version of the role +(used in the previous role modes `default_templates` and `advanced_templates`), or an updated definition of the dictionary containing the new variable names. The role will fail if both old and new variable names are defined in the dictionary. -Variables on the top level take precedences over those members of `sap_swpm_templates_install_dictionary` with the same name. +Variables on the top level will be overridden by members of `sap_swpm_templates_install_dictionary` with the same name. Following is the complete list of conversions of members of the dictionary `sap_swpm_templates_install_dictionary`, where `sap_prod` is an example entry for `sap_swpm_templates_product_input`: diff --git a/roles/sap_swpm/tasks/pre_install.yml b/roles/sap_swpm/tasks/pre_install.yml index 304a4987..3e843656 100644 --- a/roles/sap_swpm/tasks/pre_install.yml +++ b/roles/sap_swpm/tasks/pre_install.yml @@ -17,6 +17,7 @@ - sap_swpm_inifile_custom_values_dictionary is defined tags: always +# Note: Variable definitions in sap_swpm_templates_install_dictionary will overwrite existing top level definitions. - name: SAP SWPM Pre Install - Define variables from 'sap_swpm_templates_install_dictionary' if present when: sap_swpm_templates_install_dictionary is defined tags: always @@ -26,8 +27,9 @@ ansible.builtin.debug: msg: - "sap_swpm_templates_product_input: >{{ sap_swpm_templates_product_input }}<" + ignore_errors: true - - name: SAP SWPM Pre Install - Ensure that only one of 'sap_swpm_inifile_sections_list' and 'sap_swpm_inifile_list' is defined in the dict + - name: SAP SWPM Pre Install - Fail if 'sap_swpm_inifile_sections_list' and 'sap_swpm_inifile_list' are both defined in the dict ansible.builtin.fail: msg: - "FAIL: In 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]', both" @@ -37,7 +39,7 @@ - sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_sections_list'] is defined - sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_list'] is defined - - name: SAP SWPM Pre Install - Ensure that only one of 'sap_swpm_inifile_parameters_dict' or 'sap_swpm_inifile_custom_values_dictionary' is defined in the dict + - name: SAP SWPM Pre Install - Fail if 'sap_swpm_inifile_parameters_dict' and 'sap_swpm_inifile_custom_values_dictionary' are both defined in the dict ansible.builtin.fail: msg: - "FAIL: In 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]', both" @@ -47,27 +49,27 @@ - sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_parameters_dict'] is defined - sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_custom_values_dictionary'] is defined -# Define sap_swpm_product_catalog_id from low level member, or use top level variable if defined. Top level variable has precedence. +# Unconditionally define sap_swpm_product_catalog_id from low level member. - name: SAP SWPM Pre Install - Define 'sap_swpm_product_catalog_id' from low level member with the same name ansible.builtin.set_fact: - sap_swpm_product_catalog_id: "{{ sap_swpm_product_catalog_id | - d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_product_catalog_id']) }}" +# sap_swpm_product_catalog_id: "{{ sap_swpm_product_catalog_id | +# d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_product_catalog_id']) }}" + sap_swpm_product_catalog_id: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_product_catalog_id'] }}" when: sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_product_catalog_id'] is defined - - name: SAP SWPM Pre Install - Display sap_swpm_product_catalog_id + - name: SAP SWPM Pre Install - Display 'sap_swpm_product_catalog_id' ansible.builtin.debug: msg: "sap_swpm_product_catalog_id: >{{ sap_swpm_product_catalog_id }}<" -# Define sap_swpm_inifile_sections_list from low level member, or use top level variable if defined. Top level variable has precedence. +# Unconditionally define sap_swpm_inifile_sections_list from low level member. - name: SAP SWPM Pre Install - Define 'sap_swpm_inifile_sections_list' from low level member ansible.builtin.set_fact: - sap_swpm_inifile_sections_list: "{{ sap_swpm_inifile_sections_list | - d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_list']) | + sap_swpm_inifile_sections_list: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_list'] | d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_sections_list']) }}" when: sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_list'] is defined or sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_sections_list'] is defined -# Define sap_swpm_role_parameters_dict from low level member. Top level variable does not exist. +# Define sap_swpm_role_parameters_dict from low level member. - name: SAP SWPM Pre Install - Define 'sap_swpm_role_parameters_dict' from low level member ansible.builtin.set_fact: sap_swpm_role_parameters_dict: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_dictionary'] | @@ -75,54 +77,42 @@ when: sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_dictionary'] is defined or sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_role_parameters_dict'] is defined -# Define role variables from low level member, looping over 'sap_swpm_role_parameters_dict': + - name: SAP SWPM Pre Install - Display 'sap_swpm_role_parameters_dict' + ansible.builtin.debug: + msg: "sap_swpm_role_parameters_dict: >{{ sap_swpm_role_parameters_dict }}<" + when: sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict + ignore_errors: true + +# Define role variables from low level member, looping over 'sap_swpm_role_parameters_dict'. # Reason for noqa: We are setting variables from the content of a dict, and the variable names are in the dict keys. - name: SAP SWPM Pre Install - Define role variables from low level member, looping over 'sap_swpm_role_parameters_dict' # noqa var-naming[no-jinja] ansible.builtin.set_fact: - "{{ line_item.key }}": "{{ lookup('vars', line_item.key, default=line_item.value) }}" + "{{ line_item.key }}": "{{ line_item.value }}" loop: "{{ sap_swpm_role_parameters_dict | dict2items if sap_swpm_role_parameters_dict is mapping else [] }}" loop_control: loop_var: line_item - when: sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict + when: + - sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict - - name: SAP SWPM Pre Install - Display all vars of 'sap_swpm_role_parameters_dict' + - name: SAP SWPM Pre Install - Display all vars and values of 'sap_swpm_role_parameters_dict' ansible.builtin.debug: - msg: "{{ line_item.key }}: {{ lookup('vars', line_item.key) }}" + msg: "{{ line_item.key }}: >{{ lookup('vars', line_item.key) }}<" loop: "{{ sap_swpm_role_parameters_dict | dict2items if sap_swpm_role_parameters_dict is mapping else [] }}" loop_control: loop_var: line_item when: sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict - - - name: SAP SWPM Pre Install - Looking for any differences... - ansible.builtin.set_fact: - different_values_sap_swpm_role_parameters_dict: - "{{ different_values_sap_swpm_role_parameters_dict | d({}) | combine({line_item.key: line_item.value}) }}" - loop: "{{ sap_swpm_role_parameters_dict | dict2items if sap_swpm_role_parameters_dict is mapping else [] }}" - loop_control: - loop_var: line_item - when: - - sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict - - line_item.value != lookup('vars', line_item.key) ignore_errors: true - - name: SAP SWPM Pre Install - Display any differences - ansible.builtin.debug: - msg: - - "Note: The following values from" - - "sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_role_parameters_dict']" - - "are ignored because they have already been defined on the top level, as follows:" - - "{{ different_values_sap_swpm_role_parameters_dict }}" - when: sap_swpm_role_parameters_dict is defined and sap_swpm_role_parameters_dict - -# Define sap_swpm_inifile_parameters_dict from low level member, or use top level variable if defined. Top level variable has precedence. +# Define sap_swpm_inifile_parameters_dict from low level member, or use top level variable if defined. Low level definition has precedence. - name: SAP SWPM Pre Install - Define 'sap_swpm_inifile_parameters_dict' from low level member ansible.builtin.set_fact: - sap_swpm_inifile_parameters_dict: "{{ sap_swpm_inifile_parameters_dict | - d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_custom_values_dictionary']) | + sap_swpm_inifile_parameters_dict: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_custom_values_dictionary'] | d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_parameters_dict']) }}" when: sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_custom_values_dictionary'] is defined or sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_parameters_dict'] is defined +# The following task ensures that the two main inifile creation parameters are defined. +# This is necessary when creating the file 'inifile.params'. - name: SAP SWPM Pre Install - Define the main inifile creation parameters as empty if necessary ansible.builtin.set_fact: sap_swpm_inifile_sections_list: "{{ sap_swpm_inifile_sections_list | d([]) }}"