Created
May 5, 2022 00:35
-
-
Save jaumzors/94a8b93f5b8420c1759dab0cdc3382ea to your computer and use it in GitHub Desktop.
AWS Cloudformation template manipulation based on: https://stackoverflow.com/questions/50914422/parse-an-aws-cloudformation-template-with-the-pyyaml-library
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from cfn_tools import load_yaml | |
import cfn_flip.yaml_dumper | |
from cfn_flip import to_json | |
import yaml | |
import json | |
with open('k8s-cluster.yaml') as f: | |
raw = f.read() | |
print(type(raw)) | |
data_dict = load_yaml(raw) | |
json_resource = to_json(raw) | |
obj_dict = json.loads(json_resource) | |
obj_dict["Resources"]["k8svpc"]["Properties"]["Tags"].append({ | |
"Key": "aaaa", | |
"Value": "bbbb" | |
}) | |
print(obj_dict["Resources"]["k8svpc"]["Properties"]["Tags"]) | |
with open('output.template', 'w') as f: | |
dumper = cfn_flip.yaml_dumper.get_dumper(clean_up=False, long_form=False) | |
data_dict = load_yaml(json.dumps(obj_dict)) | |
raw = yaml.dump( | |
data_dict, | |
Dumper=dumper, | |
default_flow_style=False, | |
allow_unicode=True | |
) | |
f.write(raw) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment