network_facts: streamline set_fact and setup calls (#12982)

- invoke setup module only once to gather ipv4 and ipv6 addresses
- eliminate remaining use of `fallback_ip` and `fallback_ip6`, allowing
  us to define (with `set_fact` all the "computed" IPs variable in one
  go, since there is no longer a dependency between them.

Co-authored-by: Max Gautier <mg@max.gautier.name>
This commit is contained in:
k8s-infra-cherrypick-robot
2026-02-10 04:04:00 -08:00
committed by GitHub
parent f23796e09d
commit 4598ba2e57
3 changed files with 31 additions and 53 deletions

View File

@@ -36,16 +36,12 @@ DNS.{{ counter["dns"] }} = {{ host }}{{ increment(counter, 'dns') }}
DNS.{{ counter["dns"] }} = {{ etcd_alt_name }}{{ increment(counter, 'dns') }} DNS.{{ counter["dns"] }} = {{ etcd_alt_name }}{{ increment(counter, 'dns') }}
{% endfor %} {% endfor %}
{% for host in groups['etcd'] %} {% for host in groups['etcd'] %}
{% if hostvars[host]['access_ip'] is defined %} {% for address in hostvars[host]['main_access_ips'] %}
IP.{{ counter["ip"] }} = {{ hostvars[host]['access_ip'] }}{{ increment(counter, 'ip') }} IP.{{ counter["ip"] }} = {{ address }}{{ increment(counter, 'ip') }}
{% endif %} {% endfor %}
{% if hostvars[host]['access_ip6'] is defined %} {% for address in hostvars[host]['main_ips'] %}
IP.{{ counter["ip"] }} = {{ hostvars[host]['access_ip6'] }}{{ increment(counter, 'ip') }} IP.{{ counter["ip"] }} = {{ address }}{{ increment(counter, 'ip') }}
{% endif %} {% endfor %}
{% if ipv6_stack %}
IP.{{ counter["ip"] }} = {{ hostvars[host]['ip6'] | default(hostvars[host]['fallback_ip6']) }}{{ increment(counter, 'ip') }}
{% endif %}
IP.{{ counter["ip"] }} = {{ hostvars[host]['main_ip'] }}{{ increment(counter, 'ip') }}
{% endfor %} {% endfor %}
{% for cert_alt_ip in etcd_cert_alt_ips %} {% for cert_alt_ip in etcd_cert_alt_ips %}
IP.{{ counter["ip"] }} = {{ cert_alt_ip }}{{ increment(counter, 'ip') }} IP.{{ counter["ip"] }} = {{ cert_alt_ip }}{{ increment(counter, 'ip') }}

View File

@@ -1,7 +1,7 @@
--- ---
- name: Kubeadm | Check api is up - name: Kubeadm | Check api is up
uri: uri:
url: "https://{{ ip | default(fallback_ip) }}:{{ kube_apiserver_port }}/healthz" url: "https://{{ main_ip | ansible.utils.ipwrap }}:{{ kube_apiserver_port }}/healthz"
validate_certs: false validate_certs: false
when: ('kube_control_plane' in group_names) when: ('kube_control_plane' in group_names)
register: _result register: _result

View File

@@ -3,54 +3,36 @@
tags: tags:
- always - always
block: block:
- name: Gather ansible_default_ipv4 - name: Gather node IPs
setup: setup:
gather_subset: '!all,network' gather_subset: '!all,!min,network'
filter: "ansible_default_ipv4" filter: "ansible_default_ip*"
when: ansible_default_ipv4 is not defined when: ansible_default_ipv4 is not defined or ansible_default_ipv6 is not defined
ignore_unreachable: true ignore_unreachable: true
# Set 127.0.0.1 as fallback IP if we do not have host facts for host
# ansible_default_ipv4 isn't what you think. - name: Set computed IPs varables
# https://medium.com/opsops/ansible-default-ipv4-is-not-what-you-think-edb8ab154b10 vars:
# TODO: discard this and update all the location relying on it in "looping on hostvars" templates
- name: Set fallback_ip
set_fact:
fallback_ip: "{{ ansible_default_ipv4.address | d('127.0.0.1') }}" fallback_ip: "{{ ansible_default_ipv4.address | d('127.0.0.1') }}"
when: fallback_ip is not defined
- name: Gather ansible_default_ipv6
setup:
gather_subset: '!all,network'
filter: "ansible_default_ipv6"
when: ansible_default_ipv6 is not defined
ignore_unreachable: true
- name: Set fallback_ip6
set_fact:
fallback_ip6: "{{ ansible_default_ipv6.address | d('::1') }}" fallback_ip6: "{{ ansible_default_ipv6.address | d('::1') }}"
when: fallback_ip6 is not defined # Set 127.0.0.1 as fallback IP if we do not have host facts for host
# ansible_default_ipv4 isn't what you think.
- name: Set main access ip(access_ip based on ipv4_stack/ipv6_stack options). _ipv4: "{{ ip | default(fallback_ip) }}"
_access_ipv4: "{{ access_ip | default(_ipv4) }}"
_ipv6: "{{ ip6 | default(fallback_ip6) }}"
_access_ipv6: "{{ access_ip6 | default(_ipv6) }}"
_access_ips:
- "{{ _access_ipv4 if ipv4_stack }}"
- "{{ _access_ipv6 if ipv6_stack }}"
_ips:
- "{{ _ipv4 if ipv4_stack }}"
- "{{ _ipv6 if ipv6_stack }}"
set_fact: set_fact:
cacheable: true cacheable: true
main_access_ip: >- main_access_ip: "{{ _access_ipv4 if ipv4_stack else _access_ipv6 }}"
{%- if ipv4_stack -%} main_ip: "{{ _ipv4 if ipv4_stack else _ipv6 }}"
{{ access_ip | default(ip | default(fallback_ip)) }} # Mixed IPs - for dualstack
{%- else -%} main_access_ips: "{{ _access_ips | select }}"
{{ access_ip6 | default(ip6 | default(fallback_ip6)) }} main_ips: "{{ _ips | select }}"
{%- endif -%}
- name: Set main ip(ip based on ipv4_stack/ipv6_stack options).
set_fact:
cacheable: true
main_ip: "{{ (ip | default(fallback_ip)) if ipv4_stack else (ip6 | default(fallback_ip6)) }}"
- name: Set main access ips(mixed ips for dualstack).
set_fact:
main_access_ips: ["{{ (main_access_ip + ',' + (access_ip6 | default(ip6 | default(fallback_ip6)))) if (ipv4_stack and ipv6_stack) else main_access_ip }}"]
- name: Set main ips(mixed ips for dualstack).
set_fact:
main_ips: ["{{ (main_ip + ',' + (ip6 | default(fallback_ip6))) if (ipv4_stack and ipv6_stack) else main_ip }}"]
- name: Set no_proxy - name: Set no_proxy
import_tasks: no_proxy.yml import_tasks: no_proxy.yml