Protect hostvars iterations from implicit localhost

Ansible-core 2.16.4 appears to have a behavior change where it
will include the implicit localhost in hostvars, which means that
any location we iterate over hostvars and assume it's a real host
could throw an exception.  To avoid that, add checks that the
variables we are about to access on the host exist.

Change-Id: Iff89da761e5f6748b454610a64c2fdd4f5e56a77
This commit is contained in:
James E. Blair 2024-12-04 13:47:20 -08:00
parent 7b6b571970
commit f88eb51b71
4 changed files with 10 additions and 0 deletions

View File

@ -17,20 +17,24 @@
ipv4_addresses: >
{% set hosts = [] -%}
{% for host, vars in hostvars.items() -%}
{% if 'nodepool' in vars -%}
{% if vars['nodepool']['private_ipv4'] -%}
{% set _ = hosts.append(vars['nodepool']['private_ipv4']) -%}
{% endif -%}
{% if vars['nodepool']['public_ipv4'] -%}
{% set _ = hosts.append(vars['nodepool']['public_ipv4']) -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{{- hosts | sort | unique -}}
ipv6_addresses: >
{% set hosts = [] -%}
{% for host, vars in hostvars.items() -%}
{% if 'nodepool' in vars -%}
{% if vars['nodepool']['public_ipv6'] -%}
{% set _ = hosts.append(vars['nodepool']['public_ipv6']) -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{{- hosts | sort | unique -}}

View File

@ -3,8 +3,10 @@
host_addresses: >
{% set hosts = {} -%}
{% for host, vars in hostvars.items() -%}
{% if 'nodepool' in vars -%}
{% set _ = hosts.update({host: vars['nodepool']['private_ipv4']}) -%}
{% set _ = hosts.update({vars['ansible_hostname']: vars['nodepool']['private_ipv4']}) -%}
{% endif -%}
{% endfor -%}
{{- hosts -}}

View File

@ -12,7 +12,9 @@
host_addresses: >
{% set hosts = [] -%}
{% for host, vars in hostvars.items() -%}
{% if 'nodepool' in vars -%}
{% set _ = hosts.append({'host': host, 'address': vars['nodepool']['private_ipv4']}) -%}
{% endif -%}
{% endfor -%}
{{- hosts -}}

View File

@ -12,6 +12,7 @@
host_addresses: >
{% set hosts = [] -%}
{% for host, vars in hostvars.items() -%}
{% if 'nodepool' in vars -%}
{% if vars['nodepool']['private_ipv4'] | length > 0 -%}
{% set _ = hosts.append(vars['nodepool']['private_ipv4']) -%}
{% endif -%}
@ -21,6 +22,7 @@
{% if vars['nodepool']['public_ipv6'] | length > 0 -%}
{% set _ = hosts.append(vars['nodepool']['public_ipv6']) -%}
{% endif -%}
{% endif -%}
{% endfor -%}
{{- hosts | sort | unique -}}