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
This commit is contained in:
R. P. Taylor
2023-08-15 20:22:28 -07:00
committed by GitHub
parent 1955943d4a
commit cf3b3ca6fd

View File

@@ -1,8 +1,5 @@
--- ---
- name: Hosts | update inventory in hosts file - name: Hosts | create hosts list from inventory
when: populate_inventory_to_hosts_file
block:
- name: Hosts | create list from inventory
set_fact: set_fact:
etc_hosts_inventory_block: |- etc_hosts_inventory_block: |-
{% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%} {% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
@@ -21,7 +18,7 @@
blockinfile: blockinfile:
path: /etc/hosts path: /etc/hosts
block: "{{ hostvars.localhost.etc_hosts_inventory_block }}" block: "{{ hostvars.localhost.etc_hosts_inventory_block }}"
state: present state: "{{ 'present' if populate_inventory_to_hosts_file else 'absent' }}"
create: yes create: yes
backup: yes backup: yes
unsafe_writes: yes unsafe_writes: yes