YAML格式的语法

Posted milton

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了YAML格式的语法相关的知识,希望对你有一定的参考价值。

基本格式

  • 用空格缩进, 不能用tab
  • 用#标记注释
  • 列表: 用短划(-)标记元素
  • 映射: 用冒号(:)分隔key, value. 如果写在一行, 需要用逗号分隔并前后加花括号
  • 字符串: 不加引号, 加单引号或者加双引号都可以, 加双引号时可以使用开头的转义字符
  • 多行字符串可以用 | 或 > 符号, 紧接着换行符
  • 重复的节点, 可以用 & 标识, 并用 * 来引用

基本元素

列表

# 普通
 - Casablanca
 - North by Northwest
 - The Man Who Wasn‘t There

# 单行
[milk, pumpkin pie, eggs, juice]
men: [John Smith, Bill Jones]
women:
  - Mary Smith
  - Susan Williams

 

映射

# Indented Block
   name: John Smith
   age: 33
# Inline Block
 {name: John Smith, age: 33}
- {name: John Smith, age: 33}
- name: Mary Smith
  age: 27

 

多行字符串

data: |
   There once was a short man from Ealing
   Who got on a bus to Darjeeling
       It said on the door
       "Please don‘t spit on the floor"
   So he carefully spat on the ceiling

data: >
   Wrapped text
   will be folded
   into a single
   paragraph

   Blank lines denote
   paragraph breaks

 

指定类型

a: 123                     # an integer
b: "123"                   # a string, disambiguated by quotes
c: 123.0                   # a float
d: !!float 123             # also a float via explicit data type prefixed by (!!)
e: !!str 123               # a string, disambiguated by explicit type
f: !!str Yes               # a string via explicit type
g: Yes                     # a boolean True (yaml1.1), string "Yes" (yaml1.2)
h: Yes we have No bananas  # a string, "Yes" and "No" disambiguated by context.

picture: !!binary |
  R0lGODdhDQAIAIAAAAAAANn
  Z2SwAAAAADQAIAAACF4SDGQ
  ar3xxbJ9p0qa7R0YxwzaFME
  1IAADs=
myObject:  !myClass { name: Joe, age: 15 }

 


以上是关于YAML格式的语法的主要内容,如果未能解决你的问题,请参考以下文章

03.yaml语法和playbook写法

Kubernetes------YAML

Y服务-你真的懂 Yaml 吗

yaml语法

ansible-YAML语法

YAML概述