From 7761249774b8a6e9bebc60db69e0bc56ad9f7622 Mon Sep 17 00:00:00 2001 From: Damien Ciabrini Date: Mon, 8 Jun 2020 21:30:43 +0200 Subject: [PATCH] Preserve inode when updating /etc/hosts Containers configured by tripleo bind-mount /etc/hosts directly, which means any change to that file has to preserve the original inode, otherwise the containers will get out of sync with the host and will not see updates. Change tripleo_host_entries to not depend on atomic semantics of ansible, which changes inodes on update. Instead, perform a non-atomic update to preserve inodes, and rely on the retry logics of openstack containers to recover from unexpected behaviour in case /etc/hosts is consumed while it is being updated. Closes-Bug: #1882290 Change-Id: I34dd9121bbd650b79cb523e4dbed5949a0e7d52d --- .../tripleo_hosts_entries/tasks/main.yml | 44 +++++++++++++++++-- 1 file changed, 41 insertions(+), 3 deletions(-) diff --git a/tripleo_ansible/roles/tripleo_hosts_entries/tasks/main.yml b/tripleo_ansible/roles/tripleo_hosts_entries/tasks/main.yml index d2fc90852..a860a48b4 100644 --- a/tripleo_ansible/roles/tripleo_hosts_entries/tasks/main.yml +++ b/tripleo_ansible/roles/tripleo_hosts_entries/tasks/main.yml @@ -15,15 +15,36 @@ # under the License. +- name: Create temporary file for hosts + become: true + tempfile: + state: file + suffix: tmphosts + register: tripleo_hosts_entries_tmp_hosts + check_mode: false + tags: + - tripleo_hosts_entries + +- name: Prepare temporary /etc/hosts + become: true + copy: + remote_src: true + src: "{{ tripleo_hosts_entries_hosts_path }}" + dest: "{{ tripleo_hosts_entries_tmp_hosts.path }}" + mode: preserve + tags: + - tripleo_hosts_entries + - name: Remove old Heat hosts configuration (if present) become: true blockinfile: state: absent - path: "{{ tripleo_hosts_entries_hosts_path }}" + path: "{{ tripleo_hosts_entries_tmp_hosts.path }}" block: "" marker: "# {mark}" marker_begin: "HEAT_HOSTS_START - Do not edit manually within this section!" marker_end: "HEAT_HOSTS_END" + register: tripleo_hosts_entries_heat_marker tags: - tripleo_hosts_entries @@ -50,11 +71,11 @@ tags: - tripleo_hosts_entries -- name: Update /etc/hosts +- name: Prepare new /etc/hosts become: true blockinfile: create: true - path: "{{ tripleo_hosts_entries_hosts_path }}" + path: "{{ tripleo_hosts_entries_tmp_hosts.path }}" # BOF denotes the beginning of the file. insertbefore: BOF block: | @@ -64,5 +85,22 @@ tripleo_hosts_entries_extra_hosts_entries | default([]) %} {{ host }} {% endfor %} + register: tripleo_hosts_entries_new_entries + tags: + - tripleo_hosts_entries + +- name: Update /etc/hosts contents (if changed) + become: true + # cp preserves the inode of the existing file tripleo_hosts_entries_hosts_path + command: cp "{{ tripleo_hosts_entries_tmp_hosts.path }}" "{{ tripleo_hosts_entries_hosts_path }}" + when: tripleo_hosts_entries_heat_marker.changed or tripleo_hosts_entries_new_entries.changed + tags: + - tripleo_hosts_entries + +- name: Clean up temporary hosts file + file: + path: "{{ tripleo_hosts_entries_tmp_hosts.path }}" + state: absent + when: tripleo_hosts_entries_tmp_hosts.path is defined tags: - tripleo_hosts_entries