Merge "Use blockinfile for hosts file generation"

This commit is contained in:
Zuul 2020-03-11 14:04:50 +00:00 committed by Gerrit Code Review
commit 751cc4e3b1
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 # See the License for the specific language governing permissions and
# limitations under the License. # limitations under the License.
- name: Drop hosts file entries script locally - name: Generate hosts file records
template:
src: "openstack-host-hostfile-setup.sh.j2"
dest: "/var/tmp/openstack-host-hostfile-setup.sh"
mode: "0755"
delegate_to: localhost
run_once: true run_once: true
vars: set_fact:
ansible_python_interpreter: >- _etc_hosts_content: |-
"{{ (hostvars['localhost']['ansible_distribution'] == 'CentOS' and {% set records = [] %}
hostvars['localhost']['ansible_distribution_major_version'] is version('7', '<=')) {% for item in groups['all'] %}
| ternary('/usr/bin/python', '/usr/bin/python3') }}" {% set record = [] %}
{% set _target_rfc_name = item|replace('_', '-') %}
- name: Copy templated hosts file entries script {% set _ans_hostname = hostvars[item]['ansible_hostname'] | default(_target_rfc_name) %}
template: {% set _ = record.append(hostvars[item]['ansible_host'] | default('127.0.0.1')) %}
src: "/var/tmp/openstack-host-hostfile-setup.sh" {% set _ = record.append(_ans_hostname ~ '.' ~ openstack_domain) %}
dest: "/usr/local/bin/openstack-host-hostfile-setup.sh" {% set _ = record.append(_target_rfc_name) %}
mode: "0755" {% if (_ans_hostname != _target_rfc_name) and (_target_rfc_name != item) %}
{% set _ = record.append(item) %}
- name: Stat host file {% set _ = record.append(_ans_hostname) %}
stat: {% elif (_ans_hostname != _target_rfc_name) and (_target_rfc_name == item) %}
path: /etc/hosts {% set _ = record.append(_ans_hostname) %}
register: stat_hosts {% 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 - name: Update hosts file
command: "/usr/local/bin/openstack-host-hostfile-setup.sh" blockinfile:
register: update_hosts dest: /etc/hosts
changed_when: not stat_hosts.stat.exists or stat_hosts.stat.checksum | string != update_hosts.stdout | string 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.102 test2
127.111.111.103 test3.openstack.local 127.111.111.103 test3.openstack.local
127.111.111.103 test3.additional 127.111.111.103 test3.additional
marker: "" marker: "### {mark} OPENSTACK-ANSIBLE MANAGED BLOCK ###"
changed_when: false changed_when: false
when: when:
- "'idempotence' not in lookup('env', 'ANSIBLE_LOG_PATH')" - "'idempotence' not in lookup('env', 'ANSIBLE_LOG_PATH')"