openstack-ansible-openstack.../templates/openstack-host-hostfile-setup.sh.j2
Jimmy McCrory a436640aa7 Generate 127.0.1.1 entry on each individual host
The hostfile update script is currently being generated on the
deployment host and copied to all other hosts within an environment.
This is resulting in the 127.0.1.1 entry always containing the hostname
of the deployment host.

Instead, generate a temporary file for the script locally which
pre-populates all shared hosts entries and use that as a template for
assigning the 127.0.1.1 entry on each remote host.

Change-Id: Ia5e375772cab76c748cca058ae7d7944e7528020
Closes-Bug: #1657568
2017-01-25 15:12:43 -08:00

57 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
}
{% raw -%}
{% 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 }}"
{% endraw %}
{% 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}'