YAMLとは

デバック用の Ruby コード

YAML の開始終了

YAML のコメント

# comment
- data # comment

YAML のデータ

YAML の改行

---
- "no mark" :
  - George Bernard Shaw :
      He who can, does.
      He who cannot, teaches.
- "|" :
  - George Bernard Shaw : |
      He who can, does.
      He who cannot, teaches.
- "|+" :
  - George Bernard Shaw : |+
      He who can, does.
      He who cannot, teaches.
- "|-" :
  - George Bernard Shaw : |-
      He who can, does.
      He who cannot, teaches.
- ">" :
  - George Bernard Shaw : >
      He who can, does.
      He who cannot, teaches.
- ">+" :
  - George Bernard Shaw : >+
      He who can, does.
      He who cannot, teaches.
- ">-" :
  - George Bernard Shaw : >-
      He who can, does.
      He who cannot, teaches.

評価結果

$ cat cr.yml | ./yaml2json.rb
[{"no mark": [{"George Bernard Shaw": "He who can, does. He who cannot, teaches."    }]},
 {"|":       [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches.\n" }]},
 {"|+":      [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches.\n" }]},
 {"|-":      [{"George Bernard Shaw": "He who can, does.\nHe who cannot, teaches."   }]},
 {">":       [{"George Bernard Shaw": "He who can, does. He who cannot, teaches.\n"  }]},
 {">+":      [{"George Bernard Shaw": "He who can, does. He who cannot, teaches.\n"  }]},
 {">-":      [{"George Bernard Shaw": "He who can, does. He who cannot, teaches."    }]}
]
YAML中の改行末尾の改行
no markスペース無視
"|"改行改行
">"スペース改行

"+" 末尾に改行を付ける "-" 末尾の改行を無視

アンカー・エイリアス (参照)

---
- servers :
  - &web
    name : turip
    ip : 192.168.10.31
  - &db
    name : pansy
    ip : 192.168.10.32
- services :
  - nginx :
      server : *web
      port : 80
  - redis :
      server : *web
      port : 61
  - mapserver :
      server : *db
      port : 8080
  - postgis :
      server : *db
      port : 5432

評価結果

$ cat anchor.yml | ./yaml2json.rb
[{"servers":[{"name":"turip","ip":"192.168.10.31"},{"name":"pansy","ip":"192.168.10.32"}]},
 {"services":[
   {"nginx":{"server":{"name":"turip","ip":"192.168.10.31"},"port":80}},
   {"redis":{"server":{"name":"turip","ip":"192.168.10.31"},"port":61}},
   {"mapserver":{"server":{"name":"pansy","ip":"192.168.10.32"},"port":8080}},
   {"postgis":{"server":{"name":"pansy","ip":"192.168.10.32"},"port":5432}}
 ]}
]

アンカーの内容に key を付けたくないときには、<<: で単純マージができる

---
- servers :
  - &web
    name : turip
    ip : 192.168.10.31
  - &db
    name : pansy
    ip : 192.168.10.32
- services :
  - nginx :
      << : *web
      port : 80
  - redis :
      << : *web
      port : 61
  - mapserver :
      << : *db
      port : 8080
  - postgis :
      << : *db
      port : 5432

評価結果

$ cat anchor2.yml | ./yaml2json.rb
[{"servers":[{"name":"turip","ip":"192.168.10.31"},{"name":"pansy","ip":"192.168.10.32"}]},
 {"services":[
   {"nginx":{"name":"turip","ip":"192.168.10.31","port":80}},
   {"redis":{"name":"turip","ip":"192.168.10.31","port":61}},
   {"mapserver":{"name":"pansy","ip":"192.168.10.32","port":8080}},
   {"postgis":{"name":"pansy","ip":"192.168.10.32","port":5432}}
 ]}
]

Ansible


トップ   編集 凍結 差分 バックアップ 添付 複製 名前変更 リロード   新規 一覧 単語検索 最終更新   ヘルプ   最終更新のRSS   sitemap
Last-modified: 2016-06-07 (火) 01:38:08 (2873d)
Short-URL:
ISBN10
ISBN13
9784061426061