How to Create Yaml File

Jan 10, 2022

3 mins read

Published in
How to create YAML file. This article helps to create valid YAML document File.

Creating an YAML file requires a basic understanding of YAML and why it's used as configuration files in Kubernetes, Ansible, CircleCI etc.

What is an YAML File?

YAML or ‘Yet Another Markup Language’ is a human-readable data serialization language. It is designed in order to be both human-readable and also efficient for parsing.

The YAML specification was written in 2006 by David Preshing, the creator of PHP Markdown, who wanted a universal markup language that was easy for humans to read and write, but still allowed for significant information density and reasonable balancing of tradeoffs between the machine time required to process it and the machine time required to produce it.

These are 3 ways to create a new YAML File.

  1. Get the YAML Configuration template file such as Ansible Play file template. Here is the nginx-deplymnet.yaml
  2. Use Text Editor or Online tool to create the YAML Syntax / Structure.
  3. Create a file from the YAML URL.

1. Using YAML Template

YAML is mostly used for the configuration files. So to avoid mistakes, find the right template for the YAML configuration for Kubernetes, Ansible or CircleCI.

Here is an example of the Kubernetes nginx configuration deployment file.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
apiVersion: apps/v1
kind: Deployment
metadata:
  name: nginx-deployment
  labels:
    app: nginx
spec:
  replicas: 3
  selector:
    matchLabels:
      app: nginx
  template:
    metadata:
      labels:
        app: nginx
    spec:
      containers:
      - name: nginx
        image: nginx:1.14.2
        ports:
        - containerPort: 80

Use this YAML validator tool to validate the YAML

https://codebeautify.org/yaml-validator

Once file data are validated, save the file with the extension of .yaml. This section helps to create valid YAML file.

2. Using Text Editor or Online YAML Tool

Open a YAML Formatter tool from the link below

https://codebeautify.org/yaml-beautifier
or
https://jsonformatter.org/yaml-parser

Copy and Paste the YAML Data which is mentioned in Option 1 in the Input tool of the online tool.

Download this file using the download icon mentioned on the Right input area. It will download the YAML file.

This https://codebeautify.org/yaml-beautifier already supports the YAML validation. This will create a New YAML file and will allow downloading.

Now if you already have a YAML document or file, you can upload it to this tool and modify or delete a few objects, an array of the YAML and download the updated and valid YAML file.

This tool also help to create a YAML from text file. Upload text file using the upload file button and YAML will be automatically validated and beautified.

3. Create a file from the YAML URL

Developer needs to work with data and files resides on the cloud servers and nowadays 95% of web and apps are hosted on AWS, Azure, Google Cloud or Digital Ocean.

Developer can use remote URL to save and edit the YAML files.

Here are few samples of the YAML URL. Now click on these YAML samples.

Once this link is open, use save as functionality of the browser and save. It will generate YAML file and save it on your device.

How to create a YAML file in linux

Using echo :

echo "apiVersion: apps/v1" > config.yml

Using touch :

touch config.yml

I hope this article helps you to understand basic ways to make or create valid YAML files and beautify them.

Know More about YAML :

Sharing is caring!