<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          Kubernetes 之 YAML 語法

          共 12097字,需瀏覽 25分鐘

           ·

          2021-04-22 16:00

          YAML 是一種非常簡潔/強大/專門用來寫配置文件的語言!

          YAML 全稱是 ”YAML Ain’t a Markup Language” 的遞歸縮寫,該語言的設(shè)計參考了 JSON / XML 和 SDL 等語言,強調(diào)以數(shù)據(jù)為中心,簡潔易讀,編寫簡單。

          YAML 語法特性

          學(xué)過編程的人理解起來應(yīng)該非常容易

          語法特點

          • 大小寫敏感

          • 通過縮進表示層級關(guān)系

          • 禁止使用tab縮進,只能使用空格鍵

          • 縮進的空格數(shù)目不重要,只要相同層級左對齊

          • 使用#表示注釋

          # yaml
          languages:
              - Ruby
              - Perl
              - Python
          websites:
              YAML: yaml.org
              Ruby: ruby-lang.org
              Python: python.org
              Perl: use.perl.org

          # Json
          {
              languages: [
                  'Ruby',
                  'Perl',
                  'Python'
              ],
              websites: {
                  YAML: 'yaml.org',
                  Ruby: 'ruby-lang.org',
                  Python: 'python.org',
                  Perl: 'use.perl.org'
              }
          }

          數(shù)據(jù)結(jié)構(gòu)

          • 對象: 鍵值對的字典

          • 數(shù)組: 一組按次序排列的列表

          • 純量: 單個的且不可再分的值

          # 純量
          hello

          # 數(shù)組
          - Cat
          - Dog
          - Goldfish

          # 對象
          animal: pets

          引號區(qū)別

          • 單引號(''): 特殊字符作為普通字符串處理

          • 雙引號(""): 特殊字符作為本身想表示的意思

          # 單引號
          name: 'Hi,\nTom'

          # 雙引號
          name: "Hi,\nTom"

          內(nèi)置類型列表

          # YAML允許使用個感嘆號(!)強制轉(zhuǎn)換數(shù)據(jù)類型
          # 單嘆號通常是自定義類型,雙嘆號是內(nèi)置類型
          money: !!str
          123

          date: !Boolean
          true
          對應(yīng)編號內(nèi)置類型解釋說明
          1!!int整數(shù)類型
          2!!float浮點類型
          3!!bool布爾類型
          4!!str字符串類型
          5!!null空值
          6!!set集合
          7!!seq列表
          8!!map鍵值表
          9!!binary字符串類型
          10!!timestamp日期時間類型
          11!!omap/!!pairs鍵值列表

          YAML 中的純量

          純量是最基本的且不可再分的值

          字符串

          # 不適用引號
          name: Tom

          # 使用單引號
          name: 'Tom'

          # 使用雙引號
          name: "Tom"

          布爾值

          debug: true
          debug: false

          數(shù)字

          12       # 十進制整數(shù)
          014      # 八進制整數(shù)
          0xC      #十六進制整數(shù)
          13.4     #浮點數(shù)
          1.2e+34  #指數(shù)
          .inf     #無窮大

          Null

          date: ~
          date: null

          時間

          # 使用iso-8601標準表示日期
          date: 2018-01-01t16:59:43.10-05:00

          YAML 特殊類型

          日常使用中基本不會用到的類型

          文本塊

          # 注意“|”與文本之間須另起一行
          # 使用|標注的文本內(nèi)容縮進表示的塊,可以保留塊中已有的回車換行
          value: |
            hello
            world!

          # 輸出結(jié)果
          # hello 換行 world!!
          # +表示保留文字塊末尾的換行
          # -表示刪除字符串末尾的換行
          value: |
          hello

          value: |-
          hello

          value: |+
          hello

          # 輸出結(jié)果
          # hello\n hello hello\n\n
          # 注意“>”與文本之間的空格
          # 使用>標注的文本內(nèi)容縮進表示的塊,將塊中回車替換為空格最終連接成一行
          value: > hello
          world!

          # 輸出結(jié)果
          # hello 空格 world!

          錨點與引用

          # 復(fù)制代碼注意*引用部分不能追加內(nèi)容
          # 使用&定義數(shù)據(jù)錨點,即要復(fù)制的數(shù)據(jù)
          # 使用*引用錨點數(shù)據(jù),即數(shù)據(jù)的復(fù)制目的地
          name: &a yaml
          book: *a
          books:
             - java
             - *a
             - python

          # 輸出結(jié)果
          book: yaml
          books:[java, yaml, python]

          YAML 實例說明

          光說不練假把式 => JS-Yaml 官網(wǎng)實例地址 https://nodeca.github.io/js-yaml

          # Collection Types #############################################################
          ################################################################################

          # http://yaml.org/type/map.html -----------------------------------------------#

          map:
            # Unordered set of key: value pairs.
            Block style: !!map
              Clark: Evans
              Ingy: d?t Net
              Oren: Ben-Kiki
            Flow style: !!map { Clark: Evans, Ingy: d?t Net, Oren: Ben-Kiki }

          # http://yaml.org/type/omap.html ----------------------------------------------#

          omap:
            # Explicitly typed ordered map (dictionary).
            Bestiary: !!omap
              - aardvark: African pig-like ant eater. Ugly.
              - anteater: South-American ant eater. Two species.
              - anaconda: South-American constrictor snake. Scaly.
              # Etc.
            # Flow style
            Numbers: !!omap [one: 1, two: 2, three: 3]

          # http://yaml.org/type/pairs.html ---------------------------------------------#

          pairs:
            # Explicitly typed pairs.
            Block tasks: !!pairs
              - meeting: with team.
              - meeting: with boss.
              - break: lunch.
              - meeting: with client.
            Flow tasks: !!pairs [meeting: with team, meeting: with boss]

          # http://yaml.org/type/set.html -----------------------------------------------#

          set:
            # Explicitly typed set.
            baseball players: !!set
              ? Mark McGwire
              ? Sammy Sosa
              ? Ken Griffey
            # Flow style
            baseball teams: !!set { Boston Red Sox, Detroit Tigers, New York Yankees }

          # http://yaml.org/type/seq.html -----------------------------------------------#

          seq:
            # Ordered sequence of nodes
            Block style: !!seq
              - Mercury # Rotates - no light/dark sides.
              - Venus # Deadliest. Aptly named.
              - Earth # Mostly dirt.
              - Mars # Seems empty.
              - Jupiter # The king.
              - Saturn # Pretty.
              - Uranus # Where the sun hardly shines.
              - Neptune # Boring. No rings.
              - Pluto # You call this a planet?
            Flow style: !!seq [
                Mercury,
                Venus,
                Earth,
                Mars, # Rocks
                Jupiter,
                Saturn,
                Uranus,
                Neptune, # Gas
                Pluto,
              ] # Overrated

          # Scalar Types #################################################################
          ################################################################################

          # http://yaml.org/type/bool.html ----------------------------------------------#

          bool:
            - true
            - True
            - TRUE
            - false
            - False
            - FALSE

          # http://yaml.org/type/float.html ---------------------------------------------#

          float:
            canonical: 6.8523015e+5
            exponentioal: 685.230_15e+03
            fixed: 685_230.15
            sexagesimal: 190:20:30.15
            negative infinity: -.inf
            not a number: .NaN

          # http://yaml.org/type/int.html -----------------------------------------------#

          int:
            canonical: 685230
            decimal: +685_230
            octal: 02472256
            hexadecimal: 0x_0A_74_AE
            binary: 0b1010_0111_0100_1010_1110
            sexagesimal: 190:20:30

          # http://yaml.org/type/merge.html ---------------------------------------------#

          merge:
            - &CENTER { x: 1, y: 2 }
            - &LEFT { x: 0, y: 2 }
            - &BIG { r: 10 }
            - &SMALL { r: 1 }

            # All the following maps are equal:

            - # Explicit keys
              x: 1
              y: 2
              r: 10
              label: nothing

            - # Merge one map
              <<: *CENTER
              r: 10
              label: center

            - # Merge multiple maps
              <<: [*CENTER, *BIG]
              label: center/big

            - # Override
              <<: [*BIG, *LEFT, *SMALL]
              x: 1
              label: big/left/small

          # http://yaml.org/type/null.html ----------------------------------------------#

          null:
            # This mapping has four keys,
            # one has a value.
            empty:
            canonical: ~
            english: null
            ~: null key
            # This sequence has five
            # entries, two have values.
            sparse:
              - ~
              - 2nd entry
              -
              - 4th entry
              - Null

          # http://yaml.org/type/str.html -----------------------------------------------#

          string: abcd

          # http://yaml.org/type/timestamp.html -----------------------------------------#

          timestamp:
            canonical: 2001-12-15T02:59:43.1Z
            valid iso8601: 2001-12-14t21:59:43.10-05:00
            space separated: 2001-12-14 21:59:43.10 -5
            no time zone (Z): 2001-12-15 2:59:43.10
            date (00:00:00Z): 2002-12-14

          # JavaScript Specific Types ####################################################
          ################################################################################

          # https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/RegExp

          regexp:
            simple: !!js/regexp foobar
            modifiers: !!js/regexp /foobar/mi

          # https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/undefined

          undefined: !!js/undefined ~

          # https://developer.mozilla.org/en/JavaScript/Reference/Global_Objects/Function

          function: !!js/function >
            function foobar() {
              return 'Wow! JS-YAML Rocks!';
            }

          # Custom types #################################################################
          ################################################################################

          # JS-YAML allows you to specify a custom YAML types for your structures.
          # This is a simple example of custom constructor defined in `js/demo.js` for
          # custom `!sexy` type:
          #
          # var SexyYamlType = new jsyaml.Type('!sexy', {
          #   kind: 'sequence',
          #   construct: function (data) {
          #     return data.map(function (string) { return 'sexy ' + string; });
          #   }
          # });
          #
          # var SEXY_SCHEMA = jsyaml.Schema.create([ SexyYamlType ]);
          #
          # result = jsyaml.load(yourData, { schema: SEXY_SCHEMA });

          foobar: !sexy
            - bunny
            - chocolate

          作者:Escape
          文章來源:https://escapelife.github.io/posts/8032061c.html
          文章轉(zhuǎn)載: K8S中文社區(qū)

          (版權(quán)歸原作者所有,侵刪)

          瀏覽 66
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  久久天天躁狠狠躁夜夜躁2014 | 黄笔毛片大全免费观看 | 可以看av的网站 麻豆精产成人精品 | 女同性恋一区二区 | 日韩一级黄视频 |