Chapter 2: Lists and Arrays

In this chapter, we’ll dive into YAML lists and arrays, which are used to represent ordered collections of items. This chapter will explore different ways to define lists, including single-level lists, nested lists, and inline lists. All concepts will be explained with examples, including comments for better understanding.

2.1 Single-Level Lists

A simple list in YAML is represented by a series of items prefixed by a dash (-). This is known as a single-level list, and it can contain strings, numbers, or even other complex structures.

Example 1: Simple List of Strings

# List of fruits
fruits:
  - apple
  - banana
  - orange
Here, fruits is a key, and its value is a list of items in the form of string (apple, banana, orange).


Example 2: List of Numbers

# List of version numbers
versions:
  - 1.0
  - 2.1
  - 3.4
In this case, versions is a key which consists of a list of numeric values.

2.2 Nested Lists

YAML allows you to create lists within lists. This is known as a nested list and is useful for representing more complex data structures.

Example: Nested List

# List of items with nested lists
shopping_list:
  - fruits:
      - apple
      - orange
  - vegetables:
      - carrot
      - broccoli

Explanation: - The outer list contains two dictionaries: one for fruits and one for vegetables. - Each dictionary contains a list of specific items (apple, carrot, etc.).

Step-by-Step Identification:

  1. shopping_list is a list
  2. It contains multiple items, each prefixed with -.
  3. In YAML, a leading - denotes an item in a list.

  4. Each item in shopping_list is a dictionary

  5. Each list item consists of a single key (fruits or vegetables) followed by a colon (:).
  6. In YAML, key-value pairs indicate a dictionary.

  7. The value of fruits and vegetables is a list

  8. The values of fruits and vegetables are lists because their items are also prefixed with -.
  9. Example: - apple, - orange under fruits means fruits is a key pointing to a list.

  10. shopping_list: is a key whose value is a list (indicated by -).

  11. Each list item is a dictionary (e.g., fruits: with key-value pairs).
  12. Each dictionary’s value is a list (e.g., - apple, - orange under fruits).

2.3 Lists of Dictionaries

In YAML, you can create a list where each item is a dictionary (also known as a map). This is very useful in DevOps tools where you might define multiple resources with similar properties.

Example: List of Dictionaries

# List of server configurations
servers:
  - name: nginx
    version: 1.18.0
    ports:
      - 80
      - 443
  - name: apache
    version: 2.4
    ports:
      - 8080

Explanation: - The servers key contains a list of dictionaries. - Each dictionary represents a server with its name, version, and ports list. - The ports key contains its own list of numbers, further nesting data within the list of dictionaries.

Step-by-Step Identification:

  1. servers is a list
  2. It contains multiple items, each prefixed with -.
  3. The presence of - indicates that servers is a list.

  4. Each item in servers is a dictionary

  5. Every list item consists of key-value pairs (name, version, and ports).
  6. In YAML, a key followed by a colon (:) represents a dictionary.

  7. The ports key holds a list

  8. The ports key contains multiple values, each prefixed with -.
  9. This means ports is a list nested inside each dictionary.

Breakdown of Structure:

  • The outer structure is a list (servers).
  • Each list item is a dictionary with keys name, version, and ports.
  • The ports key contains a list of port numbers.

Visual Representation:

servers (List) Dictionary (Key-Value Pairs) Nested List (ports)
- name: nginx version: 1.18.0 - 80
- 443
- name: apache version: 2.4 - 8080

This structure is commonly used in DevOps for managing multiple server configurations in a structured and readable format.


2.4 Inline Lists

Sometimes, you may want to define a list in a more compact form. YAML allows you to write a list on a single line using square brackets ([]).

Example: Inline List

# Inline list of colors
colors: [red, blue, green]

This inline list format is equivalent to:

colors:
  - red
  - blue
  - green

Inline lists can make configurations more compact, but they may reduce readability, especially in larger files.


2.5 Complex Lists with Nested Dictionaries

You can combine lists, dictionaries, and nested structures to represent more complex data.

Example: Complex Nested List with Dictionaries

# Complex configuration for a web application
webapp:
  name: "My App"
  environments:
    - name: development
      servers:
        - name: dev-server-1
          port: 3000
        - name: dev-server-2
          port: 3001
    - name: production
      servers:
        - name: prod-server-1
          port: 80
        - name: prod-server-2
          port: 443

Explanation: - The environments key contains a list of environments (development, production). - Each environment contains a list of servers, each with its own name and port. - This type of structure is very common in DevOps for defining environments, servers, or services.

Step-by-Step Identification:

  1. webapp is a dictionary
  2. It has key-value pairs like name and environments.
  3. The colon (:) after webapp indicates it is a dictionary.

  4. environments is a list

  5. Each item under environments is prefixed with -, meaning it is a list.
  6. The list contains dictionaries representing different environments (development and production).

  7. Each environment (development and production) is a dictionary

  8. Contains name and servers keys.

  9. servers is a nested list inside each environment

  10. Each item under servers is also prefixed with -, making it a list.
  11. The list contains dictionaries representing different servers.
  12. Each server has two key-value pairs: name and port.

Breakdown of Structure:

  • webapp (Dictionary)
  • Contains a name (string).
  • Has an environments list.

  • environments (List)

  • Contains multiple dictionaries (development and production).

  • Each environment dictionary

  • Has a name key.
  • Contains a servers list.

  • servers (List)

  • Contains multiple dictionaries (name and port of each server).

Visual Representation:

Key Type Sub-Keys Nested Structure
webapp Dictionary name, environments -
├── name String "My App" -
├── environments List development, production -
│ ├── name String "development" -
│ ├── servers List dev-server-1, dev-server-2 -
│ │ ├── name String "dev-server-1" -
│ │ ├── port Integer 3000 -
│ │ ├── name String "dev-server-2" -
│ │ ├── port Integer 3001 -
│ ├── name String "production" -
│ ├── servers List prod-server-1, prod-server-2 -
│ │ ├── name String "prod-server-1" -
│ │ ├── port Integer 80 -
│ │ ├── name String "prod-server-2" -
│ │ ├── port Integer 443 -
Summary:
  1. webapp is a dictionary with name and environments keys.
  2. environments is a list of dictionaries, each representing a deployment stage.
  3. Each environment has a servers list, which contains dictionaries specifying the servers and their ports.
  4. The entire structure is hierarchical, making it a useful format for defining complex configurations in DevOps.

This YAML format is commonly used for web applications where different environments (development, production) need separate server configurations.


2.6 Best Practices for Lists

  • Consistency: Make sure you are consistent with your indentation. Incorrect indentation will cause errors.
  • Readability: For simple lists, inline lists can be more readable, but for complex structures, multi-line lists are often preferred for clarity.
  • Modularity: When your list grows large, consider splitting it across multiple YAML files and referencing them where necessary (e.g., in tools like Kubernetes or Ansible).

2.7 Real-World Use Cases of Lists in DevOps

Kubernetes Pods: Define a list of containers within a Pod.

apiVersion: v1
kind: Pod
metadata:
  name: my-pod
spec:
  containers:
    - name: nginx-container
      image: nginx:1.18
    - name: redis-container
      image: redis:6.0

The containers key is a list where each item represents a container inside the Pod.

Ansible Playbooks: Define tasks as a list of actions to be executed.

- hosts: all
  tasks:
    - name: Install Nginx
      yum:
        name: nginx
        state: present
    - name: Start Nginx service
      service:
        name: nginx
        state: started

The tasks key contains a list of dictionaries, each representing a task to be performed.


2.8 Practice Exercise: Writing a YAML File with Lists

Now that you understand lists and arrays in YAML, here’s a practical exercise to solidify your understanding. Try writing a YAML file to represent a list of users and their roles in different environments:

# List of users with roles in different environments
users:
  - name: Alice
    role: developer
    environments:
      - development
      - staging
  - name: Bob
    role: sysadmin
    environments:
      - production
      - staging
  - name: Charlie
    role: tester
    environments:
      - development

This concludes Chapter 2 on Lists and Arrays. You should now be comfortable defining lists, using inline lists, and creating nested structures that are common in YAML files for DevOps tools.