Skip to content

Commit

Permalink
sap_swpm: Prioritize sap_swpm_templates_install_dictionary members
Browse files Browse the repository at this point in the history
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 <bfinger@redhat.com>
  • Loading branch information
berndfinger committed Sep 19, 2024
1 parent 95ef236 commit c012680
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 42 deletions.
8 changes: 4 additions & 4 deletions roles/sap_swpm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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`:
Expand Down
66 changes: 28 additions & 38 deletions roles/sap_swpm/tasks/pre_install.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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"
Expand All @@ -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"
Expand All @@ -47,82 +49,70 @@
- 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'] |
d(sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_role_parameters_dict']) }}"
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([]) }}"
Expand Down

0 comments on commit c012680

Please sign in to comment.