diff options
-rwxr-xr-x | src/conf_mode/host_name.py | 10 |
1 files changed, 8 insertions, 2 deletions
diff --git a/src/conf_mode/host_name.py b/src/conf_mode/host_name.py index f6d091ac2..11612637c 100755 --- a/src/conf_mode/host_name.py +++ b/src/conf_mode/host_name.py @@ -81,8 +81,14 @@ def generate(config): old_hostname = subprocess.check_output(['hostname']).decode().strip() # replace the local host line - hosts = re.sub(r"({}\s+{}.*)".format(local_addr, old_hostname), - r"{}\t{} # VyOS entry\n".format(local_addr, config["fqdn"]), hosts) + vyos_host_line_re = re.compile(r"({}\s+{}.*)".format(local_addr, old_hostname)) + vyos_host_line = "{}\t{} # VyOS entry\n".format(local_addr, config["fqdn"]) + if re.search(vyos_host_line_re, hosts): + hosts = re.sub(vyos_host_line_re, vyos_host_line, hosts) + else: + # On boot (or after errors), the /etc/hosts file has no line for vyos hostname, + # so we have to add it + hosts = "{0}\n{1}".format(hosts, vyos_host_line) with open(hosts_file, 'w') as f: f.write(hosts) |