Reorder hostfile insertion to support upgrades

When upgrading from a /etc/hosts file created before commit
ae02667b84 the old entries were not
removed on the first run. The hostname generation had to be run again to
remove them.

Reordering the if statement ensures that existing entries are changed
instead of adding new entries for the same IP address and that
duplicate entries are removed in all cases.

Change-Id: I2a21d13310b457e91a090541ae7f02f974413965
This commit is contained in:
Gaudenz Steinlin 2016-10-11 09:17:16 +02:00
parent 28d36d379c
commit ed8e4ca621
3 changed files with 22 additions and 4 deletions

View File

@ -6,12 +6,12 @@ set -x
function insert_host_entry {
ENTRY=$1
ADDR=$2
if ! grep -q -o "^${ENTRY}$" /etc/hosts; then
if [[ "$(grep "^${ADDR}\b" /etc/hosts | wc -l)" -ge "2" ]]; then
sed -i "/^${ADDR}\b/d" /etc/hosts
echo "${ENTRY}" | tee -a /etc/hosts
elif ! grep -q -o "^${ADDR}\b" /etc/hosts; then
elif grep -q "^${ADDR}\b" /etc/hosts; then
sed -i "s|^${ADDR}\b\ .*|${ENTRY}|" /etc/hosts
elif [[ "$(grep -o "^${ADDR}\b" /etc/hosts | wc -l)" -ge "2" ]]; then
sed -i "/^${IPADDR}\b/d" /etc/hosts
elif ! grep -q "^${ENTRY}$" /etc/hosts; then
echo "${ENTRY}" | tee -a /etc/hosts
fi
}

View File

@ -2,10 +2,12 @@
localhost ansible_host=127.0.0.1 ansible_become=True
test1 ansible_host=127.111.111.101 ansible_become=True
test2 ansible_host=127.111.111.102 ansible_become=True
test3 ansible_host=127.111.111.103 ansible_become=True
[all_containers]
test1
test2
test3
[hosts]
localhost

View File

@ -13,6 +13,20 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Playbook for test setup
hosts: localhost
connection: local
gather_facts: false
tasks:
- name: Add entries to hosts file
blockinfile:
dest: /etc/hosts
block: |
127.111.111.102 test2
127.111.111.103 test3.openstack.local
127.111.111.103 test3.additional
marker: ""
- include: "common/test-install-openstack-hosts.yml"
- name: Playbook for role testing
@ -61,6 +75,8 @@
- "'vm.swappiness' in sysctl_content"
- "'127.111.111.101 test1.openstack.local test1' in hosts_content"
- "'127.111.111.102 test2.openstack.local test2' in hosts_content"
- "'127.111.111.103 test3.openstack.local test3' in hosts_content"
- "'127.111.111.103 test3.additional' not in hosts_content"
- "'127.0.1.1 localhost.openstack.local localhost' in hosts_content"
- "release_file.stat.exists"
- "systat_file.stat.exists"