Skip to content

Commit

Permalink
buildhosts: Rewrite the push /etc/hosts user content feature
Browse files Browse the repository at this point in the history
The old code was messy, overbloated for nothing and even created a recursive loop...

Signed-off-by: Florent CARLI <[email protected]>
  • Loading branch information
insatomcat committed Jan 23, 2025
1 parent b9e8529 commit f6850b7
Showing 1 changed file with 15 additions and 35 deletions.
50 changes: 15 additions & 35 deletions roles/network_buildhosts/tasks/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,45 +36,25 @@
state: absent
regexp: '^127.*debian'

- name: push custom host file
- name: Push custom host file
when: hosts_path is defined
block:
- name: Check if the hosts file exists
delegate_to: localhost
stat:
path: "{{ hosts_path }}"
register: hosts_file_exists
become: false
- name: Read the hosts file content if it exists
delegate_to: localhost
slurp:
src: "{{ hosts_path }}"
register: hosts_content
when: hosts_file_exists.stat.exists
become: false
- name: Create a temporary file with the hosts content
template:
src: "{{ hosts_path }}"
dest: /tmp/hosts_temp
vars:
hosts_content: "{{ (hosts_content.content | b64decode).splitlines() }}"
when: hosts_file_exists.stat.exists
- name: Read the temporary hosts file content
slurp:
src: /tmp/hosts_temp
register: temp_hosts_content
when: hosts_file_exists.stat.exists
- name: Ensure /etc/hosts contains the updated user hosts content
blockinfile:
path: /etc/hosts
block: |
{% for line in (temp_hosts_content.content | b64decode).splitlines() %}
{{ line }}
{% endfor %}
marker: "# {mark} user hosts file"
when: hosts_file_exists.stat.exists
- name: Remove the temporary hosts file
file:
path: /tmp/hosts_temp
state: absent
when: hosts_file_exists.stat.exists
when: hosts_path is defined
- when: hosts_file_exists.stat.exists
block:
- name: Read the hosts file content if it exists
delegate_to: localhost
slurp:
src: "{{ hosts_path }}"
register: hosts_content
become: false
- name: Ensure /etc/hosts contains the updated user hosts content
blockinfile:
path: /etc/hosts
block: "{{ hosts_content.content | b64decode }}"
marker: "# {mark} user hosts file"

0 comments on commit f6850b7

Please sign in to comment.