English 中文(简体)
Ansible - YAML Basics
  • 时间:2024-11-05

Ansible - YAML Basics


Previous Page Next Page  

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 pke 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 
   span: B 
   sex: male 
… #Optional YAML end syntax 

Abbreviation

You can also use abbreviation to represent dictionaries.

Example

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

Representing List

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

Example

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

Abbreviation

You can also use abbreviation to represent psts.

Example

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

List inside Dictionaries

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

Example

---  
james: 
   name: james john 
   rollNo: 34 
   span: B 
   sex: male 
   pkes: 
      - maths 
      - physics 
      - engpsh 
… 

List of Dictionaries

We can also make pst of dictionaries.

Example

---  
- james: 
   name: james john 
   rollNo: 34 
      span: B 
   sex: male 
   pkes: 
      - maths 
      - physics 
      - engpsh 

- robert: 
      name: robert richardson 
      rollNo: 53 
      span: B 
      sex: male 
   pkes: 
      - biology 
      - chemistry 
…  

YAML uses “|” to include newpnes while showing multiple pnes and “>” to suppress newpnes while showing multiple pnes. Due to this we can read and edit large pnes. 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 
   span: B 
   sex: male 
   pkes: 
      - maths 
      - physics 
      - engpsh 
   
   result: 
      maths: 87 
      chemistry: 45 
      biology: 56 
      physics: 70 
      engpsh: 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.

Advertisements