Only set hostname on 127.0.0.1 if not present in /etc/hosts

The current code appends hostname to 127.0.0.1 line on /etc/hosts
if not set already.
That approach is wrong, 127.0.0.1 should just contain localhost and
in case there's no physical interface besides loopback then it should
be added 127.0.1.1 <hostname>.
This behaviour also breaks nodes managed by puppet with physical interfaces
configured, like the baremetal00 node.
If I set the /etc/hosts to:

127.0.0.1 localhost
15.184.64.4 baremetal00.vanilla.ic.openstack.org baremetal00

and run the install playbook, then baremetal00 is added to the
127.0.0.1 line, which makes puppet 'facter fqdn' command to return
'baremetal00' and thus puppet apply runs no longer work on the node
as the site.pp contains the fqdn name for the node.

This change simply greps for hostname on /etc/hosts and if not present
then it continues the current (even wrong imho) of adding hostname
to 127.0.0.1

For reference: https://www.debian.org/doc/manuals/debian-reference/ch05.en.html#_the_hostname_resolution

Change-Id: I47f0a9c6777809911398ca25c66b83f8ee0221ec
This commit is contained in:
Ricardo Carrillo Cruz 2016-08-16 10:11:54 +02:00
parent c6fc3b01c2
commit 5506f5e327

View File

@ -30,7 +30,7 @@
- name: "Start rabbitmq-server"
service: name=rabbitmq-server state=started
- name: "RabbitMQ - Testing if hostname is defined in /etc/hosts"
command: grep -i "127.0.0.1.*{{ ansible_hostname }}\ localhost" /etc/hosts
command: grep -i "{{ ansible_hostname }}" /etc/hosts
ignore_errors: yes
register: test_grep_fix_hostname
# NOTE(sean-k-mooney) in a docker container this will fail so /etc/hosts