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
至此,我们已经介绍了如何用一个playbook文件运行一个plays。在小型架构或则使用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包含。从外部变量文件包含变量,在第二章已经讨论过。下面是每个包含类型的介绍:
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=keys/{{ user }}.pub dest=/home/{{ user }}/.ssh/authorized_keys mode=0600 owner={{ user }} group={{ user }} #7 We expect that a variable named user will be passed to us, and that their public key will be in the keys directory. The account is created, the ssh config directory is made, and finally we can copy this in their public key. The easiest way to use this config file would be to include it with the with_items keyword we learned about in Chapter 3, Advanced Playbooks. This would look similar to the following code:
Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七),布布扣,bubuko.com
Ansible@一个高效的配置管理工具--Ansible configure management--翻译(七)
原文地址:http://blog.csdn.net/smallfish1983/article/details/37764035