Scenario
A microservices application uses two YAML configuration files: a base file with defaults and an overrides file for environment-specific settings.
Task
Write a Python script named merge_config.py in /home/interview/ that accepts two YAML file paths as command-line arguments (base and overrides), performs a deep merge, then extracts the database section and saves it to /home/interview/database_config.json.
The script will be executed as: python3 /home/interview/merge_config.py /home/interview/base.yaml /home/interview/overrides.yaml
Note: The PyYAML library is already installed.
Example
Expected output format in /home/interview/database_config.json:
{
"primary": {
"host": "prod-db-primary.example.com",
"port": 5432,
...
},
"replica": {
...
}
}
Step 1: Create the Python script
nano /home/interview/merge_config.py
Write a script that deep merges YAML files and extracts the database section:
import yaml
import json
import sys
def deep_merge(base, override):
"""Recursively merge override into base"""
result = base.copy()
for key, value in override.items():
if key in result and isinstance(result[key], dict) and isinstance(value, dict):
result[key] = deep_merge(result[key], value)
else:
result[key] = value
return result
# Load base configuration
with open(sys.argv[1], 'r') as f:
base_config = yaml.safe_load(f)
# Load overrides configuration
with open(sys.argv[2], 'r') as f:
overrides_config = yaml.safe_load(f)
# Deep merge configurations
merged_config = deep_merge(base_config, overrides_config)
# Extract database section
database_config = merged_config['database']
# Save to JSON
with open('/home/interview/database_config.json', 'w') as f:
json.dump(database_config, f, indent=2)
print("Configuration merged and database section extracted")
The deep_merge function recursively merges nested dictionaries, preserving values that only exist in the base config while applying overrides.
Step 2: Run the script
python3 /home/interview/merge_config.py /home/interview/base.yaml /home/interview/overrides.yaml
Step 3: Verify the output
cat /home/interview/database_config.json
The output should show the database configuration with production values merged in (e.g., prod-db-primary.example.com for primary host, max_connections: 50).