---

- name: Display host and domain name, and IP address before the modification
  debug:
    msg:
      - "sap_hostname = {{ sap_hostname }}"
      - "sap_domain = {{ sap_domain }}"
      - "sap_ip = {{ sap_ip }}"

- 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_preconfigure_register_sap_hostname_aliases
  changed_when: false

- name: Display hostname aliases
  debug:
    var=__sap_preconfigure_register_sap_hostname_aliases

- name: Check if ipv4 address, FQDN, and hostname are in /etc/hosts
  block:
    - name: Perform the /etc/hosts completeness check
      command: awk 'BEGIN{a=0}/{{ sap_ip }}/&&/{{ sap_hostname }}.{{ sap_domain }}/&&/{{ sap_hostname }}/{a++}END{print a}' /etc/hosts
      register: __sap_preconfigure_register_ipv4_fqdn_sap_hostname_once_check
      changed_when: false

    - name: Display the output of the /etc/hosts completeness check
      debug:
        var: __sap_preconfigure_register_ipv4_fqdn_sap_hostname_once_check.stdout_lines,
             __sap_preconfigure_register_ipv4_fqdn_sap_hostname_once_check.stderr_lines

    - name: Display the expected output of the /etc/hosts completeness check
      debug:
        msg:
          - "Expected:"
          - "{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}"
      when:
        - __sap_preconfigure_register_ipv4_fqdn_sap_hostname_once_check.stdout != "1"

    - name: Fail if ip4 address, FQDN, or hostname are not in /etc/hosts
      fail:
        msg:
          - "Server's ip4 address, FQDN, or hostname are not in /etc/hosts!"
          - "Expected:"
          - "{{ sap_ip }} {{ sap_hostname }}.{{ sap_domain }} {{ sap_hostname }}"
      when:
        - __sap_preconfigure_register_ipv4_fqdn_sap_hostname_once_check.stdout != "1"
      ignore_errors: "{{ ansible_check_mode }}"

  when:
    - not sap_preconfigure_modify_etc_hosts | bool

# 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: __sap_preconfigure_register_duplicate_ip_check
  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!"

- name: Report if there is more than one line with the IP address
  debug:
    msg:
      - "More than one line containing {{ sap_ip }}. File /etc/hosts will not be modified."
  when:
    - not ansible_check_mode
    - __sap_preconfigure_register_duplicate_ip_check.stdout == 'Duplicate IP entry in /etc/hosts!'
    - sap_preconfigure_modify_etc_hosts | bool

- 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 }}{{ __sap_preconfigure_register_sap_hostname_aliases.stdout }}"
    backup: yes
  when:
    - not ansible_check_mode
    - sap_domain | length > 0
    - __sap_preconfigure_register_duplicate_ip_check.stdout != 'Duplicate IP entry in /etc/hosts!'
    - sap_preconfigure_modify_etc_hosts | bool

- name: Check for duplicate or missing entries of hostname and fqdn in /etc/hosts
  shell: |
    n=$(awk 'BEGIN{a=0}/^{{ line_item }}\s/||/\s{{ line_item }}\s/||/\s{{ line_item }}$/{a++}END{print a}' /etc/hosts)
    if [ $n -eq 1 ]; then
      exit 0
    else
      exit 1
    fi
  with_items:
    - '{{ sap_hostname }}.{{ sap_domain }}'
    - '{{ sap_hostname }}'
  changed_when: false
  loop_control:
    loop_var: line_item
  when: not ansible_check_mode

- name: Check hostname -s and hostname -f settings
  shell: test "$(hostname -s)" = "$(hostname)" -a "$(hostname -f)" = "$(hostname).$(hostname -d)"
  changed_when: false
