11import yaml
22import logging
3+ from core .utils .env_expander import (
4+ expand_env_vars ,
5+ ) # if you put the function above in env_expander.py
36
47logger = logging .getLogger (__name__ )
58
@@ -10,29 +13,36 @@ class TopologyLoaderError(Exception):
1013
1114class TopologyLoader :
1215 """
13- Loads containerlab topology data from a YAML file.
16+ Loads containerlab topology data from a YAML file, expanding environment variables .
1417 """
1518
1619 def load (self , input_file : str ) -> dict :
1720 """
18- Load the containerlab YAML topology file.
21+ Load the containerlab YAML topology file, including environment-variable expansion .
1922
2023 :param input_file: Path to the containerlab YAML file.
21- :return: Parsed containerlab topology data.
24+ :return: Parsed containerlab topology data with env variables expanded .
2225 :raises TopologyLoaderError: If file not found or parse error occurs.
2326 """
2427 logger .debug (f"Loading topology from file: { input_file } " )
2528 try :
2629 with open (input_file , "r" ) as file :
27- containerlab_data = yaml .safe_load (file )
30+ raw_content = file .read ()
31+
32+ # Expand ${VAR:=default} placeholders before parsing
33+ expanded_content = expand_env_vars (raw_content )
34+
35+ containerlab_data = yaml .safe_load (expanded_content )
2836 logger .debug ("Topology successfully loaded." )
2937 return containerlab_data
38+
3039 except FileNotFoundError :
3140 error_message = (
3241 f"Error: The specified clab file '{ input_file } ' does not exist."
3342 )
3443 logger .error (error_message )
3544 raise TopologyLoaderError (error_message )
45+
3646 except Exception as e :
3747 error_message = f"An error occurred while loading the config: { e } "
3848 logger .error (error_message )
0 commit comments