Hyppää sisältöön

Significant changes to Puhti & Mahti authentication coming in April! Read about the SSH key and multi-factor authentication requirements.

Lyhyt johdatus YAML-kieleen

YAML:ia käytetään kuvaamaan avain-arvo -karttoja ja taulukoita. YAML-tiedostot tunnistetaan käyttämällä .yml tai .yaml tiedostopäätettä.

YAML-datasetti voi olla

  • arvo
value
  • taulukko
- value 1
- value 2
- value 3

tai

[value 1, value 2, value 3]
  • sanakirja
key: value
another_key: another value

tai

key:
  value
another_key:
  another value
  • YAML-datasetti
key:
  - value 1
  - another key:
      yet another key: value 2
    another key 2:
      - more values
    this keys value is also an array:
    - but indentation is not necessary here

Arvot voidaan syöttää monille riveille käyttäen >:

key: >
  Here's a value that is written over multiple lines
  but is actually still considered a single line until now.

  Placing double newline here will result in newline in the actual data.

Verbatim-tyyliä tuetaan | merkillä:

key: |
  Now each
  newline is
  treated as such

YAML vs JSON

YAML on JSON:in (JavaScript Object Notation) yläjoukko. Tällöin,

{
  "key": [
    {
      "value 1": {
        "another key": {
          "yet another key": "value 2"
        },
        "another key 2": [
          "more values"
        ],
        "this keys value is also an array": ["but indentation is not necessary here"]
      }
    }
  ]
}

on myös kelvollinen YAML. Yleisesti ottaen YAML on kompaktimpaa kuin JSON:

key:
  - value 1:
      another key:
        yet another key: value 2
      another key 2:
        - more values
      this keys value is also an array:
        - but indentation is not necessary here

yq komentorivityökalu

yq on hyödyllinen työkalu yaml-tiedostojen kanssa työskentelyyn. Sen voi asentaa käyttämällä pip:

$ pip show yq  
Name: yq
Version: 3.0.2
Summary: Command-line YAML/XML processor - jq wrapper for YAML/XML documents
Home-page: https://github.com/kislyuk/yq
Author: Andrey Kislyuk
Author-email: kislyuk@gmail.com
License: Apache Software License
Location: /home/jtahir/Documents/csc-stuff/osclient/lib/python3.6/site-packages
Requires: argcomplete, PyYAML, toml, xmltodict
Required-by: 

yq on jq-kääre. Tämä tarkoittaa, että se muuntaa yaml-syötteen jsoniksi ja käsittelee sen sen jälkeen jq:lle, ja siksi sillä on sama syntaksi kuin jq:lla. Seuraavassa esimerkissä .data.WebHookSecretKey:n arvo haetaan raakatilassa (ilman lainausmerkkejä):

$ echo 'apiVersion: v1
kind: Secret
data:
  WebHookSecretKey: dGhpc19pc19hX2JhZF90b2tlbgo=
metadata:
  name: webhooksecret
  namespace: mynamespace     # set this to your project namespace
' | yq ' .data.WebHookSecretKey ' -r
dGhpc19pc19hX2JhZF90b2tlbgo=

Lisätietoja löytyy osoitteista yaml.org tai json.org.