Skip to content
Snippets Groups Projects
Unverified Commit cf3b3ca6 authored by R. P. Taylor's avatar R. P. Taylor Committed by GitHub
Browse files

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
parent 1955943d
Branches
Tags
No related merge requests found
--- ---
- 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:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment