配置文件yaml

YAML

integrate YAML into your workflows for better configuration management

YAML is one of the most popular data serialization languages, and it is used mostly for writing configuration files. The YAML recursive acronym stands for YAML Ain’t Markup Language.

extension

.yaml or .yml.

示例

---
# key: value [mapping]
company: spacelift
# key: value is an array [sequence]
domain:
 - devops
 - devsecops
tutorial:
  - yaml:
      name: "YAML Ain't Markup Language" #string [literal]
      type: awesome #string [literal]
      born: 2001 #number [literal]
  - json:
      name: JavaScript Object Notation #string [literal]
      type: great #string [literal]
      born: 2001 #number [literal]
  - xml:
      name: Extensible Markup Language #string [literal]
      type: good #string [literal]
      born: 1996 #number [literal]
author: omkarbirade
published: true

data types

structures

common use cases

What is a YAML used for?

  • Configuration management (CM) – Ansible uses yaml files to describe all CM configurations (playbooks, roles, etc.).
  • Infrastructure as code (IaC)OpenTofu, for example, can read yaml files and use them as input for different resources, data sources, and even outputs.
  • CI/CD – Many CI/CD products rely on yaml to describe their pipelines (GitHub Actions, GitLab CI/CD, Azure DevOps, CircleCI)
  • Container orchestration (CO) – K8s and Docker Compose rely heavily on yaml files to describe the infrastructure resources.
  • Data serialization – YAML can be used to describe complex data types such as lists, maps, and objects.
  • APIs – YAML can be used in defining API contracts and specifications (e.g. OpenAPI).

YAML是JSON的超集

YAML is, in fact, a superset of JSON. All JSON files are valid YAML files, but not the other way around.

Best practices for writing YAML files

YAML relies heavily on indentation, so if your indentation is not correct, the yaml file will be automatically invalid when you try to use it in your automations. Here are some of the best practices you can use when writing yaml files:

  1. Use consistent indentation – You should always use spaces and be consistent with the number of spaces you use throughout your file (usually, this should be two or four).
  2. Keep the files short – You can configure everything in a single YAML file, but this will make everything hard to debug. You should always try to split your configurations into multiple files to speed up the debugging process.
  3. Use descriptive key names – To make your configuration smooth and easy to understand by other people, the key names used throughout the file should be meaningful.
  4. Structure complex data – Lists are denoted by a dash (“-”), while maps are key-value pairs.
  5. Use comments – Comments will help you and others understand what that data is used for. To write a comment in a YAML file, you can prefix the line with the “#” character
  6. Use validations – Validate your YAML files regularly using special tools for this purpose to catch syntax errors fast.
  7. Use quotes for strings – This is especially useful if your string contains special characters or starts with a number.
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容