Ansible is one of the most popular automation tools used for configuration management, application deployment, and task automation. Its simplicity and power stem from its use of YAML (YAML Ain’t Markup Language), a human-readable data serialization format.
YAML is the backbone of Ansible playbooks, inventories, and variable files, making it essential for anyone working with Ansible to understand its syntax and structure.
In this article, we’ll dive deep into the basics of YAML and how it is used in Ansible. We’ll cover YAML syntax, provide examples of Ansible YAML files, and explore various YAML structures such as lists, dictionaries, and nested data.
By the end of this article, you’ll have a solid understanding of how to write and interpret YAML for Ansible automation. Enrolling in Automation courses may help you learn YAML faster and gain job-ready skills.
YAML is a data serialization language designed to be both human-readable and machine-parsable. It is often used for configuration files and data exchange between systems.
YAML’s simplicity and readability make it an ideal choice for Ansible, where playbooks and configurations need to be easily understood and maintained.
The following are the key features of YAML:
● YAML uses indentation and simple syntax, making it easy to read and write.
● YAML supports complex data structures like lists, dictionaries, and nested objects.
● Unlike JSON or XML, YAML relies on indentation and colons to define structure.
● YAML supports comments, which start with a # symbol.
● YAML is case-sensitive, so key and Key are treated as different entities.
Before diving into Ansible-specific examples, let’s review the basic syntax of YAML.
1. Key-Value Pairs
YAML uses key-value pairs to represent data. The key and value are separated by a colon (:).
name: Alice age: 30 |
2. Indentation
Indentation is used to define the hierarchy of data. Spaces (not tabs) are used for indentation.
person: name: Alice age: 30 |
3. Lists
Lists are represented with a hyphen (-) followed by a space.
fruits: - apple - banana - cherry |
4. Dictionaries
Dictionaries are collections of key-value pairs.
person: name: Alice age: 30 hobbies: - reading - hiking |
5. Strings
Strings can be written without quotes unless they contain special characters.
message: Hello, World! |
Ansible playbooks are written in YAML and typically start with three dashes (---). Let’s look at a simple example of an Ansible playbook:
--- - name: Install and start Apache hosts: webservers become: yes tasks: - name: Ensure Apache is installed apt: name: apache2 state: present - name: Ensure Apache is running service: name: apache2 state: started |
1. name: Describes the purpose of the playbook.
2. hosts: Specifies the target hosts (e.g., webservers).
3. become: yes: Indicates that the tasks will be executed with elevated privileges (e.g., using sudo).
4. tasks: A list of tasks to be executed. Each task is called an Ansible module.
Ansible modules are reusable units of code that perform specific tasks. In the example above, apt and service are modules. Modules are invoked in playbooks using key-value pairs, where the key is the module name and the value is the module's parameters.
Example of Modules
The following are some of the examples of such modules:
● apt: Manages packages on Debian-based systems.
● service: Manages services on the target system.
● copy: Copies files from the control machine to the target machine.
● command: Executes commands on the target machine.
A YAML list of objects is a collection of items, where each item is an object (a set of key-value pairs). This structure is commonly used in Ansible to define multiple tasks or hosts.
Example
The following are some of the examples of such objects:
- name: Alice age: 30 - name: Bob age: 25 |
This represents a list of two objects, each with name and age keys.
A YAML list of dictionaries is like a list of objects. Each item in the list is a dictionary (key-value pairs). This structure is useful for defining complex data.
Example
The following are some of the examples of that list of dictionaries:
- person: name: Alice age: 30 - person: name: Bob age: 25 |
Here, each item in the list is a dictionary under the person key.
A YAML list of lists is a nested structure where each item in the main list is itself a list. This structure is useful for representing multi-dimensional data.
Example
The following are some of the examples of those lists:
- - apple - banana - cherry - - dog - cat - bird |
This represents two lists: one of fruits and one of animals.
A YAML list of strings is a simple list where each item is a string. This structure is commonly used for defining variables or parameters.
Example
The following are some of the examples of these list of strings:
- apple - banana - cherry |
This represents a list of three fruits.
A YAML dictionary is a collection of key-value pairs. Dictionaries are used to represent structured data in Ansible, such as variables or task parameters.
Example
The following are some of the examples of such dictionaries:
person: name: Alice age: 30 hobbies: - reading - hiking |
In this example:
● person is the top-level key.
● name, age, and hobbies are nested keys.
● hobbies is a list of strings.
Ansible often uses advanced YAML structures to define complex configurations. Let’s explore some of these structures.
Nested dictionaries are used to represent hierarchical data.
webserver: apache: port: 80 docroot: /var/www/html nginx: port: 8080 docroot: /usr/share/nginx/html |
Lists and dictionaries can be combined to represent complex data.
servers: - name: server1 ip: 192.168.1.1 services: - apache - mysql - name: server2 ip: 192.168.1.2 services: - nginx - postgresql |
YAML supports multi-line strings using the | or > symbols.
description: | This is a multi-line string. It preserves line breaks. |
The following are the best practices to be followed while writing YAML in Ansible :
● Always use spaces (not tabs) for indentation. The standard is two spaces per level.
● Avoid overly complex structures unless necessary.
● Add comments to explain complex sections of your playbook.
● Use tools like yamllint to validate your YAML files.
● Always test your playbooks in a safe environment before deploying them to production.
YAML is the foundation of Ansible automation, and understanding its syntax and structure is crucial for creating effective playbooks and configurations. From simple key-value pairs to complex nested structures, YAML provides the flexibility and readability needed for automation tasks.
By mastering YAML, you’ll be able to write clean, efficient, and maintainable Ansible playbooks. Whether you’re defining a list of objects, a dictionary, or a multi-dimensional structure, YAML’s simplicity and clarity make it an ideal choice for Ansible configurations.
Start experimenting with YAML in your Ansible playbooks today, and you’ll quickly appreciate its power and flexibility. Happy automating!
He is a senior solution network architect and currently working with one of the largest financial company. He has an impressive academic and training background. He has completed his B.Tech and MBA, which makes him both technically and managerial proficient. He has also completed more than 450 online and offline training courses, both in India and ...
More... | Author`s Bog | Book a Meeting#Explore latest news and articles
Share this post with others