在json和yaml格式之间转换aws cloudformation模板
cfn-flip的Python项目详细描述
AWS云信息模板翻转
关于
aws cloudformation template flip是一个在JSON和YAML格式之间转换AWS CloudFormation模板的工具,尽可能使用yaml格式的短函数语法。
术语“flip”的灵感来自于著名的unix命令行工具flip,它可以在unix、mac和ms-dos格式之间转换文本文件。
安装
可以使用pip安装AWS CloudFormation Template Flip:
pip install cfn_flip
用法
aws cloudformation template flip既是一个命令行工具,也是一个python库。
注意,命令行工具的拼写为带连字符的cfn-flip
,而python包的拼写为带下划线的cfn_flip
命令行工具
Usage: cfn-flip [OPTIONS] [INPUT] [OUTPUT]
AWS CloudFormation Template Flip is a tool that converts AWS
CloudFormation templates between JSON and YAML formats, making use of the
YAML format's short function syntax where possible."
Options:
-i, --input [json|yaml] Specify the input format. Overrides -j and -y
flags.
-o, --output [json|yaml] Specify the output format. Overrides -j, -y, and
-n flags.
-j, --json Convert to JSON. Assume the input is YAML.
-y, --yaml Convert to YAML. Assume the input is JSON.
-c, --clean Performs some opinionated cleanup on your
template.
-l, --long Use long-form syntax for functions when converting
to YAML.
-n, --no-flip Perform other operations but do not flip the
output format.
--version Show the version and exit.
--help Show this message and exit.
cfn flip将检测输入模板的格式,并分别将JSON转换为YAML和YAML转换为JSON
示例:
从
stdin
读取并输出到stdout
:cat examples/test.json | cfn-flip
从文件读取并输出到
stdout
:cfn-flip examples/test.yaml
从文件读取并输出到另一个文件:
cfn-flip examples/test.json output.yaml
读取文件并清除输出
cfn-flip -c examples/test.json
python包
要从您自己的python项目中使用AWS CloudFormation Template Flip,请根据需要导入函数flip
、to_yaml
或to_json
fromcfn_flipimportflip,to_yaml,to_json"""All functions expect a string containing serialised dataand return a string containing serialised dataor raise an exception if there is a problem parsing the input"""# flip takes a best guess at the serialisation format# and returns the opposite, converting json into yaml and vice versasome_yaml_or_json=flip(some_json_or_yaml)# to_json expects serialised yaml as input, and returns serialised jsonsome_json=to_json(some_yaml)# to_yaml expects serialised json as input, and returns serialised yamlsome_yaml=to_yaml(some_json)# The clean_up flag performs some opinionated, CloudFormation-specific sanitation of the input# For example, converting uses of Fn::Join to Fn::Sub# flip, to_yaml, and to_json all support the clean_up flagclean_yaml=to_yaml(some_json,clean_up=True)