Trouble understanding YAML file

10/7/2020

I am new to YAML and I would like to understand the following piece of a .yaml file:

version: "3.7"
services:
    influxdb:
        image: influxdb:alpine
        environment:
            INFLUXDB_DB: ft_services
            INFLUXDB_ADMIN_USER: admin
            INFLUXDB_ADMIN_PASSWORD: admin
        volumes:
            - datainfluxdb:/var/lib/influxdb
        deploy:
            restart_policy:
                condition: on-failure

As far as I know, there are 3 types of data that can be used in a .yaml file: scalars, sequences and mappings. For example, version: "3.7" is a scalar. But I am not sure what the following are:

volumes:
            - datainfluxdb:/var/lib/influxdb
environment:
            INFLUXDB_DB: ft_services
            INFLUXDB_ADMIN_USER: admin
            INFLUXDB_ADMIN_PASSWORD: admin

I don't really understand what type of data are these and how do they work, can someone give me a hint?

-- kubo
kubernetes
yaml

2 Answers

10/7/2020

Lists

example

volumes:
   - data: /var/lib
     other-field: "example"
   - data: /etc

Each indented line beginning with an - above is the beginning of a List Item. There is two items in the list in the example and the whole list is named volumes. The example is a List of Maps, but also List of Scalars is valid.

Maps

example

environment:
    INFLUXDB_DB: ft_services
    INFLUXDB_ADMIN_USER: admin
    INFLUXDB_ADMIN_PASSWORD: admin

as you wrote, this is a Map with Key-Value pairs and the whole Map is named environment.

Scalars

As you wrote there is also scalars of various types. A value within quotes like "3.7" is a string.

-- Jonas
Source: StackOverflow

10/7/2020

But I am not sure what the following are:

  • List of maps:

    volumes:
      - datainfluxdb:/var/lib/influxdb

    Equal json:

    {
      "volumes": [
        {"datainfluxdb": "/var/lib/influxdb"}
      ]
    }
    
  • Map:

    environment:
      INFLUXDB_DB: ft_services
      INFLUXDB_ADMIN_USER: admin
      INFLUXDB_ADMIN_PASSWORD: admin

    Equal json:

    {
      "environment": {
        "INFLUXDB_DB": "ft_services",
        "INFLUXDB_ADMIN_USER": "admin",
        "INFLUXDB_ADMIN_PASSWORD": "admin"
      }
    }
    
  • And not mentioned in your question, but a simple List of strings

    ```
    accessModes:
      - ReadWriteOnce
    ```
    Equal json:
    ```
    {
      "accessModes": [
        "ReadWriteOnce"
      ]
    }
    ```
    

    These 3 are the most frequently seen in Kubernetes.

In my opinion, in the beginning, YAML gets the most confusing when you have nested structures. For easier understanding, I suggest using smth like https://onlineyamltools.com/convert-yaml-to-json to convert to JSON, which has more explicit structures syntax.

-- Max Lobur
Source: StackOverflow