commit 110d870eaac2efee2e131babacebc5337a15b3e3 Author: Bernd Finger Date: Thu Jul 16 11:56:22 2020 +0200 do not modify /etc/hosts if there is more than one line containing sap_ip diff --git a/tasks/RedHat/generic/check-dns-name-resolution.yml b/tasks/RedHat/generic/check-dns-name-resolution.yml index 9450b60..b7b915d 100644 --- a/tasks/RedHat/generic/check-dns-name-resolution.yml +++ b/tasks/RedHat/generic/check-dns-name-resolution.yml @@ -9,11 +9,12 @@ - "sap_domain = {{ sap_domain }}" - "sap_ip = {{ sap_ip }}" -- name: Verify that the DNS domain is set - assert: - that: - not( (sap_domain is undefined) or (sap_domain is none) or (sap_domain | trim == '') ) - msg: "You need to define the variable 'sap_domain' in defaults/main.yml." +# moved to configure-etc-hosts.yml: +#- name: Verify that the DNS domain is set +# assert: +# that: +# not( (sap_domain is undefined) or (sap_domain is none) or (sap_domain | trim == '') ) +# msg: "You need to define the variable 'sap_domain' in defaults/main.yml." ### DNS is not that necessary and as such the errors are ignored ### for production it is strongly recommended to have proper DNS setup diff --git a/tasks/RedHat/generic/configure-etc-hosts.yml b/tasks/RedHat/generic/configure-etc-hosts.yml index 18872d1..488fba7 100644 --- a/tasks/RedHat/generic/configure-etc-hosts.yml +++ b/tasks/RedHat/generic/configure-etc-hosts.yml @@ -13,7 +13,7 @@ shell: | awk '( $1 == "{{ sap_ip }}" ) { for (i=2; i<=NF; ++i) { - if (( $i != "{{ sap_hostname }}" ) && ( $i != "{{ sap_hostname }}.{{ sap_domain }}" )) { printf $i" " } + if (( $i != "{{ sap_hostname }}" ) && ( $i != "{{ sap_hostname }}.{{ sap_domain }}" )) { printf " "$i } } }' /etc/hosts register: sap_hostname_aliases @@ -54,39 +54,48 @@ msg: - "System {{ sap_hostname }}, {{ sap_hostname }}.{{ sap_domain }} has ipv4 address {{ sap_ip }}." +# We allow more than one line containing sap_ip: +- name: Check for duplicate entries of {{ sap_ip }} in /etc/hosts + shell: | + n=$(grep "^{{ sap_ip }}\s" /etc/hosts | wc -l) + if [ $n -gt 1 ]; then + echo "Duplicate IP entry in /etc/hosts!" + exit 1 + else + exit 0 + fi + register: shell_result + changed_when: false + ignore_errors: yes + when: not ansible_check_mode + +- name: Verify that variable sap_domain is set + assert: + that: + not( (sap_domain is undefined) or (sap_domain is none) or (sap_domain | trim == '') ) + msg: "Variable 'sap_domain' is undefined or empty. Please define it in defaults/main.yml or via --extra-vars!" + - debug: msg: - - "Hostname alias(es) found: {{ sap_hostname_aliases.stdout }}. So /etc/hosts will not be modified." + - "More than one line containing {{ sap_ip }}. File /etc/hosts will not be modified." when: - - sap_hostname_aliases.stdout != '' + - not ansible_check_mode + - shell_result.stdout == 'Duplicate IP entry in /etc/hosts!' - sap_preconfigure_modify_etc_hosts | bool -# We only modify /etc/hosts if there is no hostname alias defined: - name: Ensure that the entry in /etc/hosts is correct lineinfile: path: /etc/hosts regexp: '^{{ sap_ip }}\s' - line: "{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}" + line: "{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}{{sap_hostname_aliases.stdout }}" backup: yes when: - - sap_hostname_aliases.stdout == '' + - not ansible_check_mode + - sap_domain != '' + - shell_result.stdout != 'Duplicate IP entry in /etc/hosts!' - sap_preconfigure_modify_etc_hosts | bool -# We will allow more than one line containing sap_ip: -- name: Check for duplicate entries of {{ sap_ip }} in /etc/hosts - shell: | - n=$(grep "^{{ sap_ip }}\s" /etc/hosts | wc -l) - if [ $n -eq 1 ]; then - exit 0 - else - echo "Duplicate IP entry in /etc/hosts!" - exit 1 - fi - changed_when: false - ignore_errors: yes - when: not ansible_check_mode - -- name: Check for duplicate entries of hostname and fqdn in /etc/hosts +- name: Check for duplicate or missing entries of hostname and fqdn in /etc/hosts shell: | n=$(grep -w "{{ line_item }}" /etc/hosts | wc -l) if [ $n -eq 1 ]; then