Kanji
・クラウドエンジニア / フリーランス ・1993年生まれ ・愛媛県出身 / 東京都渋谷区在住 ・AWS歴5年 プロフィールの詳細
目次
# noqa
max-line-length
pyyaml
samconfig.yml
parameter_overrides
CommadelimitedList
transform_parameter_overrides.py
import yaml import argparse def is_already_transformed(overrides): return all(isinstance(item, str) and "=" in item for item in overrides) def transform_parameter_overrides(data): for key, value in data.items(): if isinstance(value, dict) and "global" in value and "parameters" in value["global"]: parameters = value["global"]["parameters"] if "parameter_overrides" in parameters: if not is_already_transformed(parameters["parameter_overrides"]): new_overrides = [] for override in parameters["parameter_overrides"]: if isinstance(override, dict): for k, v in override.items(): if isinstance(v, list): v = ",".join(v) new_overrides.append(f"{k}={v}") parameters["parameter_overrides"] = new_overrides return data def main(input_file, output_file): with open(input_file, "r") as f: data = yaml.safe_load(f) transformed_data = transform_parameter_overrides(data) with open(output_file, "w") as f: yaml.dump(transformed_data, f, default_flow_style=False, sort_keys=False) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Transform parameter overrides in a YAML file.") parser.add_argument("--input-file", help="Path to the input YAML file.", default="samconfig.original.yml") parser.add_argument("--output-file", help="Path to the output YAML file.", default="samconfig.yml") args = parser.parse_args() main(args.input_file, args.output_file)
samconfig.original.yml
python transform_parameter_overrides.py
version: 0.1 dev: global: parameters: parameter_overrides: - Environment: dev - Whitelist: # Windows Update # 参考: https://learn.microsoft.com/en-us/windows-server/administration/windows-server-update-services/deploy/2-configure-wsus#211-configure-your-firewall-to-allow-your-first-wsus-server-to-connect-to-microsoft-domains-on-the-internet - .windowsupdate.microsoft.com - .update.microsoft.com - .windowsupdate.com - download.microsoft.com - .download.windowsupdate.com - wustat.windows.com - ntservicepack.microsoft.com - go.microsoft.com - dl.delivery.mp.microsoft.com - .delivery.mp.microsoft.com - .microsoft.com
Whitelist
version: 0.1 dev: global: parameters: parameter_overrides: - Environment=com1 - Whitelist=.windowsupdate.microsoft.com,.update.microsoft.com,.windowsupdate.com,download.microsoft.com,.download.windowsupdate.com,wustat.windows.com,ntservicepack.microsoft.com,go.microsoft.com,dl.delivery.mp.microsoft.com,.delivery.mp.microsoft.com,.microsoft.com
--cli-input-json
pyyaml json
parameter.original.yml
Parameters
transform_parameters.py
import yaml import json import argparse def transform_parameters(data): if "Parameters" in data: new_parameters = [] for param in data["Parameters"]: for key, value in param.items(): if isinstance(value, list): value = ",".join(value) new_parameters.append({"ParameterKey": key, "ParameterValue": value}) data["Parameters"] = new_parameters return data def main(input_file, output_file): with open(input_file, "r") as f: data = yaml.safe_load(f) transformed_data = transform_parameters(data) with open(output_file, "w") as f: json.dump(transformed_data, f, indent=4) if __name__ == "__main__": parser = argparse.ArgumentParser(description="Transform parameters in a YAML file.") parser.add_argument("--input-file", help="Path to the input YAML file.", default="parameter.original.yml") parser.add_argument("--output-file", help="Path to the output JSON file.", default="parameter.json") args = parser.parse_args() main(args.input_file, args.output_file)
parameter.json
python transform_parameters.py
Parameters: Environment: - dev Whitelist: - .windowsupdate.microsoft.com - .update.microsoft.com - .windowsupdate.com - download.microsoft.com - .download.windowsupdate.com - wustat.windows.com - ntservicepack.microsoft.com - go.microsoft.com - dl.delivery.mp.microsoft.com - .delivery.mp.microsoft.com - .microsoft.com
{ "Parameters": [ { "ParameterKey": "Environment", "ParameterValue": "dev" }, { "ParameterKey": "Whitelist", "ParameterValue": ".windowsupdate.microsoft.com,.update.microsoft.com,.windowsupdate.com,download.microsoft.com,.download.windowsupdate.com,wustat.windows.com,ntservicepack.microsoft.com,go.microsoft.com,dl.delivery.mp.microsoft.com,.delivery.mp.microsoft.com,.microsoft.com" } ] }