From 469aebd8dcfbbc5f8c7d292f5ee651d9e1596bfc Mon Sep 17 00:00:00 2001 From: XaverStiensmeier Date: Tue, 5 Dec 2023 09:48:51 +0100 Subject: [PATCH] simplified update_hosts --- .../roles/bibigrid/files/slurm/create_server.py | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/resources/playbook/roles/bibigrid/files/slurm/create_server.py b/resources/playbook/roles/bibigrid/files/slurm/create_server.py index 6b3bd1d6..dbca77b8 100644 --- a/resources/playbook/roles/bibigrid/files/slurm/create_server.py +++ b/resources/playbook/roles/bibigrid/files/slurm/create_server.py @@ -142,16 +142,12 @@ def update_hosts(name, ip): # pylint: disable=invalid-name @param ip: ibibigrid-worker0-3k1eeysgetmg4vb-3p address @return: """ - hosts = {"host_entries": {}} - if os.path.isfile(HOSTS_FILE_PATH): - with open(HOSTS_FILE_PATH, mode="r", encoding="utf-8") as hosts_file: - hosts = yaml.safe_load(hosts_file) - if hosts is None or "host_entries" not in hosts.keys(): - hosts = {"host_entries": {}} - - hosts["host_entries"][name] = ip - with open(HOSTS_FILE_PATH, mode="w", encoding="utf-8") as hosts_file: + with open(HOSTS_FILE_PATH, mode="w+", encoding="utf-8") as hosts_file: + hosts = yaml.safe_load(hosts_file) + if not hosts or "host_entries" not in hosts: + hosts = {"host_entries": {}} + hosts["host_entries"][name] = ip yaml.dump(hosts, hosts_file)