首页 > 代码库 > Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)

Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)

如无书面授权,请勿转载

Larger Projects
Until now, we have been looking at single plays in one playbook file. This approach
will work for simple infrastructures, or when using Ansible as a simple deployment
mechanism. However, if you have a large and complicated infrastructure, then you
will need to take actions to prevent things from going out of control. This chapter
will include the following topics:
?	 Separating your playbooks into different files, and including them from some
other location
?	 Using roles to include multiple files that perform a similar function
?	 Methods for increasing the speed at which Ansible configures your machines

第四章 大型项目中Ansible的使用

至此,我们已经介绍了如何用一个playbook文件运行一个plays。在小型架构或则使用Ansible做一个简单的部署机制时这已经很有用了。但是,如果你有一个很大很复杂的系统架构,你可能需要一些额外的操作来保证事情不会超出我们的控制。本章包含以下主题:

  • 将你的playbooks分离成多个文件,存储在不同的地方
  • 使用角色包含多个文件来完成同样的功能
  • 加速使用Ansible配置机器的方法

Includes
One of the first issues you will face with a complex infrastructure is that your
playbooks will rapidly increase in size. Large playbooks can become difficult to read
and maintain. Ansible allows you to combat this problem by the way of includes.
Includes allow you to split your plays into multiple sections. You can then include
each section from other plays. This allows you to have several different parts built
for a different purpose, all included in a main play.
There are four types of includes, namely variable includes, playbook includes,
task includes, and handler includes. Including variables from an external vars_file
files has been discussed already in Chapter 2, Simple Playbooks. The following is a
summary of what each includes does:
?	 Variable includes: They allow you to put your variables in external
YAML files
?	 Playbook includes: They are used to include plays from other files in a s
ingle playLarger Projects
?	 Task includes: They let you put common tasks in other files and include
them wherever required
?	 Handler includes: They let you put all your handlers in the one place

包含

在大型复杂架构中,你第一个要面对的问题就是不断增长的playbooks文件大小,一个很大的playbooks很难去理解和维护,解决办法是使用包含includes。将你的plays分解成多个不同的段,然后在其他的plays中包含他们。不同的段根据不同目的地分类,全部包含在主play中。

有四种类型的包含,分别是变量包含、playbook包含、任务包含、处理程序handler包含。从外部变量文件包含变量,在第二章已经讨论过。下面是每个包含类型的介绍:

  • 变量包含:允许你将变量存在外部YAML文件
  • playbook包含:一个大型项目中可以包含多个plays
  • 任务包含:将任务放到普通文件中,当需要的时候包含他们
  • 处理程序Handler包含:允许你将所有的handlers处理程序放到一个地方

Task includes
Task includes can be used when you have a lot of common tasks that will be
repeated. For example, you may have a set of tasks that removes a machine, from
monitoring, and a load balancer before you can configure it. You can put these tasks
in a separate YAML file, and then include them from your main task.
Task includes inherit the facts from the play they are included from. You can also
provide your own variables, which are passed into the task and available for use.
Finally, task includes can have conditionals applied to them. If you do this,
conditionals will separately be added to each included task. The tasks are all still
included. In most cases, this is not an important distinction, but in circumstances
where variables may change, it is.
The file to include as a task includes contains a list of tasks. If you assume the
existence of any variables and hosts or groups, then you should state them in
comments at the top. This makes reusing the file much easier.
So, if you wanted to create a bunch of users and set up their environment with their
public keys, you would split out the tasks that do a single user to one file. This file
would look similar to the following code:
---
# Requires a user variable to specify user to setup
- name: Create user account
#2
user: name={{ user }} state=present
#3
#1
- name: Make user SSH config dir
#4
file: path=/home/{{ user }}/.ssh owner={{ user }} group={{ user
}} mode=0600 state=directory
#5
- name: Copy in public key
#6
copy: src=http://www.mamicode.com/keys/{{ user }}.pub dest=/home/{{ user>