James E. Blair f88eb51b71 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
2024-12-04 13:47:20 -08:00

23 lines
711 B
YAML

- name: Set up the list of hosts and addresses
set_fact:
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 -}}
- name: Add inventory hostnames to the hosts file
become: yes
lineinfile:
dest: /etc/hosts
state: present
insertafter: EOF
line: "{{ zj_host_address.value }} {{ zj_host_address.key }}"
with_dict: "{{ host_addresses }}"
loop_control:
loop_var: zj_host_address