---
#---------------------------------------------------------------
# get node identity                                    [node-id]
#---------------------------------------------------------------
- name: node identity
  tags: [ always, node-id ]
  block:

    # gather node fact, also check connectivity
    - name: get node fact
      become: false
      shell: |
        . /etc/os-release > /dev/null
        OS_MAJOR_VERSION="${VERSION_ID}"
        echo ${ID}
        if [[ $VERSION_ID == *.* ]]; then
            OS_MAJOR_VERSION=$(echo "${VERSION_ID}" | cut -d. -f1) 
        fi
        echo ${VERSION_ID}
        echo ${OS_MAJOR_VERSION}
        if command -v dpkg >/dev/null 2>&1; then
            echo ${VERSION_CODENAME}
        else
            echo el${OS_MAJOR_VERSION}
        fi
        echo $(uname -m);
        if command -v dpkg >/dev/null 2>&1; then
            echo "deb"
        else
            echo "rpm"
        fi
        echo $(whoami)
        echo $(hostname);
        echo $(grep -c '^processor' /proc/cpuinfo);
        echo $(getconf _PHYS_PAGES);
        echo $(getconf PAGE_SIZE);
        /bin/true;
      register: node_result
      args: { executable: /bin/bash }

    # calculate node identity
    - name: set node fact
      become: false
      connection: local
      set_fact:
        os_vendor:       "{{ node_result.stdout_lines[0] }}"
        os_version_full: "{{ node_result.stdout_lines[1] }}"
        os_version:      "{{ node_result.stdout_lines[2]|int }}"
        os_codename:     "{{ node_result.stdout_lines[3] }}"
        os_arch:         "{{ node_result.stdout_lines[4] }}"
        os_package:      "{{ node_result.stdout_lines[5] }}"
        node_user:       "{{ node_result.stdout_lines[6] }}"
        node_hostname:   "{{ node_result.stdout_lines[7] }}"
        node_cpu:        "{{ node_result.stdout_lines[8]|int }}"
        node_pages:      "{{ node_result.stdout_lines[9]|int }}"
        node_page_size:  "{{ node_result.stdout_lines[10]|int }}"

    # if nodename is empty, or can not be built from pgsql instance's identity
    - name: fill empty nodename with hostname
      when: (nodename is not defined or nodename == '') and not (node_id_from_pg|bool and pg_cluster is defined and pg_seq is defined and pg_cluster != '' and pg_seq != '' )
      connection: local
      set_fact: { nodename: "{{ node_hostname }}" }

    - name: build node identity
      connection: local
      set_fact:
        nodename:        "{% if node_id_from_pg|bool and pg_cluster is defined and pg_seq is defined and pg_cluster != '' and pg_seq != '' %}{{ pg_cluster }}-{{ pg_seq }}{% else %}{{ nodename|default(omit) }}{% endif %}"
        node_cluster:    "{% if node_id_from_pg|bool and pg_cluster is defined and pg_cluster != '' and (node_cluster is not defined or node_cluster == 'nodes' or node_cluster == '') %}{{ pg_cluster }}{% else %}{{ node_cluster|default('nodes') }}{% endif %}"
        node_os_code:    "{% if os_package == 'rpm' %}el{{ os_version }}{% elif os_package == 'deb' %}{{ os_vendor[0] }}{{ os_version }}{% endif %}"  # el7(legacy), el8, el9, el10, u22, u24, d12, d13
        node_os_code_fb: "{% if os_package == 'rpm' %}el10{% elif os_package == 'deb' %}{% if os_vendor == 'ubuntu' %}u24{% else %}d12{% endif %}{% endif %}"  # fallback to el10, u24, d12
        node_mem_bytes:  "{{ node_pages|int * node_page_size|int }}"
        node_mem_mb:     "{{ (node_pages|int * node_page_size|int / 1048576) | round(0, 'ceil')|int }}"
        node_mem_gb:     "{{ [ (node_pages|int * node_page_size|int / 1073741824) | round(0, 'ceil')|int , 1 ] | max }}"

    # load os ad hoc vars according to os_package (rpm/deb) (and optional aarch64 setup)
    - name: load os arch specific vars
      become: false
      connection: local
      block:
        - name: load roles/node_id/vars/{{ node_os_code }}.{{ os_arch }}.yml
          include_vars: { file: "{{ role_path }}/vars/{{ node_os_code }}.{{ os_arch }}.yml" }
          ignore_errors: true
          register: primary_vars_result
        - name: fallback to {{ node_os_code_fb }}.{{ os_arch }}.yml if {{ node_os_code }}.{{ os_arch }}.yml not exists
          include_vars: { file: "{{ role_path }}/vars/{{ node_os_code_fb }}.{{ os_arch }}.yml" }
          when: primary_vars_result is failed

    # nodename, cluster, spec, os and modules
    - name: print node identity
      connection: local
      debug:
        msg: >-
          nodename: {{ nodename }},
          node_cluster: {{ node_cluster }},
          spec: c{{ node_cpu }}m{{ node_mem_gb }}, os: {{ os_vendor }}.{{ os_version }}.{{ os_arch }} {{ node_os_code }}:{{ os_package }},
          {% if inventory_hostname == admin_ip %}[ADMIN]{% endif %}
          {% if inventory_hostname in groups["infra"]|default([]) %}[INFRA]{% endif %}
          {% if pg_cluster is defined %}[PGSQL: {{ pg_cluster }}]{% endif %}
          {% if redis_cluster is defined %}[REDIS: {{ redis_cluster }}]{% endif %}
          {% if etcd_cluster  is defined %}[ETCD: {{ etcd_cluster }}]{% endif %}
          {% if minio_cluster is defined %}[MINIO: {{ minio_cluster }}]{% endif %}
          {% if kafka_cluster is defined %}[KAFKA: {{ kafka_cluster }}]{% endif %}
          {% if vip_enabled is defined and vip_enabled|bool %}[VIP: {{ vip_address }}]{% endif %}

...