Ansible – YAML Basics

Ansible uses YAML syntax for expressing Ansible playbooks. This chapter provides an overview of YAML. Ansible uses YAML because it is very easy for humans to understand, read and write when compared to other data formats like XML and JSON.

Every YAML file optionally starts with “—” and ends with “…”.

Understanding YAML

In this section, we will learn the different ways in which the YAML data is represented.

key-value pair

YAML uses simple key-value pair to represent the data. The dictionary is represented in key: value pair.

Note − There should be space between : and value.

Example: A student record

--- #Optional YAML start syntax 
james: 
   name: james john 
   rollNo: 34 
   div: B 
   sex: male 
… #Optional YAML end syntax 

Abbreviation

You can also use abbreviation to represent dictionaries.

Example

James: {name: james john, rollNo: 34, div: B, sex: male}

Representing List

We can also represent List in YAML. Every element(member) of list should be written in a new line with same indentation starting with “- “ (- and space).

Example

---
countries:  
   - America 
   - China 
   - Canada 
   - Iceland 
…

Abbreviation

You can also use abbreviation to represent lists.

Example

Countries: [‘America’, ‘China’, ‘Canada’, ‘Iceland’] 

List inside Dictionaries

We can use list inside dictionaries, i.e., value of key is list.

Example

---  
james: 
   name: james john 
   rollNo: 34 
   div: B 
   sex: male 
   likes: 
      - maths 
      - physics 
      - english 
… 

List of Dictionaries

We can also make list of dictionaries.

Example

---  
- james: 
   name: james john 
   rollNo: 34 
      div: B 
   sex: male 
   likes: 
      - maths 
      - physics 
      - english 

- robert: 
      name: robert richardson 
      rollNo: 53 
      div: B 
      sex: male 
   likes: 
      - biology 
      - chemistry 
…  

YAML uses “|” to include newlines while showing multiple lines and “>” to suppress newlines while showing multiple lines. Due to this we can read and edit large lines. In both the cases intendentation will be ignored.

We can also represent Boolean (True/false) values in YAML. where boolean values can be case insensitive.

Example

---  
- james: 
   name: james john 
   rollNo: 34 
   div: B 
   sex: male 
   likes: 
      - maths 
      - physics 
      - english 
   
   result: 
      maths: 87 
      chemistry: 45 
      biology: 56 
      physics: 70 
      english: 80 
   
   passed: TRUE 
   
   messageIncludeNewLines: | 
      Congratulation!! 
      You passed with 79% 
   
   messageExcludeNewLines: > 
      Congratulation!! 
      You passed with 79% 

Some common words related to Ansible.

Service/Server − A process on the machine that provides the service.

Machine − A physical server, vm(virtual machine) or a container.

Target machine − A machine we are about to configure with Ansible.

Task − An action(run this, delete that) etc managed by Ansible.

Playbook − The yml file where Ansible commands are written and yml is executed on a machine.

Leave a Reply