📜  Ansible剧本

📅  最后修改于: 2020-12-26 12:19:28             🧑  作者: Mango

Ansible剧本

剧本是用于编写Ansible代码的文件。剧本以YAML格式编写。 YAML的意思是“另外一种标记语言”,因此不需要太多语法。剧本是Ansible的核心功能之一,它告诉Ansible执行什么,并且在复杂的场景中使用。它们提供了更大的灵活性。

剧本包含用户希望在特定计算机上执行的步骤。剧本按顺序运行。剧本是Ansible所有用例的基础。

Ansible剧本比编程语言更倾向于配置语言。

通过剧本,您可以为某些主机指定特定角色,并为其他主机指定其他角色。通过这样做,您可以在非常不同的场景中编排多台服务器,所有这些都在一本书中。

剧本结构

每个剧本都是一个或多个剧本的集合。通过使用戏剧来构造剧本。一本剧本中可以有多个剧本。

游戏的函数是映射针对特定主机定义的一组指令。

YAML编辑器不同,但更喜欢使用简单的编辑器,例如notepad ++。首先,打开记事本++并复制粘贴以下YAML,然后将语言更改为YAML(语言→YAML)。

YAML始终以—(3个连字符)开头。

创建剧本

让我们从编写示例YAML文件开始。首先,我们必须定义一个任务。这些是角色和剧本Ansible模块的接口。

一本包含一个任务的剧本,包含多个任务,如下例所示。

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

   vars: 
      oracle_db_port_value : 1521
   
   tasks:
   -name: Install the Oracle DB
      yum: 
    
   -name: Ensure the installed service is enabled and running
   service:
      name: 

以上是剧本的基本语法。将其保存为文件test.yml 。 YAML语法需要遵循正确的缩进。

YAML标签

以下是一些YAML标签,例如:

Tags Explanation
Name It specifies the name of the Ansible Playbooks.
Hosts It specifies the lists of the hosts against which you want to run the task. And the host’s Tag is mandatory. It tells Ansible that on which hosts to run the listed tasks. These tasks can be run on the same machine or the remote machine. One can run the tasks on the multiple machines, and the host’s tag can have a group of host’s entry as well.
Vars Vars tag defines the variables which you can use in your playbook. Its usage is similar to the variables in any programming language.
Tasks Tasks are the lists of the actions which need to perform in the playbooks. All the playbooks should contain the tasks to be executed. A task field includes the name of the task. It is not mandatory but useful for debugging the playbook. Internally each task links to a piece of code called a module. A module should be executed, and arguments that are required for the module you want to run.