Skip to content

Commit

Permalink
sap_swpm: Recognize sap_swpm_templates_install_dictionary
Browse files Browse the repository at this point in the history
Signed-off-by: Bernd Finger <[email protected]>
  • Loading branch information
berndfinger committed Sep 9, 2024
1 parent a7875d7 commit 06c1739
Show file tree
Hide file tree
Showing 2 changed files with 162 additions and 37 deletions.
74 changes: 40 additions & 34 deletions roles/sap_swpm/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,47 +80,59 @@ The following role parameter is no longer used because there are no role `modes`

#### sap_swpm_ansible_role_mode

The following two role parameters have been renamed. If the new variables are not defined, the old ones are converted to the new ones.
The following two role parameters have been renamed. If the new variables are undefined and the old ones are defined,
the old ones are converted to the new ones.

#### sap_swpm_inifile_list -> sap_swpm_inifile_sections_list

**Previous name**: sap_swpm_inifile_list\
**New name**: sap_swpm_inifile_sections_list\
**Previous name**: `sap_swpm_inifile_list`\
**New name**: `sap_swpm_inifile_sections_list`\
**Reason**: This variable contains sections of the sapinst input file, `inifile.params`.
The new variable name is reflecting this purpose.

#### sap_swpm_inifile_custom_values_dictionary -> sap_swpm_inifile_parameters_dict

**Previous name**: sap_swpm_inifile_custom_values_dictionary\
**New name**: sap_swpm_inifile_parameters_dict\
**Previous name**: `sap_swpm_inifile_custom_values_dictionary`\
**New name**: `sap_swpm_inifile_parameters_dict`\
**Reason**: This variable contains parameter names and values of the
sapinst input file, `inifile.params`. The new variable name is reflecting this purpose.

#### Migration from the `*_templates` modes of the previous version of `sap_swpm`
#### sap_swpm_inifile_dictionary -> sap_swpm_role_parameters_dict

The role `sap_swpm` does no longer use the dictionary `sap_swpm_templates_install_dictionary`.
This dictionary was used in the previous role modes `default_templates` and `advanced_templates`.
**Previous name**: `sap_swpm_inifile_dictionary`\
**New name**: `sap_swpm_role_parameters_dict`\
**Reason**: This dictionary contains parameter names and values of the role `sap_swpm`.
The new variable name is reflecting this purpose.\
**Note**: This variable was only used as a member of `sap_swpm_templates_install_dictionary`, in the
previous `default_templates` mode.

Because of this change, required low level members of `sap_swpm_templates_install_dictionary` have to be
redefined to top level variables. Creating top level variables from low level members
of a dict can be done:
#### Using sap_swpm_templates_install_dictionary

- in a separate task using `ansible.builtin.set_fact` before calling `sap_swpm`, or
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.

- in the task calling `sap_swpm` with a `vars`: section of the task calling `sap_swpm`.
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`),
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.

Example:
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`:
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_product_catalog_id']` -> `sap_swpm_product_catalog_id`
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_inifile_sections_list']` -> `sap_swpm_inifile_sections_list`
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_inifile_parameters_dict']` -> `sap_swpm_inifile_parameters_dict`
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_role_parameters_dict']` -> `sap_swpm_role_parameters_dict` -> top level role variables\
Example: `sap_swpm_install_saphostagent: 'true'`

```
- name: Execute the role sap_swpm
ansible.builtin.include_role:
name: community.sap_install.sap_swpm
vars:
sap_swpm_templates_product_input: "sap_s4hana_2023_distributed_nwas_ascs_ha"
sap_swpm_product_catalog_id: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_product_catalog_id'] }}"
sap_swpm_inifile_sections_list: "{{ sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]['sap_swpm_inifile_list'] }}"
```
Former `default_templates` mode:
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_inifile_list']` -> `sap_swpm_inifile_sections_list`
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_inifile_dictionary']` -> top level role variables\
Example: `sap_swpm_install_saphostagent: 'true'`

Former `advanced_templates` mode:
- `sap_swpm_templates_install_dictionary[sap_prod]['sap_swpm_inifile_custom_values_dictionary']` -> `sap_swpm_inifile_parameters_dict`

## Execution

Expand Down Expand Up @@ -170,23 +182,17 @@ Sample Ansible Playbook Execution

- If such a file does *not* exist, the role will create an SAP SWPM `inifile.params` file by one of the following methods:

Method 1: Predefined sections of the file `inifile_params.j2` will be used to create the file `inifile.params`.
The variable `sap_swpm_inifile_sections_list` contains a list of sections which will part of the file `inifile.params`.
All other sections will be ignored. The inifile parameters themselves will be set according to other role parameters.
Example: The inifile parameter `archives.downloadBasket` will be set to the content of the role parameter
`sap_swpm_software_path`.
Method 1: Predefined sections of the file `inifile_params.j2` will be used to create the file `inifile.params`. The variable `sap_swpm_inifile_sections_list` contains a list of sections which will part of the file `inifile.params`. All other sections will be ignored. The inifile parameters themselves will be set according to other role parameters. Example: The inifile parameter `archives.downloadBasket` will be set to the content of the role parameter `sap_swpm_software_path`.

Method 2: The file `inifile.params` will be configured from the content of the dictionary `sap_swpm_inifile_parameters_dict`.
This dictionary is defined like in the following example:
Method 2: The file `inifile.params` will be configured from the content of the dictionary `sap_swpm_inifile_parameters_dict`. This dictionary is defined like in the following example:

```
sap_swpm_inifile_parameters_dict:
archives.downloadBasket: /software/download_basket
NW_getFQDN.FQDN: poc.cloud
NW_getFQDN.FQDN: example.com
```
It is also possible to use method 1 for creating the inifile and then replace or set additional variables using method 2:
Just define both of the related parameters, `sap_swpm_inifile_sections_list` and `sap_swpm_inifile_parameters_dict`.
It is also possible to use method 1 for creating the inifile and then replace or set additional variables using method 2: Define both of the related parameters, `sap_swpm_inifile_sections_list` and `sap_swpm_inifile_parameters_dict`.
- The file `inifile.params` is then transferred to a temporary directory on the managed node, to be used by the sapinst process.
Expand Down
125 changes: 122 additions & 3 deletions roles/sap_swpm/tasks/pre_install.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,129 @@
# SPDX-License-Identifier: Apache-2.0
---

- name: SAP SWPM Pre Install - Rename variables
- name: SAP SWPM Pre Install - Define 'sap_swpm_inifile_sections_list' from 'sap_swpm_inifile_list' if necessary
ansible.builtin.set_fact:
sap_swpm_inifile_sections_list: "{{ sap_swpm_inifile_list | d(sap_swpm_inifile_sections_list | d([])) }}"
sap_swpm_inifile_parameters_dict: "{{ sap_swpm_inifile_custom_values_dictionary | d(sap_swpm_inifile_parameters_dict) | d({}) }}"
sap_swpm_inifile_sections_list: "{{ sap_swpm_inifile_list }}"
when:
- sap_swpm_inifile_sections_list is undefined
- sap_swpm_inifile_list is defined
tags: always

- name: SAP SWPM Pre Install - Define 'sap_swpm_inifile_parameters_dict' from 'sap_swpm_inifile_custom_values_dictionary' if necessary
ansible.builtin.set_fact:
sap_swpm_inifile_parameters_dict: "{{ sap_swpm_inifile_custom_values_dictionary }}"
when:
- sap_swpm_inifile_parameters_dict is undefined
- sap_swpm_inifile_custom_values_dictionary is defined
tags: always

- 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
block:

- name: SAP SWPM Pre Install - Display 'sap_swpm_templates_product_input'
ansible.builtin.debug:
msg:
- "sap_swpm_templates_product_input: >{{ sap_swpm_templates_product_input }}<"
- 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
ansible.builtin.fail:
msg:
- "FAIL: In 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]', both"
- "'sap_swpm_inifile_sections_list' and 'sap_swpm_inifile_list' are defined!"
- "Only one of these variables may be defined in 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]'."
when:
- 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
ansible.builtin.fail:
msg:
- "FAIL: In 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]', both"
- "'sap_swpm_inifile_parameters_dict', and 'sap_swpm_inifile_custom_values_dictionary', are defined!"
- "Only one of these variables may be defined in 'sap_swpm_templates_install_dictionary[sap_swpm_templates_product_input]'."
when:
- 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.
- 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']) }}"
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
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.
- 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']) |
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.
- 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':
# 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) }}"
loop: "{{ sap_swpm_role_parameters_dict | dict2items }}"
loop_control:
loop_var: line_item
when: sap_swpm_role_parameters_dict is defined

- name: SAP SWPM Pre Install - Display all vars of 'sap_swpm_role_parameters_dict'
ansible.builtin.debug:
msg: "{{ line_item.key }}: {{ lookup('vars', line_item.key) }}"
loop: "{{ sap_swpm_role_parameters_dict | dict2items }}"
loop_control:
loop_var: line_item

- 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 }}"
loop_control:
loop_var: line_item
when:
- 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 }}"

# Define sap_swpm_inifile_parameters_dict from low level member, or use top level variable if defined. Top level variable 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']) |
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

- 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([]) }}"
sap_swpm_inifile_parameters_dict: "{{ sap_swpm_inifile_parameters_dict | d({}) }}"
tags: always

################
Expand Down

0 comments on commit 06c1739

Please sign in to comment.