skylab
/
skylab-ansible
Archived
2
0
Fork 0
This repository has been archived on 2023-05-19. You can view files and clone it, but cannot push or open issues or pull requests.
skylab-ansible/skylab/core/roles/server/tasks/hosts.yaml

33 lines
1014 B
YAML

---
- name: Retrieve current hostsfile contents
ansible.builtin.command:
cmd: cat /etc/hosts
changed_when: false
register: _existing_hostsfile_raw
- name: Assemble hostsfile lines
vars:
_hostsfile_lines: []
ansible.builtin.set_fact:
_hostsfile_lines: "{{ _hostsfile_lines + [hostvars[item].skylab_cluster.address.internal | ansible.netcommon.ipaddr('address') + ' ' + item + '.local ' + hostvars[item].skylab_legacy_names | default([]) | join(' ')] }}"
loop: "{{ groups.cluster }}"
- name: Configure local hostsfile
become: true
ansible.builtin.lineinfile:
path: /etc/hosts
line: "{{ item }}"
state: present
loop: "{{ _hostsfile_lines }}"
loop_control:
label: "{{ item.partition(' ')[0] }}"
- name: Remove unmanaged hostsfile entries
become: true
when: "'localhost' not in item and item not in _hostsfile_lines"
ansible.builtin.lineinfile:
path: /etc/hosts
line: "{{ item }}"
state: absent
loop: "{{ _existing_hostsfile_raw.stdout_lines }}"