Files
openstack-ansible-openstack…/templates/openstack-host-hostfile-setup.sh.j2
Gaudenz Steinlin ed8e4ca621 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
2016-10-11 09:18:06 +02:00

55 lines
2.0 KiB
Django/Jinja

#!/usr/bin/env bash
# {{ ansible_managed }}
set -x
function insert_host_entry {
ENTRY=$1
ADDR=$2
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 "^${ADDR}\b" /etc/hosts; then
sed -i "s|^${ADDR}\b\ .*|${ENTRY}|" /etc/hosts
elif ! grep -q "^${ENTRY}$" /etc/hosts; then
echo "${ENTRY}" | tee -a /etc/hosts
fi
}
function host_update {
ANSHOSTNAME=$1
RFCHOSTNAME=$2
INVHOSTNAME=$3
IPADDR=$4
DOMAINNAME=$5
if [[ "${ANSHOSTNAME}" != "${RFCHOSTNAME}" ]] && [[ "${RFCHOSTNAME}" != "${INVHOSTNAME}" ]]; then
insert_host_entry "${IPADDR} ${RFCHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${INVHOSTNAME} ${ANSHOSTNAME}" "${IPADDR}"
elif [[ "${ANSHOSTNAME}" != "${RFCHOSTNAME}" ]] && [[ "${RFCHOSTNAME}" == "${INVHOSTNAME}" ]]; then
insert_host_entry "${IPADDR} ${RFCHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${ANSHOSTNAME}" "${IPADDR}"
elif [[ "${ANSHOSTNAME}" == "${RFCHOSTNAME}" ]] && [[ "${RFCHOSTNAME}" == "${INVHOSTNAME}" ]]; then
insert_host_entry "${IPADDR} ${RFCHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME}" "${IPADDR}"
else
insert_host_entry "${IPADDR} ${RFCHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${INVHOSTNAME}" "${IPADDR}"
fi
}
{% set host_rfc_1034_1035_name = inventory_hostname|replace('_', '-') %}
host_update "{{ ansible_hostname|default(host_rfc_1034_1035_name) }}" \
"{{ host_rfc_1034_1035_name }}" \
"{{ inventory_hostname }}" \
"127.0.1.1" \
"{{ openstack_domain }}"
{% for item in groups['all'] %}
{% set target_rfc_1034_1035_name = item|replace('_', '-') %}
host_update "{{ hostvars[item]['ansible_hostname']|default(target_rfc_1034_1035_name) }}" \
"{{ target_rfc_1034_1035_name }}" \
"{{ item }}" \
"{{ hostvars[item]['ansible_host'] | default("127.0.0.1") }}" \
"{{ openstack_domain }}"
{% endfor %}
md5sum /etc/hosts|awk '{print $1}'