后浪笔记一零二四

yaml语法

yaml中的折叠块标量和文本块标量(literal block scalar)

  • 文本块标量: |
    • 文本块标量会包含换行符和每行尾随的空格。
  • 折叠块标量: >
    • 折叠块标量会折叠换行符为空格。
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
include_newlines: |
  exactly as you see
  will appear these three
  lines of poetry  

fold_newlines: >
  this is really a
  single line of text
  despite appearances  

fold_some_newlines: >
  a
  b

  c
  d
    e
  f  
same_as: "a b\nc d\n  e\nf\n"

示例

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
---
receipt:     Oz-Ware Purchase Invoice
date:        2012-08-06
customer:
    given:   Dorothy
    family:  Gale
   
items:
    - part_no:   A4786
      descrip:   Water Bucket (Filled)
      price:     1.47
      quantity:  4

    - part_no:   E1628
      descrip:   High Heeled "Ruby" Slippers
      size:      8
      price:     133.7
      quantity:  1

bill-to:  &id001    # 锚点
    street: | 
            123 Tornado Alley
            Suite 16
    city:   East Centerville
    state:  KS

ship-to:  *id001    # 引用

specialDelivery:  >
    Follow the Yellow Brick
    Road to the Emerald City.
    Pay no attention to the
    man behind the curtain.    
...

在yaml中,字符串不一定要用双引号标识,另外,在缩进中空白字符的数目并不是非常重要,只要相同层次结构的元素左侧对齐就可以了(不过不能使用TAB字符)。

在一个文件中,可以同时包含多个文件,并用"—“分割。选择性的符号”…“可以用来表示文件结尾(在利用流的通信中,这非常重要,可以在不关闭流的情况下,发送结束信号)

yaml的基本组件

1 清单(数组)

常用区块格式(block format)来表示,也就是用短杠+空白字符作为起始。

1
2
3
4
--- # 最喜欢的电影
- Casablanca
- North by Northwest
- Notorious

也可以用内置格式(inline format)来表示,用方括号包围,多个元素以"逗号+空格"分割。

1
2
--- # 货物清单
[milk, pumpkin pie, eggs, juice]

2. 散列表

--- #区块形式
name: John Smith
age: 33
--- #内置形式
{name: John Smith, age: 33}

3. 区块的字符

有两种方法表示多行文字,一种可以保存新行(使用|字符),另一种可以折叠新行(使用>字符)。

  • 保存新行(newlines preserved)
1
2
3
4
5
6
data: |
  There once was a man from Darjeeling
  Who got on a bus bound for Ealing
    It said on the door
    "Please don't spit on the floor"
  So he carefully spat on the ceiling  

根据设置,前方的引领空白符号(leading white space)必须排成条状,以便和其他数据或行为明显区分。

  • 折叠新行(newlines folded)
1
2
3
4
5
6
7
8
data: >
  Wrapped text
  will be folded
  into a single
  paragraph

  Blank lines denote
  paragraph breaks  

和保存新行不同的是,换行符会被替换为空白字符。

4. 层次结构化的元素

  • 于清单中使用散列表
1
2
3
- {name: John Smith, age: 33}
- name: Mary Smith
  age: 27
  • 于散列表中使用清单
1
2
3
4
men: [John Smith, Bill Jones]
women: 
  - Mary Smith
  - Susan Williams

yaml的高级组件

1. 数据合并和引用

refer(*)会将树状结构加入锚点标记的内容,并可以在所有数据结构中运作。 合并(<<)只有散列表中可以使用,可以将键值由锚点标记复制到指定的散列表中。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
---
- step: &id001
    instrument:   Lasik 2000
    pulseEnergy:  5.4
    pulseDuration: 12
    repetition:   1000
    spotSize:     1mm
- step:
    <<: *id001
    spotSize:     2mm   #覆盖"spotSize"的值
- step:
    <<: *id001    
    pulseEnergy:   500.0  # 覆盖键值
    alert: >              # 加入其他键值
      warn patient of
      andible pop

kubectl模板的使用

1
2
3
for a in ${aa[@]}; do echo $a; kubectl get deploy $a -n lanjing-test -o go-template --template '{{range $key,$val:=  .spec.template.spec.volumes }}{{range $key1,$val1 := $val}}{{if eq $key1 "configMap"}}{{ println $val1.name }}{{end}}{{end}}{{end}}' >> test.txt; done

cat test.txt | uniq  > test.txt

专题:

本文发表于 2022-09-13,最后修改于 2022-09-13。

本站永久域名「 jiavvc.top 」,也可搜索「 后浪笔记一零二四 」找到我。


上一篇 « 时间复杂度分析 下一篇 » 如何看懂源码

赞赏支持

请我吃鸡腿 =^_^=

i ysf

云闪付

i wechat

微信

推荐阅读

Big Image