# introduction_yaml **Repository Path**: xiaolixi/introduction_yaml ## Basic Information - **Project Name**: introduction_yaml - **Description**: 介绍yaml语言 - **Primary Language**: YAML - **License**: Not specified - **Default Branch**: master - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2024-09-21 - **Last Updated**: 2025-07-21 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # introduction_yaml ## 介绍 在日常的工作当中,我们经常会使用yaml标记语言。 然而在日常工作使用的都是比较简单的场景,对于一些复杂场景下,似乎我们掌握的yaml语法就不够了。 例如.gitlab-ci.yml文件、k8s的yaml文件或者其他项目的yaml文件。好像他们都变的复杂起来了。 所以这里想要系统的介绍yaml标记语言。 ## yaml语言是什么 YAML 是 "YAML Ain't a Markup Language"(YAML 不是一种标记语言)的递归缩写。在开发的这种语言时,YAML 的意思其实是:"Yet Another Markup Language"(仍是一种标记语言)。 YAML 的语法和其他高级语言类似,并且可以表达简单,它使用空白符号缩进标示逻辑关系。 YAML 的配置文件后缀为 .yaml 或 .yml Because of the history of YAML the implementations differ a lot. There are three versions, 1.0, 1.1 and 1.2. The specification for version 1.1 had problems like ambiguity 1.2 was created to solve the problems with 1.1, but already many implementations had been created and were never updated. There was no test suite until 2016 ![输入图片说明](Snipaste_2024-09-21_11-36-25.png) [ :pushpin: yaml info网站](https://www.yaml.info/index.html) [ :pushpin: yaml learn案例网站](https://learnxinyminutes.com/docs/yaml/) [ :pushpin: yaml json互转在线网站2](https://www.bairesdev.com/tools/json2yaml/) [ :pushpin: yaml json互转在线网站2](https://www.json.cn/yaml-editor/) ## yaml语法 ### 开始、结束标记 ```yaml --- # document start key: value ... # There is also the Document End Marker ... ``` ### 注释:# ```yaml --- # document start # Comments in YAML look like this. # YAML supports single-line comments. ################ # SCALAR TYPES # ################ # Our root object (which continues for the entire document) will be a map, # which is equivalent to a dictionary, hash or object in other languages. key: value ``` ### 基础类型: 数组、映射 ![输入图片说明](Snipaste_2024-09-21_12-44-12.png) ```yaml key: value number: 123 boolean: true null_value: null another_null_value: ~ key with spaces: value special_characters: "[ John ] & { Jane } - " single quotes: 'have ''one'' escape pattern' my_array: - v1 - v2 - - vv1 - vv2 - object_key: name: 'tom' age: 20 object: name: jarry age: 19 ``` ```json { "key": "value", "number": 123, "boolean": true, "null_value": null, "another_null_value": null, "key with spaces": "value", "special_characters": "[ John ] & { Jane } - ", "single quotes": "have 'one' escape pattern", "my_array": [ "v1", "v2", [ "vv1", "vv2" ], { "object_key": { "name": "tom", "age": 20 } } ], "object": { "name": "jarry", "age": 19 } } ``` ### 集合数组 ![输入图片说明](Snipaste_2024-09-21_12-36-53.png) ```yaml set1: ? item1 ? item2 ? item3 set2: { item1, item2, item3 } set3: item1: null item2: null item3: null ``` ```json { "set1": { "item1": null, "item2": null, "item3": null }, "set2": { "item1": null, "item2": null, "item3": null }, "set3": { "item1": null, "item2": null, "item3": null } } ``` ### tags的作用:use to explicitly declare types. > Syntax: !![typeName] [value] ![输入图片说明](Snipaste_2024-09-21_12-31-50.png) ```yaml explicit_boolean: !!bool true explicit_integer: !!int 42 explicit_float: !!float -42.24 explicit_string: !!str 0.5 explicit_datetime: !!timestamp 2022-11-17 12:34:56.78 +9 explicit_null: !!null null ``` ```json { "explicit_boolean": true, "explicit_integer": 42, "explicit_float": -42.24, "explicit_string": "0.5", "explicit_datetime": "2022-11-17T03:34:56.780Z", "explicit_null": null } ``` #### python/tuple、python/complex 在python里面可能还有这样的类型 ```yaml ? !!python/tuple [ 5, 7 ] : Fifty Seven ``` ### 锚点Anchors和引用Aliases - > 锚点Anchors:使用&标记 - > 引用Aliases:使用*引用 - > The expression << is called 'Merge Key Language-Independent Type'. It is used to indicate that all the keys of one or more specified maps should be inserted into the current map. :warning: NOTE: If key already exists alias will not be merged. ![输入图片说明](Snipaste_2024-09-21_12-23-26.png) ```yaml anchored_content: &anchor_name 'this is anchor' other_anchor: *anchor_name base: &base name: TOM JARRY foo: age: 10 <<: *base # doesn't merge the anchor name: John bar: <<: *base # base anchor will be merged age: 20 ``` ```json { "anchored_content": "this is anchor", "other_anchor": "this is anchor", "base": { "name": "TOM JARRY" }, "foo": { "age": 10, "name": "John" }, "bar": { "name": "TOM JARRY", "age": 20 } } ``` ### 类似json的写法 因为yaml是json的超集,因此在yaml中也可以写成类似json的格式。 ![输入图片说明](Snipaste_2024-09-21_12-11-09.png) ```yaml json_map: { "key": "value" } json_seq: [ 3, 2, 1, "takeoff" ] and quotes are optional: { key: [ 3, 2, 1, takeoff ] } ``` ```json { "json_map": { "key": "value" }, "json_seq": [ 3, 2, 1, "takeoff" ], "and quotes are optional": { "key": [ 3, 2, 1, "takeoff" ] } } ``` ### 复杂的key支持 #### 1. 数值key,其实还是字符串 ![输入图片说明](Snipaste_2024-09-21_11-58-06.png) ```yaml 0.25: a float key ``` ```json { "0.25": "a float key" } ``` #### 2. 复杂的key用`? `开始 ![输入图片说明](Snipaste_2024-09-21_12-04-39.png) ```yaml ? | This is a key that has multiple lines : value ? - Manchester United - Real Madrid : [ 2001-01-01, 2002-02-02 ] ``` ```json { "This is a key\nthat has multiple lines\n": "value", "Manchester United,Real Madrid": [ "2001-01-01T00:00:00.000Z", "2002-02-02T00:00:00.000Z" ] } ``` ### 多行文本:>、|、|-、|+、>-、>+ ```shell 1. >把多行文本转成1行 2. |把多行文本转成多行 3. -删除最后一行后面的换行符 4. +保留最后一行后面的换行符 ``` ![输入图片说明](Snipaste_2024-09-21_11-46-54.png) ```yml mutli_lines_1: > 只有一行 只有一行 只有一个换行符 mutli_lines_2: | 一行 一行 mutli_lines_3: |- 一行 一行 最后一行没有换行符 mutli_lines_4: |+ 一行 一行 最后一行可有多个换行符 yaml: 多行 ``` ```json { "mutli_lines_1": "只有一行 只有一行 只有一个换行符\n", "mutli_lines_2": "一行\n一行\n", "mutli_lines_3": "一行\n一行\n最后一行没有换行符", "mutli_lines_4": "一行\n一行\n最后一行可有多个换行符\n\n\n", "yaml": "多行" } ``` ## 第三方库 ![输入图片说明](Snipaste_2024-09-21_12-51-57.png) shell: yq