码迷,mamicode.com
首页 > 其他好文 > 详细

Intro to Playbooks

时间:2018-09-01 22:08:05      阅读:242      评论:0      收藏:0      [点我收藏+]

标签:计算   ogr   contain   control   multi   end   src   Suite   script   

Intro to Playbooks

Playbooks简介

About Playbooks

关于剧本

Playbooks are a completely different way to use ansible than in adhoc task execution mode, and are particularly powerful.

与adhoc任务执行模式相比,Playbooks使用ansible是一种完全不同的方式,并且功能特别强大。

Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.

简而言之,playbooks是真正简单的配置管理和多机器部署系统的基础,与已有的系统不同,并且非常适合部署复杂的应用程序。

Playbooks can declare configurations, but they can also orchestrate steps of any manual ordered process, even as different steps must bounce back and forth between sets of machines in particular orders. They can launch tasks synchronously or asynchronously.

Playbooks可以声明配置,但它们也可以协调任何手动订购流程的步骤,即使不同的步骤必须在特定订单的机器组之间来回跳转。他们可以同步或异步启动任务。

While you might run the main /usr/bin/ansible program for ad-hoc tasks, playbooks are more likely to be kept in source control and used to push out your configuration or assure the configurations of your remote systems are in spec.

虽然您可以运行/usr/bin/ansible主程序来执行临时任务,但是更有可能将源代码保留在源代码管理中并用于推出配置或确保远程系统的配置符合规范。

There are also some full sets of playbooks illustrating a lot of these techniques in the ansible-examples repository. We’d recommend looking at these in another tab as you go along.

ansible-examples存储库中还有一些完整的手册说明了很多这些技术 我们建议您在另一个标签中查看这些内容。

There are also many jumping off points after you learn playbooks, so hop back to the documentation index after you’re done with this section.

在学习了剧本后,还有许多跳跃点,所以在完成本节后,请跳回文档索引。

Playbook Language Example

Playbook语言示例

Playbooks are expressed in YAML format (see YAML Syntax) and have a minimum of syntax, which intentionally tries to not be a programming language or script, but rather a model of a configuration or a process.

Playbooks以YAML格式表示(请参阅YAML语法),并且具有最少的语法,有意尝试不是编程语言或脚本,而是配置或进程的模型。

Each playbook is composed of one or more ‘plays’ in a list.

每个剧本由列表中的一个或多个“剧本”组成。

The goal of a play is to map a group of hosts to some well defined roles, represented by things ansible calls tasks. At a basic level, a task is nothing more than a call to an ansible module (see Working With Modules).

游戏的目标是将一组主机映射到一些定义明确的角色,由ansible调用任务表示。在基本级别,任务只不过是对ansible模块的调用(请参阅使用模块)。

By composing a playbook of multiple ‘plays’, it is possible to orchestrate multi-machine deployments, running certain steps on all machines in the webservers group, then certain steps on the database server group, then more commands back on the webservers group, etc.

通过编写多个“播放”的剧本,可以编排多机部署,在Web服务器组中的所有计算机上运行某些步骤,然后在数据库服务器组上执行某些步骤,然后在Web服务器组上执行更多命令,等等。

“plays” are more or less a sports analogy. You can have quite a lot of plays that affect your systems to do different things. It’s not as if you were just defining one particular state or model, and you can run different plays at different times.

“戏剧”或多或少是体育比喻。你可以有很多戏剧影响你的系统做不同的事情。这并不是说你只是定义了一个特定的状态或模型,而是可以在不同的时间运行不同的plays。

For starters, here’s a playbook that contains just one play:

对于初学者来说,这是一个只包含一个游戏的剧本:

---
- hosts: webservers
  vars:
    http_port: 80
    max_clients: 200
  remote_user: root
  tasks:
  - name: ensure apache is at the latest version 确保apache是最新版本
    yum:
      name: httpd
      state: latest
  - name: write the apache config file 写apache是最新版本
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf
    notify:
    - restart apache
  - name: ensure apache is running 确保apache正在进行
    service:
      name: httpd
      state: started
  handlers:
    - name: restart apache
      service:
        name: httpd
        state: restarted

Playbooks can contain multiple plays. You may have a playbook that targets first the web servers, and then the database servers. For example:

Playbooks可以包含多个plays。您可能有一个首先针对Web服务器,然后是数据库服务器的手册。例如:

---
- hosts: webservers
  remote_user: root

  tasks:
  - name: ensure apache is at the latest version 确保apache是??最新版本
    yum:
      name: httpd
      state: latest
  - name: write the apache config file 编写apache配置文件
    template:
      src: /srv/httpd.j2
      dest: /etc/httpd.conf

- hosts: databases
  remote_user: root

  tasks:
  - name: ensure postgresql is at the latest version 确保postgresql是最新版本
    yum:
      name: postgresql
      state: latest
  - name: ensure that postgresql is started 确保postgresql启动
    service:
      name: postgresql
      state: started

You can use this method to switch between the host group you’re targeting, the username logging into the remote servers, whether to sudo or not, and so forth. Plays, like tasks, run in the order specified in the playbook: top to bottom.

您可以使用此方法在您要定位的主机组,登录到远程服务器的用户名,是否为sudo等之间切换。与任务一样,播放按照剧本中指定的顺序运行:从上到下。

Below, we’ll break down what the various features of the playbook language are.

下面,我们将分解playbook语言的各种功能。

Basics

基础知识

Hosts and Users

主机列表和用户

For each play in a playbook, you get to choose which machines in your infrastructure to target and what remote user to complete the steps (called tasks) as.

对于剧本中的每个游戏,您可以选择基础架构中的哪些计算机作为目标,以及用什么远程用户完成步骤(称为任务)。

The hosts line is a list of one or more groups or host patterns, separated by colons, as described in the Working with Patterns documentation. The remote_user is just the name of the user account:

hosts行是一个由冒号分隔的一个或多个组或主机模式的列表,如使用模式 文档中所述。remote_user只是用户帐户的名称:

---
- hosts: webservers
  remote_user: root

The remote_user parameter was formerly called just user. It was renamed in Ansible 1.4 to make it more distinguishable from the user module (used to create users on remote systems).

remote_user参数以前只称为user它在Ansible 1.4中重命名,使其与用户模块(用于在远程系统上创建用户)更加区分

Intro to Playbooks

标签:计算   ogr   contain   control   multi   end   src   Suite   script   

原文地址:https://www.cnblogs.com/cevinchen/p/9571578.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!