Skip to content

Commit

Permalink
Merge pull request sap-linuxlab#660 from berndfinger/use_sap_maintain…
Browse files Browse the repository at this point in the history
…_etc_hosts

sap_general_preconfigure: Use the role sap_maintain_etc_hosts
  • Loading branch information
berndfinger authored Feb 29, 2024
2 parents 7634e2e + e46628a commit 1c6348e
Show file tree
Hide file tree
Showing 11 changed files with 138 additions and 121 deletions.
14 changes: 6 additions & 8 deletions roles/sap_general_preconfigure/defaults/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -145,18 +145,16 @@ sap_general_preconfigure_kernel_parameters: "{{ __sap_general_preconfigure_kerne
sap_general_preconfigure_max_hostname_length: '13'
# The maximum length of the hostname. See SAP note 611361.

# Reason for noqa: A separate role is planned to replace the code which uses this variable.
sap_hostname: "{{ ansible_hostname }}" # noqa var-naming[no-role-prefix]
# If "global" variables are set, use those. If not, default to the values from gather_facts:
sap_general_preconfigure_ip: "{{ sap_ip | d(ansible_default_ipv4.address) }}"
# The IPV4 address to be used for updating or checking `/etc/hosts` entries.

sap_general_preconfigure_hostname: "{{ sap_hostname | d(ansible_hostname) }}"
# The hostname to be used for updating or checking `/etc/hosts` entries.

# Reason for noqa: A separate role is planned to replace the code which uses this variable.
sap_domain: "{{ ansible_domain }}" # noqa var-naming[no-role-prefix]
sap_general_preconfigure_domain: "{{ sap_domain | d(ansible_domain) }}"
# The DNS domain name to be used for updating or checking `/etc/hosts` entries.

# Reason for noqa: A separate role is planned to replace the code which uses this variable.
sap_ip: "{{ ansible_default_ipv4.address }}" # noqa var-naming[no-role-prefix]
# The IPV4 address to be used for updating or checking `/etc/hosts` entries.

# sap_general_preconfigure_db_group_name: (not defined by default)
# Use this variable to specify the name of the RHEL group which is used for the database processes.
# If defined, it will be used to configure process limits as per step
Expand Down
6 changes: 3 additions & 3 deletions roles/sap_general_preconfigure/meta/argument_specs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -310,21 +310,21 @@ argument_specs:
required: false
type: str

sap_hostname:
sap_general_preconfigure_hostname:
default: "{{ ansible_hostname }}"
description:
- The hostname to be used for updating or checking `/etc/hosts` entries.
required: false
type: str

sap_domain:
sap_general_preconfigure_domain:
default: "{{ ansible_domain }}"
description:
- The DNS domain name to be used for updating or checking `/etc/hosts` entries.
required: false
type: str

sap_ip:
sap_general_preconfigure_ip:
default: "{{ ansible_default_ipv4.address }}"
description:
- The IPV4 address to be used for updating or checking `/etc/hosts` entries.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
- name: Assert that the DNS domain is set
ansible.builtin.assert:
that: not( (ansible_domain is undefined) or (ansible_domain is none) or (ansible_domain | trim == '') )
fail_msg: "FAIL: The DNS domain is not configured! So variable 'sap_domain' needs to be configured!"
fail_msg: "FAIL: The DNS domain is not configured! So variable 'sap_general_preconfigure_domain' needs to be configured!"
success_msg: "PASS: The DNS domain is configured."
# ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"
ignore_errors: yes

- name: Assert that variable sap_domain is set
- name: Assert that variable sap_general_preconfigure_domain is set
ansible.builtin.assert:
that: not( (sap_domain is undefined) or (sap_domain is none) or (sap_domain | trim == '') )
fail_msg: "FAIL: The variable 'sap_domain' is not set!"
success_msg: "PASS: The variable 'sap_domain' is set."
that: not( (sap_general_preconfigure_domain is undefined) or (sap_general_preconfigure_domain is none) or (sap_general_preconfigure_domain | trim == '') )
fail_msg: "FAIL: The variable 'sap_general_preconfigure_domain' is not set!"
success_msg: "PASS: The variable 'sap_general_preconfigure_domain' is set."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Check if the bind-utils package, which contains the dig command, is available
Expand All @@ -21,8 +21,8 @@
fail_msg: "FAIL: The package 'bind-utils' is not installed! DNS checking not possible!"
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Check if IP address for sap_hostname.sap_domain is resolved correctly
ansible.builtin.command: dig {{ sap_hostname }}.{{ sap_domain }} +short
- name: Check if IP address for sap_general_preconfigure_hostname.sap_general_preconfigure_domain is resolved correctly
ansible.builtin.command: dig {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} +short
register: __sap_general_preconfigure_register_dig_short_assert
ignore_errors: yes
changed_when: no
Expand All @@ -35,41 +35,41 @@
success_msg: "PASS: The variable 'ansible_default_ipv4.address' is defined."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Assert that sap_ip is set
- name: Assert that sap_general_preconfigure_ip is set
ansible.builtin.assert:
that: __sap_general_preconfigure_register_dig_short_assert.stdout == sap_ip
fail_msg: "FAIL: The variable 'sap_ip' is not set!"
success_msg: "PASS: The variable 'sap_ip' is set."
that: __sap_general_preconfigure_register_dig_short_assert.stdout == sap_general_preconfigure_ip
fail_msg: "FAIL: The variable 'sap_general_preconfigure_ip' is not set!"
success_msg: "PASS: The variable 'sap_general_preconfigure_ip' is set."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"
when: "'bind-utils' in ansible_facts.packages"

### BUG: dig does not use search path in resolv.con on PPCle
- name: Check if IP address for sap_hostname with search path is resolved correctly
ansible.builtin.command: dig {{ sap_hostname }} +search +short
- name: Check if IP address for sap_general_preconfigure_hostname with search path is resolved correctly
ansible.builtin.command: dig {{ sap_general_preconfigure_hostname }} +search +short
register: __sap_general_preconfigure_register_dig_search_short_assert
changed_when: false
ignore_errors: true
when: "'bind-utils' in ansible_facts.packages"

- name: Assert that the IP address for sap_hostname is resolved correctly
- name: Assert that the IP address for sap_general_preconfigure_hostname is resolved correctly
ansible.builtin.assert:
that: __sap_general_preconfigure_register_dig_search_short_assert.stdout == sap_ip
fail_msg: "FAIL: The IP address for 'sap_hostname' could not be resolved!"
success_msg: "PASS: The IP address for 'sap_hostname' was resolved."
that: __sap_general_preconfigure_register_dig_search_short_assert.stdout == sap_general_preconfigure_ip
fail_msg: "FAIL: The IP address for 'sap_general_preconfigure_hostname' could not be resolved!"
success_msg: "PASS: The IP address for 'sap_general_preconfigure_hostname' was resolved."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(true) }}"
when: "'bind-utils' in ansible_facts.packages"

- name: Check if the reverse name resolution is correct
ansible.builtin.command: dig -x {{ sap_ip }} +short
ansible.builtin.command: dig -x {{ sap_general_preconfigure_ip }} +short
register: __sap_general_preconfigure_register_dig_reverse_assert
changed_when: false
ignore_errors: true
when: "'bind-utils' in ansible_facts.packages"

- name: Assert that the reverse name resolution is correct
ansible.builtin.assert:
that: __sap_general_preconfigure_register_dig_reverse_assert.stdout == (sap_hostname + '.' + sap_domain + '.')
fail_msg: "FAIL: The reverse name resolution of 'sap_ip' was not successful!"
success_msg: "PASS: The reverse name resolution of 'sap_ip' was successful."
that: __sap_general_preconfigure_register_dig_reverse_assert.stdout == (sap_general_preconfigure_hostname + '.' + sap_general_preconfigure_domain + '.')
fail_msg: "FAIL: The reverse name resolution of 'sap_general_preconfigure_ip' was not successful!"
success_msg: "PASS: The reverse name resolution of 'sap_general_preconfigure_ip' was successful."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(true) }}"
when: "'bind-utils' in ansible_facts.packages"
Original file line number Diff line number Diff line change
Expand Up @@ -3,88 +3,64 @@
- name: Assert - Display host and domain name, and IP address
ansible.builtin.debug:
msg:
- "sap_hostname = {{ sap_hostname }}"
- "sap_domain = {{ sap_domain }}"
- "sap_ip = {{ sap_ip }}"

# Note: There is no check related to hostname aliases.
#- name: Get all hostname aliases of {{ sap_ip }}
# shell: |
# awk '( $1 == "{{ sap_ip }}" ) {
# for (i=2; i<=NF; ++i) {
# if (( $i != "{{ sap_hostname }}" ) && ( $i != "{{ sap_hostname }}.{{ sap_domain }}" )) { printf $i" " }
# }
# }' /etc/hosts
# register: sap_base_settings_register_hostname_aliases
# changed_when: false
# check_mode: false

#- name: Print hostname aliases
# debug:
# var=sap_hostname_aliases
- "sap_general_preconfigure_hostname = {{ sap_general_preconfigure_hostname }}"
- "sap_general_preconfigure_domain = {{ sap_general_preconfigure_domain }}"
- "sap_general_preconfigure_ip = {{ sap_general_preconfigure_ip }}"

- name: Check if ipv4 address, FQDN, and hostname are once in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/{{ sap_ip }}/&&/{{ sap_hostname }}.{{ sap_domain }}/&&/{{ sap_hostname }}/{a++}END{print a}' /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/{{ sap_general_preconfigure_ip }}/&&/{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}/&&/{{ sap_general_preconfigure_hostname }}/{a++}END{print a}' /etc/hosts
register: __sap_general_preconfigure_register_ipv4_fqdn_sap_hostname_once_assert
ignore_errors: yes
changed_when: no

- name: Assert that ipv4 address, FQDN, and hostname are once in /etc/hosts
ansible.builtin.assert:
that: __sap_general_preconfigure_register_ipv4_fqdn_sap_hostname_once_assert.stdout == '1'
fail_msg: "FAIL: The line '{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}' needs to be once in /etc/hosts!"
success_msg: "PASS: The line '{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}' is once in /etc/hosts."
fail_msg: "FAIL: The line '{{ sap_general_preconfigure_ip }} {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} {{ sap_general_preconfigure_hostname }}' needs to be once in /etc/hosts!"
success_msg: "PASS: The line '{{ sap_general_preconfigure_ip }} {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} {{ sap_general_preconfigure_hostname }}' is once in /etc/hosts."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

#- name: Ensure that the entry in /etc/hosts is correct
# ansible.builtin.lineinfile:
# path: /etc/hosts
# regexp: '^{{ sap_ip }}\s'
# line: "{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }} {{ sap_base_settings_register_hostname_aliases.stdout }}"
# when:
# - sap_general_preconfigure_assert_modify_etc_hosts | bool

- name: Count the number of sap_ip ({{ sap_ip }}) entries in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/{{ sap_ip }}/{a++}END{print a}' /etc/hosts
- name: Count the number of sap_general_preconfigure_ip ({{ sap_general_preconfigure_ip }}) entries in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/{{ sap_general_preconfigure_ip }}/{a++}END{print a}' /etc/hosts
register: __sap_general_preconfigure_register_sap_ip_once_assert
ignore_errors: yes
changed_when: no

- name: Assert that there is just one line containing {{ sap_ip }} in /etc/hosts
- name: Assert that there is just one line containing {{ sap_general_preconfigure_ip }} in /etc/hosts
ansible.builtin.assert:
that: __sap_general_preconfigure_register_sap_ip_once_assert.stdout == '1'
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_ip }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_ip }}' in /etc/hosts."
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_general_preconfigure_ip }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_general_preconfigure_ip }}' in /etc/hosts."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Check for duplicate or missing entries of {{ sap_hostname }}.{{ sap_domain }} in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/^{{ sap_hostname }}.{{ sap_domain }}\s/||
/\s{{ sap_hostname }}.{{ sap_domain }}\s/||
/\s{{ sap_hostname }}.{{ sap_domain }}$/{a++}END{print a}' /etc/hosts
- name: Check for duplicate or missing entries of {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/^{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}\s/||
/\s{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}\s/||
/\s{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}$/{a++}END{print a}' /etc/hosts
register: __sap_general_preconfigure_register_fqdn_once_assert
ignore_errors: yes
changed_when: no

- name: Assert that there is just one line containing {{ sap_hostname }}.{{ sap_domain }} in /etc/hosts
- name: Assert that there is just one line containing {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} in /etc/hosts
ansible.builtin.assert:
that: __sap_general_preconfigure_register_fqdn_once_assert.stdout == '1'
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_hostname }}.{{ sap_domain }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_hostname }}.{{ sap_domain }}' in /etc/hosts."
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}' in /etc/hosts."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Check for duplicate or missing entries of {{ sap_hostname }} in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/^{{ sap_hostname }}\s/||
/\s{{ sap_hostname }}\s/||
/\s{{ sap_hostname }}$/{a++}END{print a}' /etc/hosts
- name: Check for duplicate or missing entries of {{ sap_general_preconfigure_hostname }} in /etc/hosts
ansible.builtin.command: awk 'BEGIN{a=0}/^{{ sap_general_preconfigure_hostname }}\s/||
/\s{{ sap_general_preconfigure_hostname }}\s/||
/\s{{ sap_general_preconfigure_hostname }}$/{a++}END{print a}' /etc/hosts
register: __sap_general_preconfigure_register_sap_hostname_once_assert
ignore_errors: yes
changed_when: no

- name: Assert that there is just one line containing {{ sap_hostname }} in /etc/hosts
- name: Assert that there is just one line containing {{ sap_general_preconfigure_hostname }} in /etc/hosts
ansible.builtin.assert:
that: __sap_general_preconfigure_register_sap_hostname_once_assert.stdout == '1'
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_hostname }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_hostname }}' in /etc/hosts."
fail_msg: "FAIL: There is no line, or more than one line, containing '{{ sap_general_preconfigure_hostname }}' in /etc/hosts!"
success_msg: "PASS: There is only one line containing '{{ sap_general_preconfigure_hostname }}' in /etc/hosts."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: Check hostname -s
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,16 @@
ignore_errors: yes
changed_when: no

- name: Assert that the output of hostname matches the content of variable sap_hostname
- name: Assert that the output of hostname matches the content of variable sap_general_preconfigure_hostname
ansible.builtin.assert:
that: __sap_general_preconfigure_register_hostname_assert.stdout == sap_hostname
fail_msg: "FAIL: The output of 'hostname' does not match the content of variable 'sap_hostname'!"
success_msg: "PASS: The output of 'hostname' matches the content of variable 'sap_hostname'."
that: __sap_general_preconfigure_register_hostname_assert.stdout == sap_general_preconfigure_hostname
fail_msg: "FAIL: The output of 'hostname' does not match the content of variable 'sap_general_preconfigure_hostname'!"
success_msg: "PASS: The output of 'hostname' matches the content of variable 'sap_general_preconfigure_hostname'."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"

- name: "Assert that the length of the hostname is not longer than 'sap_general_preconfigure_max_hostname_length'"
ansible.builtin.assert:
that: (sap_hostname | length | int) <= (sap_general_preconfigure_max_hostname_length | int)
fail_msg: "FAIL: The length of the hostname is {{ sap_hostname | length | int }} but must be less or equal to {{ sap_general_preconfigure_max_hostname_length }} (variable 'sap_general_preconfigure_max_hostname_length')!"
success_msg: "PASS: The length of the hostname is {{ sap_hostname | length | int }}, which is less or equal to {{ sap_general_preconfigure_max_hostname_length }} (variable 'sap_general_preconfigure_max_hostname_length')."
that: (sap_general_preconfigure_hostname | length | int) <= (sap_general_preconfigure_max_hostname_length | int)
fail_msg: "FAIL: The length of the hostname is {{ sap_general_preconfigure_hostname | length | int }} but must be less or equal to {{ sap_general_preconfigure_max_hostname_length }} (variable 'sap_general_preconfigure_max_hostname_length')!"
success_msg: "PASS: The length of the hostname is {{ sap_general_preconfigure_hostname | length | int }}, which is less or equal to {{ sap_general_preconfigure_max_hostname_length }} (variable 'sap_general_preconfigure_max_hostname_length')."
ignore_errors: "{{ sap_general_preconfigure_assert_ignore_errors | d(false) }}"
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
---

- name: Check dns forwarding settings
ansible.builtin.shell: test "$(dig {{ sap_hostname }}.{{ sap_domain }} +short)" = "{{ sap_ip }}"
ansible.builtin.shell: test "$(dig {{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }} +short)" = "{{ sap_general_preconfigure_ip }}"
changed_when: false
ignore_errors: true

### BUG: dig does not use search path in resolv.con on PPCle
- name: Check resolv.conf settings
ansible.builtin.shell: test "$(dig {{ sap_hostname }} +search +short)" = "{{ sap_ip }}"
ansible.builtin.shell: test "$(dig {{ sap_general_preconfigure_hostname }} +search +short)" = "{{ sap_general_preconfigure_ip }}"
changed_when: false
ignore_errors: true

- name: Check dns reverse settings
ansible.builtin.shell: test "$(dig -x {{ sap_ip }} +short)" = "{{ sap_hostname }}.{{ sap_domain }}."
ansible.builtin.shell: test "$(dig -x {{ sap_general_preconfigure_ip }} +short)" = "{{ sap_general_preconfigure_hostname }}.{{ sap_general_preconfigure_domain }}."
changed_when: false
ignore_errors: true
Loading

0 comments on commit 1c6348e

Please sign in to comment.