YAML เป็นอีกหนึ่งรูปแบบของข้อมูลที่ให้อ่านเข้าใจง่าย ถูกใช้อย่างมากในโลกของ DevOps และ Container ดังจะเห็นบ่อย ๆ ใน configuration ต่าง ๆ เช่น
ดังนั้นมาทำความรู้จักและเข้าใจเกี่ยวกับ YAML กันหน่อย
YAML (YAML Ain’t Markup Language) คืออะไร โดยเริ่มต้นย่อมาจาก Yet Another Markup Language แต่เปลี่ยนมาเป็น YAML Ain’t Markup Language เพื่อทำให้ชัดเจนว่าแตกต่างจาก Markup Language นะ เพราะว่ามันคือ Data Serialization Language
จะมีรูปแบบคล้ายกับ XML และ JSON แต่ใช้ syntax ที่น้อยหรือง่ายกว่า เพื่อให้ใช้งานและดูแลได้ง่าย และบอกว่าถูกออกแบบมาให้คนอ่านง่ายขึ้น จริงไหมนะ ?
ปล. แต่ถ้าไม่ได้ใช้ Text Editor หรือ IDE ที่สนับสนุน YAML แล้ว อาจจะทำให้คุณมีปัญหาได้ยกตัวอย่าง เช่น เพียงแค่ผิด 1 space bar อาจจะใช้เวลาหาข้อผิดพลาดมากก็ได้
ความสามารถของ YAML ที่แจ่ม ๆ คือ
ยกตัวอย่างเช่น
id: 1 name: name 1 --- id: 2 name: name 2 #comment is-an-integer: !!int 11.11 is-a-string: !!str 12.3 is-a-boolean: !!bool yes --- integer: 25 hex: 0x12d4 #evaluates to 4820 octal: 023332 #evaluates to 9946 float: 25.0 exponent: 12.3015e+05 #evaluates to 1230150.0 boolean: Yes string: "25" infinity: .inf # evaluates to infinity neginf: -.Inf #evaluates to negative infinity not: .NAN # Not a Number
รูปแบบ หรือ syntax ของ YAML
message_string: Hello World data_01: | These Newlines data_02: > This text is a single paragraph
ต่อมาเก็บข้อมูลในรูปแบบของ sequence ซึ่งจะมีลักษณะคล้ายกับ array หรือ list สำหรับเก็บหลาย ๆ ค่าใน key เดียว ยกตัวอย่างเช่น
fruit: - apple - banana - orange
รูปแบบ dictionary ก็มี
customers: - custome1: name: Cust 01 age: 30 - custome2: name: Cust 02 age: 25
แน่นอนว่ายังมีรูปแบบอื่น ๆ ให้ศึกษาและใช้งานอีก แต่นี่คือความรู้เบื้องต้นที่ควรต้องรู้จักไว้บ้าง ยกตัวอย่างที่อาจจะเจอคือ Manifest file ของ Kubernetes เป็นต้น
kind: Service metadata: name: web-app-svc spec: type: NodePort ports: - port: 8080 targetPort: 8080 nodePort: 30012 selector: app: web-app