Use blockinfile for hosts file generation

This patch aims to simplify generation of hosts
file content as it's now generated purely with ansible.

As a result upgrade jobs should be fixed afterwards

Change-Id: I7961115f215153515ba3f3a00bbbeeb9fb4568f1
This commit is contained in:
Dmitriy Rabotyagov 2020-02-21 17:02:36 +02:00 committed by Dmitriy Rabotyagov (noonedeadpunk)
parent 8c0cededa9
commit c64e1caf72
4 changed files with 34 additions and 73 deletions

View File

@ -0,0 +1,7 @@
---
upgrade:
- |
Generation of records for `/etc/hosts` is now made with blockinfile
ansible module. During upgrade you will have records doubled in yours
`/etc/hosts` as we don't drop previously created records for safety
reasons if `openstack_host_manage_hosts_file` is set to true.

View File

@ -13,31 +13,32 @@
# See the License for the specific language governing permissions and
# limitations under the License.
- name: Drop hosts file entries script locally
template:
src: "openstack-host-hostfile-setup.sh.j2"
dest: "/var/tmp/openstack-host-hostfile-setup.sh"
mode: "0755"
delegate_to: localhost
- name: Generate hosts file records
run_once: true
vars:
ansible_python_interpreter: >-
"{{ (hostvars['localhost']['ansible_distribution'] == 'CentOS' and
hostvars['localhost']['ansible_distribution_major_version'] is version('7', '<='))
| ternary('/usr/bin/python', '/usr/bin/python3') }}"
- name: Copy templated hosts file entries script
template:
src: "/var/tmp/openstack-host-hostfile-setup.sh"
dest: "/usr/local/bin/openstack-host-hostfile-setup.sh"
mode: "0755"
- name: Stat host file
stat:
path: /etc/hosts
register: stat_hosts
set_fact:
_etc_hosts_content: |-
{% set records = [] %}
{% for item in groups['all'] %}
{% set record = [] %}
{% set _target_rfc_name = item|replace('_', '-') %}
{% set _ans_hostname = hostvars[item]['ansible_hostname'] | default(_target_rfc_name) %}
{% set _ = record.append(hostvars[item]['ansible_host'] | default('127.0.0.1')) %}
{% set _ = record.append(_ans_hostname ~ '.' ~ openstack_domain) %}
{% set _ = record.append(_target_rfc_name) %}
{% if (_ans_hostname != _target_rfc_name) and (_target_rfc_name != item) %}
{% set _ = record.append(item) %}
{% set _ = record.append(_ans_hostname) %}
{% elif (_ans_hostname != _target_rfc_name) and (_target_rfc_name == item) %}
{% set _ = record.append(_ans_hostname) %}
{% elif (_ans_hostname == _target_rfc_name) and (_target_rfc_name != item) %}
{% set _ = record.append(item) %}
{% endif %}
{% set _ = records.append(record | join(' ')) %}
{% endfor %}
{{ records }}
- name: Update hosts file
command: "/usr/local/bin/openstack-host-hostfile-setup.sh"
register: update_hosts
changed_when: not stat_hosts.stat.exists or stat_hosts.stat.checksum | string != update_hosts.stdout | string
blockinfile:
dest: /etc/hosts
block: "{{ _etc_hosts_content | join('\n') }}"
marker: "### {mark} OPENSTACK-ANSIBLE MANAGED BLOCK ###"

View File

@ -1,47 +0,0 @@
#!/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} ${ANSHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${INVHOSTNAME} ${ANSHOSTNAME}" "${IPADDR}"
elif [[ "${ANSHOSTNAME}" != "${RFCHOSTNAME}" ]] && [[ "${RFCHOSTNAME}" == "${INVHOSTNAME}" ]]; then
insert_host_entry "${IPADDR} ${ANSHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${ANSHOSTNAME}" "${IPADDR}"
elif [[ "${ANSHOSTNAME}" == "${RFCHOSTNAME}" ]] && [[ "${RFCHOSTNAME}" == "${INVHOSTNAME}" ]]; then
insert_host_entry "${IPADDR} ${ANSHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME}" "${IPADDR}"
else
insert_host_entry "${IPADDR} ${ANSHOSTNAME}.${DOMAINNAME} ${RFCHOSTNAME} ${INVHOSTNAME}" "${IPADDR}"
fi
}
{% 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 %}
sha1sum /etc/hosts|awk '{print $1}'

View File

@ -30,7 +30,7 @@
127.111.111.102 test2
127.111.111.103 test3.openstack.local
127.111.111.103 test3.additional
marker: ""
marker: "### {mark} OPENSTACK-ANSIBLE MANAGED BLOCK ###"
changed_when: false
when:
- "'idempotence' not in lookup('env', 'ANSIBLE_LOG_PATH')"