From cf3b3ca6fd0b67727530c58e3211b7a333167d9d Mon Sep 17 00:00:00 2001
From: "R. P. Taylor" <1686627+rptaylor@users.noreply.github.com>
Date: Tue, 15 Aug 2023 20:22:28 -0700
Subject: [PATCH] clean up /etc/hosts file if populate_inventory_to_hosts_file
 is false (#10144)

* de-populate hosts file if populate_inventory_to_hosts_file is false

keep newline

* fix when condition
---
 .../preinstall/tasks/0090-etchosts.yml        | 49 +++++++++----------
 1 file changed, 23 insertions(+), 26 deletions(-)

diff --git a/roles/kubernetes/preinstall/tasks/0090-etchosts.yml b/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
index 6b3a8921b..6bec16998 100644
--- a/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
+++ b/roles/kubernetes/preinstall/tasks/0090-etchosts.yml
@@ -1,32 +1,29 @@
 ---
-- name: Hosts | update inventory in hosts file
-  when: populate_inventory_to_hosts_file
-  block:
-    - name: Hosts | create list from inventory
-      set_fact:
-        etc_hosts_inventory_block: |-
-          {% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
-          {% 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'] }} {% else %} {{ item }}.{{ dns_domain }} {{ item }} {% endif %}
+- name: Hosts | create hosts list from inventory
+  set_fact:
+    etc_hosts_inventory_block: |-
+      {% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
+      {% 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'] }} {% else %} {{ item }}.{{ dns_domain }} {{ item }} {% endif %}
 
-          {% endif %}
-          {% endfor %}
-      delegate_to: localhost
-      connection: local
-      delegate_facts: yes
-      run_once: yes
+      {% endif %}
+      {% endfor %}
+  delegate_to: localhost
+  connection: local
+  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
-        unsafe_writes: yes
-        marker: "# Ansible inventory hosts {mark}"
-        mode: 0644
+- name: Hosts | populate inventory into hosts file
+  blockinfile:
+    path: /etc/hosts
+    block: "{{ hostvars.localhost.etc_hosts_inventory_block }}"
+    state: "{{ 'present' if populate_inventory_to_hosts_file else 'absent' }}"
+    create: yes
+    backup: yes
+    unsafe_writes: yes
+    marker: "# Ansible inventory hosts {mark}"
+    mode: 0644
 
 - name: Hosts | populate kubernetes loadbalancer address into hosts file
   lineinfile:
-- 
GitLab