diff --git a/instack_undercloud/tests/test_undercloud.py b/instack_undercloud/tests/test_undercloud.py index bbbda0176..de832cd0b 100644 --- a/instack_undercloud/tests/test_undercloud.py +++ b/instack_undercloud/tests/test_undercloud.py @@ -203,7 +203,8 @@ class TestCheckHostname(BaseTestCase): undercloud._check_hostname() mock_run_command.assert_called_with([ 'sudo', '/bin/bash', '-c', - 'echo 127.0.0.1 test.hostname test >> /etc/hosts'], + 'sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1test.hostname test /" ' + '/etc/hosts'], name='hostname-to-etc-hosts') @mock.patch('instack_undercloud.undercloud._run_command') @@ -219,7 +220,8 @@ class TestCheckHostname(BaseTestCase): undercloud._check_hostname() mock_run_command.assert_called_with([ 'sudo', '/bin/bash', '-c', - 'echo 127.0.0.1 test.hostname test >> /etc/hosts'], + 'sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1test.hostname test /" ' + '/etc/hosts'], name='hostname-to-etc-hosts') @mock.patch('instack_undercloud.undercloud._run_command') @@ -237,7 +239,9 @@ class TestCheckHostname(BaseTestCase): undercloud._check_hostname() mock_run_command.assert_called_with([ 'sudo', '/bin/bash', '-c', - 'echo 127.0.0.1 test-hostname.domain test-hostname >> /etc/hosts'], + 'sed -i "s/127.0.0.1\(\s*\)/' + '127.0.0.1\\1test-hostname.domain test-hostname /" ' + '/etc/hosts'], name='hostname-to-etc-hosts') @mock.patch('instack_undercloud.undercloud._run_command') diff --git a/instack_undercloud/undercloud.py b/instack_undercloud/undercloud.py index d657c3503..8e2b9be71 100644 --- a/instack_undercloud/undercloud.py +++ b/instack_undercloud/undercloud.py @@ -636,9 +636,10 @@ def _check_hostname(): if short_hostname == detected_static_hostname: raise RuntimeError('Configured hostname is not fully ' 'qualified.') - echo_cmd = ('echo 127.0.0.1 %s %s >> /etc/hosts' % - (detected_static_hostname, short_hostname)) - args = ['sudo', '/bin/bash', '-c', echo_cmd] + sed_cmd = ('sed -i "s/127.0.0.1\(\s*\)/127.0.0.1\\1%s %s /" ' + '/etc/hosts' % + (detected_static_hostname, short_hostname)) + args = ['sudo', '/bin/bash', '-c', sed_cmd] _run_command(args, name='hostname-to-etc-hosts') LOG.info('Added hostname %s to /etc/hosts', detected_static_hostname) diff --git a/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml b/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml new file mode 100644 index 000000000..edea8c785 --- /dev/null +++ b/releasenotes/notes/set-valid-hosts-file-49d6aa96365908a7.yaml @@ -0,0 +1,7 @@ +--- +fixes: + - When the hostname was written to /etc/hosts, it resulted in an invalid + /etc/hosts file due to 127.0.0.1 being specified twice on different lines. + That issue is now corrected such that the hostnames will be added to the + existing line for 127.0.0.1, which results in valid syntax for /etc/hosts. + See https://bugs.launchpad.net/tripleo/+bug/1709460