---
- set_fact:
    kube_apiserver_count: "{{ groups['kube-master'] | length }}"

- set_fact:
    kube_apiserver_address: "{{ ip | default(ansible_default_ipv4['address']) }}"

- set_fact:
    kube_apiserver_access_address: "{{ access_ip | default(kube_apiserver_address) }}"

- set_fact:
    is_kube_master: "{{ inventory_hostname in groups['kube-master'] }}"

- set_fact:
    first_kube_master: "{{ hostvars[groups['kube-master'][0]]['access_ip'] | default(hostvars[groups['kube-master'][0]]['ip'] | default(hostvars[groups['kube-master'][0]]['ansible_default_ipv4']['address'])) }}"

- set_fact:
    loadbalancer_apiserver_localhost: false
  when: loadbalancer_apiserver is defined

- set_fact:
    kube_apiserver_endpoint: |-
      {% if not is_kube_master and loadbalancer_apiserver_localhost|default(true) -%}
           https://localhost:{{ nginx_kube_apiserver_port|default(kube_apiserver_port) }}
      {%- elif is_kube_master -%}
           https://127.0.0.1:{{ kube_apiserver_port }}
      {%- else -%}
      {%-   if loadbalancer_apiserver is defined and loadbalancer_apiserver.port is defined -%}
           https://{{ apiserver_loadbalancer_domain_name|default('lb-apiserver.kubernetes.local') }}:{{ loadbalancer_apiserver.port|default(kube_apiserver_port) }}
      {%-   else -%}
           https://{{ first_kube_master }}:{{ kube_apiserver_port }}
      {%-  endif -%}
      {%- endif %}

- set_fact:
    etcd_address: "{{ ip | default(ansible_default_ipv4['address']) }}"

- set_fact:
    etcd_access_address: "{{ access_ip | default(etcd_address) }}"

- set_fact:
    etcd_peer_url: "https://{{ etcd_access_address }}:2380"

- set_fact:
    etcd_client_url: "https://{{ etcd_access_address }}:2379"

- set_fact:
    etcd_authority: "127.0.0.1:2379"

- set_fact:
    etcd_endpoint: "https://{{ etcd_authority }}"

- set_fact:
    etcd_access_addresses: |-
      {% for item in groups['etcd'] -%}
        https://{{ hostvars[item]['access_ip'] | default(hostvars[item]['ip'] | default(hostvars[item]['ansible_default_ipv4']['address'])) }}:2379{% if not loop.last %},{% endif %}
      {%- endfor %}

- set_fact:
    etcd_access_endpoint: "{% if etcd_multiaccess|default(true) %}{{ etcd_access_addresses }}{% else %}{{ etcd_endpoint }}{% endif %}"

- set_fact:
    etcd_member_name: |-
      {% for host in groups['etcd'] %}
      {%   if inventory_hostname == host %}{{"etcd"+loop.index|string }}{% endif %}
      {% endfor %}

- set_fact:
    etcd_peer_addresses: |-
      {% for item in groups['etcd'] -%}
        {{ "etcd"+loop.index|string }}=https://{{ hostvars[item].access_ip | default(hostvars[item].ip | default(hostvars[item].ansible_default_ipv4['address'])) }}:2380{% if not loop.last %},{% endif %}
      {%- endfor %}

- set_fact:
    is_etcd_master: "{{ inventory_hostname in groups['etcd'] }}"

- set_fact:
    etcd_after_v3: etcd_version | version_compare("v3.0.0", ">=")

- set_fact:
    etcd_container_bin_dir: "{% if etcd_after_v3 %}/usr/local/bin/{% else %}/{% endif %}"

- set_fact:
    peer_with_calico_rr: "{{ 'calico-rr' in groups and groups['calico-rr']|length > 0 }}"

- name: check if atomic host
  stat:
    path: /run/ostree-booted
  register: ostree

- set_fact:
    is_atomic: "{{ ostree.stat.exists }}"

- set_fact:
    kube_cert_group: "kube"
  when: is_atomic

- include: set_resolv_facts.yml
  tags: [bootstrap-os, resolvconf, facts]
