---
# SAP Note: 2382421 - Optimizing the Network Configuration on HANA- and OS-Level
#
- name: Configure - Display SAP note number 2382421 and its version
  debug:
    msg: "SAP note {{ (__sap_hana_preconfigure_sapnotes_versions | selectattr('number', 'match', '^2382421$') | first).number }}
          (version {{ (__sap_hana_preconfigure_sapnotes_versions | selectattr('number', 'match', '^2382421$') | first).version }}): Recommended network settings for SAP HANA"

- name: Set kernel tunables as per SAP note 2382421
  block:

    - name: Modify entries in file '{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}'
      lineinfile:
        path: "{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}"
        create: yes
        mode: 0644
        regexp: ^{{ line_item.name }}.*
        line: "{{ line_item.name }}={{ line_item.value }}"
        state: present
      with_items: "{{ sap_hana_preconfigure_kernel_parameters }}"
      loop_control:
        loop_var: line_item

    - name: Set net.core.wmem_max and net.core.rmem_max if necessary
      block:

        - name: Get the value of net.ipv4.tcp_wmem
          command: sysctl net.ipv4.tcp_wmem
          check_mode: no
          register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem

        - name: Display the maximum value of net.ipv4.tcp_wmem
          debug:
            var: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem.stdout.split()[-1]

        - name: Get the value of net.ipv4.tcp_rmem
          command: sysctl net.ipv4.tcp_rmem
          check_mode: no
          register: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem

        - name: Display the maximum value of net.ipv4.tcp_rmem
          debug:
            var: __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem.stdout.split()[-1]

        - name: Set net.core.wmem_max to the maximum value of net.ipv4.tcp_wmem if necessary
          lineinfile:
            path: "{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}"
            create: yes
            mode: 0644
            regexp: ^net.core.wmem_max.*
            line: "net.core.wmem_max={{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_wmem.stdout.split()[-1] }}"
            state: present

        - name: Set net.core.rmem_max to the maximum value of net.ipv4.tcp_rmem if necessary
          lineinfile:
            path: "{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}"
            create: yes
            mode: 0644
            regexp: ^net.core.rmem_max.*
            line: "net.core.rmem_max={{ __sap_hana_preconfigure_register_sysctl_ipv4_tcp_rmem.stdout.split()[-1] }}"
            state: present

      when:
        - not sap_hana_preconfigure_use_netapp_settings_nfs|d(false)
        - not ansible_architecture == 'ppc64le'

    - name: Reload kernel parameters from file '{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}'
      command: sysctl -p "{{ __sap_hana_preconfigure_etc_sysctl_saphana_conf }}"
      register: __sap_hana_preconfigure_register_saphana_conf_sysctl_p_output

    - name: Display kernel parameters for network tuning
      debug:
        var: __sap_hana_preconfigure_register_saphana_conf_sysctl_p_output.stdout_lines

  when: sap_hana_preconfigure_config_all|d(true) or sap_hana_preconfigure_2382421|d(false)
