Puppet 简明教程
Puppet - Module
在 Puppet 中,模块可以定义为资源、类、文件、定义和模板的集合。Puppet 支持对模块进行轻松重新分配,这对于代码模块化非常有用,因为人们可以编写一个特定的通用模块,并通过非常简单的代码更改多次使用它。例如,这将在 /etc/puppet 下启用默认的站点配置,在 /etc/share/puppet 下使用 Puppet 自带的模块。
In Puppet, a module can be defined as a collection of resources, classes, files, definition, and templates. Puppet supports easy re-distribution of modules, which is very helpful in modularity of code as one can write a specified generic module and can use it multiple times with very few simple code changes. For example, this will enable default site configuration under /etc/puppet, with modules shipped by Puppet proper in /etc/share/puppet.
Module Configuration
在任何 Puppet 模块中,都有两个分区,有助于定义代码的结构和控制标记。
In any Puppet module, we have two partitions which help in defining the structure of code and controlling the denominates.
-
The search path of modules is configured using colon-separated list of directories in the puppetmasterd or masterd, the later section of Puppet’s master configuration file with the modulepath parameter.
[puppetmasterd]
...
modulepath = /var/lib/puppet/modules:/data/puppet/modules
-
Access control settings for the file server modules in fileserver.conf, the path configuration for that module is always ignored, and specifying a path will produce a warning.
Modules Source
Puppet 支持存储模块的不同位置。任何模块都可以存储在任何特定计算机的不同文件系统中。但是,必须在称为 modulepath 的配置变量中指定存储模块的所有路径,它通常是 Puppet 扫描所有模块目录并在启动时加载它们的路径变量。
Puppet supports a different location for storing modules. Any module can be stored in different file system of any particular machine. However, all the paths where modules are stored must be specified in configuration variable known as modulepath which is in general, a path variable where Puppet scans for all module directories and loads them up when it is booting up.
可以将合理默认路径配置为 -
A reasonable default path can be configured as −
/etc/puppet/modules:/usr/share/puppet:/var/lib/modules.
或者,可以将 /etc/puppet 目录建立为特殊匿名模块,该模块总是首先被搜索。
Alternatively, the /etc/puppet directory could be established as a special anonymous module, which is always searched first.
Module Naming
Puppet 遵循特定模块的相同命名标准,其中模块名称必须是匹配 [-\\w+](字母、单词、数字、下划线和连字符)的正常单词,并且不包含命名空间分隔符:: 或 /。虽然它可能允许关于模块层次结构,但对于新模块,它不能被嵌套。
Puppet follows the same naming standards of a particular module wherein the module name must be normal words, matching [-\\w+] (letter, word, number, underscore and dashes) and not containing the namespace separator: : or /. While it might be allowed regarding module hierarchies, for new modules it cannot be nested.
Module Internal Organization
当用户在 Puppet 中创建新模块时,它遵循相同的结构,并且包含以特定目录结构排列的清单、分配的文件、插件和模板,如下面的代码所示。
When the user creates a new module in Puppet, it follows the same structure and contains manifest, distributed file, plugins, and templates arranged in a specific directory structure as shown in the following code.
MODULE_PATH/
downcased_module_name/
files/
manifests/
init.pp
lib/
puppet/
parser/
functions
provider/
type/
facter/
templates/
README
每当创建一个模块时,它都包含 manifests 目录中的指定修复位置的 init.pp 清单文件。此清单文件是一个默认文件,它在任何特定模块中首先执行并包含与该特定模块相关的所有类的集合。可以将其他 .pp 文件直接添加到 manifests 文件夹下。如果我们正在添加其他 .pp 文件,则应以类命名。
Whenever a module is created, it contains init.pp manifest file at the specified fix location inside manifests directory. This manifest file is a default file which executes first in any particular module and contains a collection of all the classes associated with that particular module. Additional .pp file can be added directly under the manifests folder. If we are adding additional .pp files, they should be named after the class.
使用模块实现的关键功能之一是代码共享。模块本质上应该是自包含的,这意味着应该能够随时随地包含任何模块并将其放到模块路径中,该模块路径会在 Puppet 启动时加载。在模块的帮助下,人们可以在 Puppet 基础设施编码中获得模块化。
One of the key feature achieved by using modules is code sharing. A module by nature should be self-contained which means one should be able to include any module from anywhere and drop it onto the module path, which gets loaded when Puppet boots up. With the help of modules, one gets modularity in Puppet infrastructure coding.

Example
考虑一个 autofs 模块,它安装一个固定的 auto.homes 映射并从模板生成 auto.master。
Consider an autofs module that installs a fixed auto.homes map and generates the auto.master from templates.
class autofs {
package { autofs: ensure => latest }
service { autofs: ensure => running }
file { "/etc/auto.homes":
source => "puppet://$servername/modules/autofs/auto.homes"
}
file { "/etc/auto.master":
content => template("autofs/auto.master.erb")
}
}
文件系统将具有以下文件。
The file system will have the following files.
MODULE_PATH/
autofs/
manifests/
init.pp
files/
auto.homes
templates/
auto.master.erb
Module Lookup
Puppet 遵循预定义的结构,其中按照定义的结构包含多个目录和子目录。这些目录包含不同类型的文件,模块执行某些操作需要这些文件。幕后的少量魔术确保正确的文件与正确的上下文相关联。所有模块搜索都在模块路径中,它是目录的冒号分隔列表。
Puppet follows a pre-defined structure wherein it contains multiple directories and subdirectories in a defined structure. These directories contain different kind of files which are required by a module to perform certain actions. A little behind-the-scenes magic makes sure that the right file is associated with the right context. All module searches are within the modulepath, a colon-separated list of directories.
对于文件服务器上的文件引用,使用类似的引用,以便对 puppet 的引用://$servername/modules/autofs/auto.homes 解析为模块路径中的 autofs/files/auto.homes。
For file references on the fileserver, a similar reference is used so that a reference to puppet: //$servername/modules/autofs/auto.homes resolves to the file autofs/files/auto.homes in the module’s path.
要使模块同时与命令行客户端和 Puppet 主机可用,人们可以使用 puppet:///path 的 URL。即,没有显式服务器名称的 URL。此类 URL 的处理方式稍有不同 Puppet 和 puppetd 。Puppet 在本地文件系统中搜索无服务器 URL。
To make a module usable with both the command line client and a puppet master, one can use a URL of the from puppet:///path. i.e. a URL without an explicit server name. Such URL is treated slightly different by Puppet and puppetd. Puppet searches for serverless URL in the local file system.
模板文件的搜索方式与清单文件和文件类似:提及模板(“autofs/auto.master.erb”)将使 puppetmaster 先在 $templatedir/autofs/auto.master.erb ,然后在模块路径上的 autofs/templates/auto.master.erb 查找文件。使用 Puppet 时,可以利用 Puppet 下所有内容的 Puppet 版本。这称为模块自动加载。Puppet 将尝试从模块自动加载类和定义。
Template files are searched in a manner similar to manifest and files: a mention of template (“autofs/auto.master.erb”) will make the puppetmaster first look for a file in $templatedir/autofs/auto.master.erb and then autofs/templates/auto.master.erb on the module path. With Puppet versions of everything under the Puppet, it is available to use. This is called module auto loading. Puppet will attempt to auto-load classes and definitions from the module.