Ansible 简明教程

Ansible - YAML Basics

Ansible 使用 YAML 语法来表示 Ansible 剧本。本章节将概述 YAML。与 XML 和 JSON 等其他数据格式相比,Ansible 使用 YAML 是因为它非常容易让人理解、阅读和编写。

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.

每个 YAML 文件都可以选择以 “---” 开头和以 “…​” 结尾。

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

Understanding YAML

在本节中,我们将会了解 YAML 数据的各种表示方式。

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

key-value pair

YAML 使用简单的键值对来表示数据。字典采用键值对的形式表示。

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

Note - : 和值之间应该有空格。

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

我们还可以在 YAML 中表示列表。列表的每个元素(成员)都应该写在单独的行中,并且缩进方式相同,以 “- “ (- 和空格)开头。

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 使用 “|” 在显示多行时包含换行符,使用 “>” 在显示多行时抑制换行符。因此,我们可以读取和编辑长行。在这两种情况下,都将忽略缩进。

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.

我们还可以在 YAML 中表示 Boolean (真/假)值。其中, boolean 值不区分大小写。

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%