Ansible 简明教程

Ansible - Playbooks

在本篇中,我们将了解 Ansible 中的剧本。

In this chapter, we will learn about Playbooks in Ansible.

剧本是编写 Ansible 代码的文件。剧本以 YAML 格式编写。YAML 代表 Yet Another Markup Language(另一种标记语言)。 Playbooks 是 Ansible 的核心功能之一,它告诉 Ansible 要执行什么操作。它们对于 Ansible 而言就像一个待办事项列表,其中包含任务列表。

Playbooks are the files where Ansible code is written. Playbooks are written in YAML format. YAML stands for Yet Another Markup Language. Playbooks are one of the core features of Ansible and tell Ansible what to execute. They are like a to-do list for Ansible that contains a list of tasks.

剧本包含用户想要在特定机器上执行的步骤。剧本按顺序运行。剧本是 Ansible 所有用例的构建块。

Playbooks contain the steps which the user wants to execute on a particular machine. Playbooks are run sequentially. Playbooks are the building blocks for all the use cases of Ansible.

Playbook Structure

每个剧本都是其中一个或多个剧目的集合。剧本使用剧目进行构建。剧本中可以包含多个剧目。

Each playbook is an aggregation of one or more plays in it. Playbooks are structured using Plays. There can be more than one play inside a playbook.

剧目的功能是对针对特定主机定义的一组指令进行映射。

The function of a play is to map a set of instructions defined against a particular host.

YAML 是一种严格类型化的语言;因此,在编写 YAML 文件时需要格外小心。有不同的 YAML 编辑器,但我们更喜欢使用简单的编辑器,如记事本。只需打开记事本,复制并粘贴以下 yaml,将语言更改为 YAML(语言 → YAML)。

YAML is a strict typed language; so, extra care needs to be taken while writing the YAML files. There are different YAML editors but we will prefer to use a simple editor like notepad. Just open notepad and copy and paste the below yaml and change the language to YAML (Language → YAML).

YAML 以 ---(3 个连字符)开头

A YAML starts with --- (3 hyphens)

Create a Playbook

让我们从编写一个示例 YAML 文件开始。我们将逐步了解 yaml 文件中编写的每个部分。

Let us start by writing a sample YAML file. We will walk through each section written in a yaml file.

---
   name: install and configure DB
   hosts: testServer
   become: yes

   vars:
      oracle_db_port_value : 1521

   tasks:
   -name: Install the Oracle DB
      yum: <code to install the DB>

   -name: Ensure the installed service is enabled and running
   service:
      name: <your service name>

上面是一个示例剧本,我们尝试介绍剧本的基本语法。将上面的内容保存到一个文件 test.yml 。YAML 语法需要遵循正确的缩进,在编写语法时需要小心。

The above is a sample Playbook where we are trying to cover the basic syntax of a playbook. Save the above content in a file as test.yml. A YAML syntax needs to follow the correct indentation and one needs to be a little careful while writing the syntax.

The Different YAML Tags

现在,让我们浏览不同的 YAML 标签。下面描述了不同的标签:

Let us now go through the different YAML tags. The different tags are described below −

name

此标签指定 Ansible 剧本的名称。它指定该剧本的作用。可以给剧本指定任何合乎逻辑的名称。

This tag specifies the name of the Ansible playbook. As in what this playbook will be doing. Any logical name can be given to the playbook.

hosts

此标签指定我们想要对之运行任务的主机或主机组的列表。hosts 字段/标签是必填项。它会告知 Ansible 应在哪些主机上运行所列任务。可以在同一台机器或远程机器上运行任务。可以将任务运行在多台机器上,因此 hosts 标签也可以包含一组主机条目。

This tag specifies the lists of hosts or host group against which we want to run the task. The hosts field/tag is mandatory. It tells Ansible on which hosts to run the listed tasks. The tasks can be run on the same machine or on a remote machine. One can run the tasks on multiple machines and hence hosts tag can have a group of hosts’ entry as well.

vars

Vars 标签允许你定义剧本中可用的变量。用法类似于任何编程语言中的变量。

Vars tag lets you define the variables which you can use in your playbook. Usage is similar to variables in any programming language.

tasks

所有剧本都应该包含任务或待执行的任务列表。任务是需要执行的一系列操作。tasks 字段包含任务的名称。该名称用作用户的帮助文本。它不是必填项,但有助于调试剧本。每个任务在内部链接到称为模块的代码块。应该执行的模块以及要执行的模块所需的论证。

All playbooks should contain tasks or a list of tasks to be executed. Tasks are a list of actions one needs to perform. A tasks field contains the name of the task. This works as the help text for the user. It is not mandatory but proves useful in debugging the playbook. Each task internally links to a piece of code called a module. A module that should be executed, and arguments that are required for the module you want to execute.