diff --git a/roles/network_buildhosts/tasks/main.yml b/roles/network_buildhosts/tasks/main.yml index a088dffcd..764b1c34c 100644 --- a/roles/network_buildhosts/tasks/main.yml +++ b/roles/network_buildhosts/tasks/main.yml @@ -36,7 +36,8 @@ 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 @@ -44,37 +45,16 @@ 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"