mirror of
https://github.com/kubernetes-sigs/kubespray.git
synced 2025-12-14 13:54:37 +03:00
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:
@@ -1,32 +1,29 @@
|
|||||||
---
|
---
|
||||||
- name: Hosts | update inventory in hosts file
|
- name: Hosts | create hosts list from inventory
|
||||||
when: populate_inventory_to_hosts_file
|
set_fact:
|
||||||
block:
|
etc_hosts_inventory_block: |-
|
||||||
- name: Hosts | create list from inventory
|
{% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
|
||||||
set_fact:
|
{% if 'access_ip' in hostvars[item] or 'ip' in hostvars[item] or 'ansible_default_ipv4' in hostvars[item] -%}
|
||||||
etc_hosts_inventory_block: |-
|
{{ hostvars[item]['access_ip'] | default(hostvars[item]['ip'] | default(hostvars[item]['ansible_default_ipv4']['address'])) }}
|
||||||
{% for item in (groups['k8s_cluster'] + groups['etcd'] | default([]) + groups['calico_rr'] | default([])) | unique -%}
|
{%- 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 %}
|
||||||
{% 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 %}
|
{% endif %}
|
||||||
{% endfor %}
|
{% endfor %}
|
||||||
delegate_to: localhost
|
delegate_to: localhost
|
||||||
connection: local
|
connection: local
|
||||||
delegate_facts: yes
|
delegate_facts: yes
|
||||||
run_once: yes
|
run_once: yes
|
||||||
|
|
||||||
- name: Hosts | populate inventory into hosts file
|
- name: Hosts | populate inventory into hosts file
|
||||||
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
|
||||||
marker: "# Ansible inventory hosts {mark}"
|
marker: "# Ansible inventory hosts {mark}"
|
||||||
mode: 0644
|
mode: 0644
|
||||||
|
|
||||||
- name: Hosts | populate kubernetes loadbalancer address into hosts file
|
- name: Hosts | populate kubernetes loadbalancer address into hosts file
|
||||||
lineinfile:
|
lineinfile:
|
||||||
|
|||||||
Reference in New Issue
Block a user