Merge pull request #12832 from VannTen/cleanup/network_facts

network_facts: streamline set_fact and setup calls
This commit is contained in:
Kubernetes Prow Robot 2026-01-06 15:01:10 +05:30 committed by GitHub
commit 14b20ad2a2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
4 changed files with 31 additions and 54 deletions

View File

@ -93,7 +93,6 @@ tf-elastx_ubuntu20-calico:
extends: .terraform_apply extends: .terraform_apply
stage: deploy-part1 stage: deploy-part1
when: on_success when: on_success
allow_failure: true
variables: variables:
<<: *elastx_variables <<: *elastx_variables
PROVIDER: openstack PROVIDER: openstack

View File

@ -39,16 +39,12 @@ DNS.{{ counter["dns"] }} = {{ apiserver_loadbalancer_domain_name }}{{ increment(
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