Skip to content

Commit

Permalink
Update variable names for simpler understanding
Browse files Browse the repository at this point in the history
  • Loading branch information
marcelmamula committed Sep 26, 2024
1 parent c2699a9 commit ee0d67a
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 57 deletions.
24 changes: 16 additions & 8 deletions roles/sap_vm_temp_vip/INPUT_PARAMETERS.md
Original file line number Diff line number Diff line change
@@ -1,34 +1,42 @@
## Input Parameters for sap_vm_temp_vip Ansible Role
<!-- BEGIN Role Input Parameters -->
### sap_vm_temp_vip_primary_ip
### sap_vm_temp_vip_default_ip

- _Type:_ `string`
- _Default:_ `ansible_default_ipv4.address`

Primary IP on default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.
IP Address of default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.

### sap_vm_temp_vip_primary_netmask
### sap_vm_temp_vip_default_netmask

- _Type:_ `string`
- _Default:_ `ansible_default_ipv4.netmask`

Netmask of primary IP on default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.
Netmask of default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.

### sap_vm_temp_vip_primary_prefix
### sap_vm_temp_vip_default_prefix

- _Type:_ `string`
- _Default:_ `ansible_default_ipv4.prefix`

Prefix of primary IP on default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.
Prefix of default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.

### sap_vm_temp_vip_primary_broadcast
### sap_vm_temp_vip_default_broadcast

- _Type:_ `string`
- _Default:_ `ansible_default_ipv4.broadcast`

Broadcast of primary IP on default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.</br>
Broadcast of default network interface is obtained from Ansible Facts and it is used for calculation of missing input parameters.</br>
This parameter is empty on some cloud platforms and VIP is created without broadcast if attempt to calculate fails.

### sap_vm_temp_vip_default_interface

- _Type:_ `string`
- _Default:_ `ansible_default_ipv4.interface` or `eth0`

Default Network Interface name is obtained from Ansible Facts and it is used for calculation of missing input parameters.</br>
Ensure to use correct Network Interface if default interface from Ansible Facts does not represent desired Network Interface.

### sap_vm_temp_vip_hana_primary
- _Type:_ `string`
- _Default:_ `sap_ha_pacemaker_cluster_vip_hana_primary_ip_address`
Expand Down
9 changes: 5 additions & 4 deletions roles/sap_vm_temp_vip/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
---
# General variables that are calculated from Ansible facts
sap_vm_temp_vip_primary_ip: "{{ ansible_default_ipv4.address | default('') }}"
sap_vm_temp_vip_primary_netmask: "{{ ansible_default_ipv4.netmask | default('') }}"
sap_vm_temp_vip_primary_prefix: "{{ ansible_default_ipv4.prefix | default('') }}"
sap_vm_temp_vip_primary_broadcast: "{{ ansible_default_ipv4.broadcast | default('') }}"
sap_vm_temp_vip_default_ip: "{{ ansible_default_ipv4.address | default('') }}"
sap_vm_temp_vip_default_netmask: "{{ ansible_default_ipv4.netmask | default('') }}"
sap_vm_temp_vip_default_prefix: "{{ ansible_default_ipv4.prefix | default('') }}"
sap_vm_temp_vip_default_broadcast: "{{ ansible_default_ipv4.broadcast | default('') }}"
sap_vm_temp_vip_default_interface: "{{ ansible_default_ipv4.interface | default('eth0') }}"


# SAP specific IPs are defined from sap_install.sap_ha_pacemaker_role input variables
Expand Down
46 changes: 23 additions & 23 deletions roles/sap_vm_temp_vip/tasks/get_temp_vip_details.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
failed_when: false

# Get content of ip address show filtered by primary IP
- name: Get contents of ip address show for {{ sap_vm_temp_vip_primary_ip }}
- name: Get contents of ip address show for {{ sap_vm_temp_vip_default_ip }}
ansible.builtin.shell:
cmd: set -o pipefail && ip -oneline address show {{ __sap_vm_temp_vip_get_route.stdout }} | grep {{ sap_vm_temp_vip_primary_ip }}
cmd: set -o pipefail && ip -oneline address show {{ __sap_vm_temp_vip_get_route.stdout }} | grep {{ sap_vm_temp_vip_default_ip }}
when:
- __sap_vm_temp_vip_get_route.stdout is defined and __sap_vm_temp_vip_get_route.stdout | length > 0
register: __sap_vm_temp_vip_get_ips
Expand All @@ -19,52 +19,52 @@

# Extract prefix from netmask if it is available
# Use localhost (execution host) Python3 instead of relying on target host
- name: Calculate prefix from netmask {{ sap_vm_temp_vip_primary_netmask }}
- name: Calculate prefix from netmask {{ sap_vm_temp_vip_default_netmask }}
delegate_to: localhost
ansible.builtin.command:
cmd: >
python3 -c "import ipaddress; print(ipaddress.IPv4Network('{{ sap_vm_temp_vip_primary_ip }}/{{ sap_vm_temp_vip_primary_netmask }}', strict=False).prefixlen)"
python3 -c "import ipaddress; print(ipaddress.IPv4Network('{{ sap_vm_temp_vip_default_ip }}/{{ sap_vm_temp_vip_default_netmask }}', strict=False).prefixlen)"
when:
- sap_vm_temp_vip_primary_prefix == ''
- sap_vm_temp_vip_primary_netmask | length > 0
- sap_vm_temp_vip_default_prefix == ''
- sap_vm_temp_vip_default_netmask | length > 0
register: __sap_vm_temp_vip_get_prefix_netmask
changed_when: false
failed_when: false

# Extract prefix from primary IP on default interface if netmask is not available
# Stdout result is array instead of string. [0] is used to select only one in case of multiple results.
# [0] could be replaced by join('') but it would require duplicate record validation.
- name: Calculate prefix from IP {{ sap_vm_temp_vip_primary_ip }} if sap_vm_temp_vip_primary_netmask is empty
- name: Calculate prefix from IP {{ sap_vm_temp_vip_default_ip }} if sap_vm_temp_vip_default_netmask is empty
ansible.builtin.set_fact:
__sap_vm_temp_vip_get_prefix_ip:
"{{ (__sap_vm_temp_vip_inet[0] | basename) if __sap_vm_temp_vip_inet | length > 0 else __sap_vm_temp_vip_inet }}"
vars:
__sap_vm_temp_vip_inet: "{{ __sap_vm_temp_vip_get_ips.stdout | regex_search('inet ([0-9.]+/[0-9]+)', '\\1') }}"
when:
- sap_vm_temp_vip_primary_prefix == ''
- sap_vm_temp_vip_primary_netmask == ''
- sap_vm_temp_vip_default_prefix == ''
- sap_vm_temp_vip_default_netmask == ''
- __sap_vm_temp_vip_get_ips is defined and __sap_vm_temp_vip_get_ips.stdout is defined and __sap_vm_temp_vip_get_ips.stdout | length > 0
changed_when: false


# Combine final prefix variable based on decision below:
# 1. Always use /32 for AWS and GCP, regardless of existing prefix
# 2. Else use prefix calculated from netmask if it is available and sap_vm_temp_vip_primary_prefix is empty
# 3. Else use prefix calculated from primary IP if netmask is not available and sap_vm_temp_vip_primary_prefix is empty
# 4. Else use sap_vm_temp_vip_primary_prefix (regardless of content) to be used to skip steps.
# 2. Else use prefix calculated from netmask if it is available and sap_vm_temp_vip_default_prefix is empty
# 3. Else use prefix calculated from primary IP if netmask is not available and sap_vm_temp_vip_default_prefix is empty
# 4. Else use sap_vm_temp_vip_default_prefix (regardless of content) to be used to skip steps.
- name: Update netmask prefix variable if it was calculated
ansible.builtin.set_fact:
__sap_vm_temp_vip_primary_prefix: >-
__sap_vm_temp_vip_prefix: >-
{%- if __sap_vm_temp_vip_force_static_32 -%}
32
{%- elif sap_vm_temp_vip_primary_prefix | length == 0
{%- elif sap_vm_temp_vip_default_prefix | length == 0
and __sap_vm_temp_vip_get_prefix_netmask.stdout is defined and __sap_vm_temp_vip_get_prefix_netmask.stdout | length > 0 -%}
{{ __sap_vm_temp_vip_get_prefix_netmask.stdout }}
{%- elif sap_vm_temp_vip_primary_prefix | length == 0
{%- elif sap_vm_temp_vip_default_prefix | length == 0
and __sap_vm_temp_vip_get_prefix_ip is defined and __sap_vm_temp_vip_get_prefix_ip | length > 0 -%}
{{ __sap_vm_temp_vip_get_prefix_ip }}
{%- else -%}
{{ sap_vm_temp_vip_primary_prefix }}
{{ sap_vm_temp_vip_default_prefix }}
{%- endif -%}
vars:
__sap_vm_temp_vip_force_static_32:
Expand All @@ -75,26 +75,26 @@
# Extract broadcast IP from primary IP if it is present and ansible fact ansible_default_ipv4.broadcast is empty
# Stdout result is array instead of string. [0] is used to select only one in case of multiple results.
# [0] could be replaced by join('') but it would require duplicate record validation.
- name: Calculate broadcast IP from IP {{ sap_vm_temp_vip_primary_ip }} if sap_vm_temp_vip_primary_broadcast is empty
- name: Calculate broadcast IP from IP {{ sap_vm_temp_vip_default_ip }} if sap_vm_temp_vip_default_broadcast is empty
ansible.builtin.set_fact:
__sap_vm_temp_vip_get_broadcast_ip:
"{{ (__sap_vm_temp_vip_brd[0] | basename) if __sap_vm_temp_vip_brd | length > 0 else __sap_vm_temp_vip_brd }}"
vars:
__sap_vm_temp_vip_brd: "{{ __sap_vm_temp_vip_get_ips.stdout | regex_search('brd ([0-9.]+)', '\\1') }}"
when:
- sap_vm_temp_vip_primary_broadcast == ''
- sap_vm_temp_vip_default_broadcast == ''
- __sap_vm_temp_vip_get_ips is defined and __sap_vm_temp_vip_get_ips.stdout is defined and __sap_vm_temp_vip_get_ips.stdout | length > 0
changed_when: false

# Combine final broadcast IP based on decision below:
# 1. Use calculated broadcast from primary IP if sap_vm_temp_vip_primary_broadcast is empty
# 2. Else use sap_vm_temp_vip_primary_broadcast (regardless of content) to be used during VIP creation
# 1. Use calculated broadcast from primary IP if sap_vm_temp_vip_default_broadcast is empty
# 2. Else use sap_vm_temp_vip_default_broadcast (regardless of content) to be used during VIP creation
- name: Update broadcast IP variable if it was calculated
ansible.builtin.set_fact:
__sap_vm_temp_vip_primary_broadcast: >-
{%- if sap_vm_temp_vip_primary_broadcast | length == 0
__sap_vm_temp_vip_broadcast: >-
{%- if sap_vm_temp_vip_default_broadcast | length == 0
and __sap_vm_temp_vip_get_broadcast_ip is defined and __sap_vm_temp_vip_get_broadcast_ip | length > 0 -%}
{{ __sap_vm_temp_vip_get_broadcast_ip }}
{%- else -%}
{{ sap_vm_temp_vip_primary_broadcast }}
{{ sap_vm_temp_vip_default_broadcast }}
{%- endif -%}
8 changes: 4 additions & 4 deletions roles/sap_vm_temp_vip/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
---
# Ansible role to setup temporary Virtual IP (VIP)

- name: Assert that sap_vm_temp_vip_primary_ip is defined
- name: Assert that sap_vm_temp_vip_default_ip is defined
ansible.builtin.assert:
that: sap_vm_temp_vip_primary_ip is defined and sap_vm_temp_vip_primary_ip | length > 0
that: sap_vm_temp_vip_default_ip is defined and sap_vm_temp_vip_default_ip | length > 0
fail_msg:
- "Unable to get ansible fact ansible_default_ipv4.address or variable sap_vm_temp_vip_primary_ip is empty!"
- "Ensure that gather_facts:true is set and sap_vm_temp_vip_primary_ip is not empty."
- "Unable to get ansible fact ansible_default_ipv4.address or variable sap_vm_temp_vip_default_ip is empty!"
- "Ensure that gather_facts:true is set and sap_vm_temp_vip_default_ip is not empty."


- name: Block to ensure that only supported groups are allowed
Expand Down
32 changes: 16 additions & 16 deletions roles/sap_vm_temp_vip/tasks/set_temp_vip.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,15 @@
msg: >-
{%- if __vip_expected == __vip_found -%}
VIP address {{ __vip_expected }} is already present. VIP creation will be skipped.
{%- elif __vip_expected != __vip_found and __sap_vm_temp_vip_primary_prefix != '' and not __vip_multiple -%}
{%- elif __vip_expected != __vip_found and __sap_vm_temp_vip_prefix != '' and not __vip_multiple -%}
VIP address {{ __vip_expected }} is already present with different prefix {{ __vip_found }}. VIP creation will be skipped.
{%- elif __vip_multiple -%}
Multiple VIP address entries found. VIP creation will be skipped.
{%- else -%}
VIP address {{ __sap_vm_temp_vip_address }} is already present, but comparison failed because of empty __sap_vm_temp_vip_primary_prefix.
VIP address {{ __sap_vm_temp_vip_address }} is already present, but comparison failed because of empty sap_vm_temp_vip_default_prefix.
{%- endif -%}
vars:
__vip_expected: "{{ __sap_vm_temp_vip_address ~ '/' ~ __sap_vm_temp_vip_primary_prefix }}"
__vip_expected: "{{ __sap_vm_temp_vip_address ~ '/' ~ __sap_vm_temp_vip_prefix }}"
__vip_found: "{{ __sap_vm_temp_vip_get_vip.stdout | regex_search('inet ([0-9.]+/[0-9]+)', '\\1') | join('') if not __vip_multiple else '' }}"
__vip_multiple: "{{ true if __sap_vm_temp_vip_get_vip.stdout_lines | length > 1 else false }}"
when:
Expand All @@ -78,17 +78,17 @@
# 1. VIP address is defined based on target host group
# 2. Prefix is defined or generated using netmask or primary IP prefix
# 3. Broadcast IP is used only if it was defined or generated using primary IP broadcast
- name: Generate command for IP creation - Prefix /{{ __sap_vm_temp_vip_primary_prefix }} static IPs
- name: Generate command for IP creation - Prefix /{{ __sap_vm_temp_vip_prefix }} static IPs
ansible.builtin.set_fact:
__sap_vm_temp_vip_command: >-
{%- if __sap_vm_temp_vip_primary_broadcast | length > 0 -%}
ip address add {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_primary_prefix }} brd {{ __sap_vm_temp_vip_primary_broadcast }} dev eth0 noprefixroute
{%- if __sap_vm_temp_vip_broadcast | length > 0 -%}
ip address add {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_prefix }} brd {{ __sap_vm_temp_vip_broadcast }} dev {{ sap_vm_temp_vip_default_interface }} noprefixroute
{%- else -%}
ip address add {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_primary_prefix }} brd + dev eth0 noprefixroute
ip address add {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_prefix }} brd + dev {{ sap_vm_temp_vip_default_interface }} noprefixroute
{%- endif -%}
when:
- __sap_vm_temp_vip_address is defined and __sap_vm_temp_vip_address | length > 0
- __sap_vm_temp_vip_primary_prefix | length > 0
- __sap_vm_temp_vip_prefix | length > 0
- __sap_vm_temp_vip_get_vip.stdout is defined and __sap_vm_temp_vip_get_vip.stdout | length == 0


Expand All @@ -97,10 +97,10 @@
ansible.builtin.debug:
msg:
- "Ansible Facts:"
- primary_ip_address = {{ sap_vm_temp_vip_primary_ip }}
- primary_ip_address_netmask = {{ sap_vm_temp_vip_primary_netmask }}
- primary_ip_address_netmask_cidr_prefix = {{ __sap_vm_temp_vip_primary_prefix }}
- primary_ip_broadcast_address = {{ __sap_vm_temp_vip_primary_broadcast }}
- primary_ip_address = {{ sap_vm_temp_vip_default_ip }}
- primary_ip_address_netmask = {{ sap_vm_temp_vip_default_netmask }}
- primary_ip_address_netmask_cidr_prefix = {{ __sap_vm_temp_vip_prefix }}
- primary_ip_broadcast_address = {{ __sap_vm_temp_vip_broadcast }}
- ""
- "Command to be executed:"
- "{{ __sap_vm_temp_vip_command }}"
Expand All @@ -115,10 +115,10 @@
- "ERROR: Unable to generate command because of lacking data."
- ""
- "Please review facts below, to see which are empty or missing:"
- primary_ip_address = {{ sap_vm_temp_vip_primary_ip }}
- primary_ip_address_netmask = {{ sap_vm_temp_vip_primary_netmask }}
- primary_ip_address_netmask_cidr_prefix = {{ __sap_vm_temp_vip_primary_prefix }}
- primary_ip_broadcast_address = {{ __sap_vm_temp_vip_primary_broadcast }}
- primary_ip_address = {{ sap_vm_temp_vip_default_ip }}
- primary_ip_address_netmask = {{ sap_vm_temp_vip_default_netmask }}
- primary_ip_address_netmask_cidr_prefix = {{ __sap_vm_temp_vip_prefix }}
- primary_ip_broadcast_address = {{ __sap_vm_temp_vip_broadcast }}
when:
- __sap_vm_temp_vip_command is not defined or (__sap_vm_temp_vip_command is defined and __sap_vm_temp_vip_command | length == 0)
- __sap_vm_temp_vip_get_vip.stdout is defined and __sap_vm_temp_vip_get_vip.stdout | length == 0
Expand Down
4 changes: 2 additions & 2 deletions roles/sap_vm_temp_vip/tasks/set_temp_vip_lb_listener.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@
- __sap_vm_temp_vip_address is defined and __sap_vm_temp_vip_address | length > 0
- __sap_vm_temp_vip_port is defined and __sap_vm_temp_vip_port | length > 0
- __sap_vm_temp_vip_port_check.stdout is defined and __sap_vm_temp_vip_port_check.stdout | length == 0
- __sap_vm_temp_vip_primary_prefix | length > 0 # Dont execute if prefix was empty during VIP creation
- __sap_vm_temp_vip_prefix | length > 0 # Dont execute if prefix was empty during VIP creation
block:

# Get content of ip address show filtered by VIP - Additional execution if VIP was previously created
- name: Check if VIP is was already attached to network interface
ansible.builtin.shell:
cmd: "set -o pipefail && ip --oneline address show | grep {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_primary_prefix }}"
cmd: "set -o pipefail && ip --oneline address show | grep {{ __sap_vm_temp_vip_address }}/{{ __sap_vm_temp_vip_prefix }}"
executable: /bin/bash
register: __sap_vm_temp_vip_check_ip
changed_when: false
Expand Down

0 comments on commit ee0d67a

Please sign in to comment.