From b09fe64ff13474abe892a75fc8e56a73ad7ff500 Mon Sep 17 00:00:00 2001
From: Lovro Seder <vrovro@gmail.com>
Date: Thu, 16 Apr 2020 15:12:45 +0200
Subject: [PATCH] Calculate inventory list only once (#5956)

---
 .../preinstall/tasks/0090-etchosts.yml        | 19 +++++++++++++------
 1 file changed, 13 insertions(+), 6 deletions(-)

diff --git a/roles/kubernetes/preinstall/tasks/0090-etchosts.yml b/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
index d7b18012f..9edab21f5 100644
--- a/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
+++ b/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
@@ -1,14 +1,21 @@
 ---
-- name: Hosts | populate inventory into hosts file
-  blockinfile:
-    path: /etc/hosts
-    block: |-
+- name: Hosts | create list from inventory
+  set_fact:
+    etc_hosts_inventory_block: |-
       {% for item in (groups['k8s-cluster'] + groups['etcd'] + groups['calico-rr']|default([]))|unique -%}
-      {% if 'access_ip' in hostvars[item] or 'ip' in hostvars[item] or fallback_ips[item] != "skip" -%}
-      {{ hostvars[item]['access_ip'] | default(hostvars[item]['ip'] | default(fallback_ips[item])) }}
+      {% if 'access_ip' in hostvars[item] or 'ip' in hostvars[item] or 'ansible_default_ipv4' in hostvars[item] -%}
+      {{ hostvars[item]['access_ip'] | default(hostvars[item]['ip'] | default(hostvars[item]['ansible_default_ipv4']['address'])) }}
       {%- if ('ansible_hostname' in hostvars[item] and item != hostvars[item]['ansible_hostname']) %} {{ hostvars[item]['ansible_hostname'] }}.{{ dns_domain }} {{ hostvars[item]['ansible_hostname'] }}{% endif %} {{ item }}.{{ dns_domain }} {{ item }}
       {% endif %}
       {% endfor %}
+  delegate_to: localhost
+  delegate_facts: yes
+  run_once: yes
+
+- name: Hosts | populate inventory into hosts file
+  blockinfile:
+    path: /etc/hosts
+    block: "{{ hostvars.localhost.etc_hosts_inventory_block }}"
     state: present
     create: yes
     backup: yes
-- 
GitLab