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
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
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:¶
shopping_listis a list- It contains multiple items, each prefixed with
-. -
In YAML, a leading
-denotes an item in a list. -
Each item in
shopping_listis a dictionary - Each list item consists of a single key (
fruitsorvegetables) followed by a colon (:). -
In YAML, key-value pairs indicate a dictionary.
-
The value of
fruitsandvegetablesis a list - The values of
fruitsandvegetablesare lists because their items are also prefixed with-. -
Example:
- apple, - orangeunderfruitsmeansfruitsis a key pointing to a list. -
shopping_list: is a key whose value is a list (indicated by-). - Each list item is a dictionary (e.g.,
fruits: with key-value pairs). - Each dictionary’s value is a list (e.g.,
- apple,- orangeunderfruits).
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:¶
serversis a list- It contains multiple items, each prefixed with
-. -
The presence of
-indicates thatserversis a list. -
Each item in
serversis a dictionary - Every list item consists of key-value pairs (
name,version, andports). -
In YAML, a key followed by a colon (
:) represents a dictionary. -
The
portskey holds a list - The
portskey contains multiple values, each prefixed with-. - This means
portsis 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, andports. - The
portskey 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:¶
webappis a dictionary- It has key-value pairs like
nameandenvironments. -
The colon (
:) afterwebappindicates it is a dictionary. -
environmentsis a list - Each item under
environmentsis prefixed with-, meaning it is a list. -
The list contains dictionaries representing different environments (
developmentandproduction). -
Each environment (
developmentandproduction) is a dictionary -
Contains
nameandserverskeys. -
serversis a nested list inside each environment - Each item under
serversis also prefixed with-, making it a list. - The list contains dictionaries representing different servers.
- Each server has two key-value pairs:
nameandport.
Breakdown of Structure:¶
webapp(Dictionary)- Contains a
name(string). -
Has an
environmentslist. -
environments(List) -
Contains multiple dictionaries (
developmentandproduction). -
Each environment dictionary
- Has a
namekey. -
Contains a
serverslist. -
servers(List) - Contains multiple dictionaries (
nameandportof 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:¶
webappis a dictionary withnameandenvironmentskeys.environmentsis a list of dictionaries, each representing a deployment stage.- Each environment has a
serverslist, which contains dictionaries specifying the servers and their ports. - 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.