在磁盘上,一个模块对应一个特殊的目录结构:
- MODULE_NAME
- manifests
- files
- templates
- lib
- tests
- spec
例如,一个名为"my_module"的模块,它的目录结构应该是:
- my_module - 顶层目录,对应到模块名称。
- manifests/ - 模块所有的清单。
- init.pp - 类定义。该文件中的类名必须对应到模块名称。
- other_class.pp - 类定义。该文件中的类名必须是my_module::other_class。
- my_defined_type.pp - 函数定义。该文件中的函数名必须是my_module::my_defined_type。
- implementation/ - 清单子目录。
- foo.pp - 类定义。该文件中的类名必须是my_module::implementation::foo。
- bar.pp - 类定义。该文件中的类别必须是my_module::implementation::bar。
- files/ - 静态文件目录,能够被被代理节点下载。
- service.conf - 该文件对应的URL是puppet://modules/my_module/service.conf。
- lib/ - 插件目录,例如自定义的行为和自定义的资源类型。查阅"使用插件"获取更多的细节。
- templates/ - 清单中用到的模板文件目录。查阅"模版"获取更多的细节。
- component.erp - 在清单中可以使用template('my_module/component.erp')的方式使用该模版。
- tests/ - 讲述如何使用类和函数的一些实例。
- init.pp
- other_class.pp - 清单目录中的每个类和函数在实例目录中都可以有对应的实例。
- spec/ - 讲述如何使用插件的一些实例。
manifests目录中的每个清单文件都只能定义一个类或函数,文件名称要与其中定义的类或函数名称对应。
init.pp是特殊的清单文件,包含的类名同模块名。其它清单文件包含的类或函数要满足下面的规则:
模块名 | :: | 其它目录::(如果存在) | 文件名(不包含后缀) | my_module | :: | | other_class | my_module | :: | implementation:: | foo |
- my_module::other_class必须在my_module/manifests/other_class.pp文件中定义。
- my_module::implementation::foo必须在my_module/manifests/implementation/foo.pp定义。
类名中间的双冒号(::)被称为命名空间分割符(namespace separator)。
模块名只能包含小写字母、数字和下划线,而且必须以小写字母开头,也就是必须满足正则表达式[a-z][a-z0-9_]*。类名在相同限制上再加上不能包含命名空间分隔符。一些特殊的名称是不允许的,例如:
模块中的静态文件都可以通过file资源类型的source属性访问。每个静态文件都可以映射到一个URL:
协议 | 三个斜杠符号 | “modules”/ | 模块名/ | 文件名 | puppet: | /// | modules/ | my_module/ | service.conf | 例如,puppet:///modules/my_module/service.conf对应的文件是my_module/files/service.conf。
每个ERB模版文件(查阅"模版"获取更多信息)都可以通过template函数被输出。输出的内容是一个简单的字符串,通常作为file资源类型的content属性,或者保存到一个变量。
template函数按照下面的方式定位模版文件:
template函数 | (‘ | 模块名/ | 模版名 | ’) | template | (' | my_module/ | component.erb | ') | 例如,template('my_module/component.erb')对应的文件是my_module/templates/component.erb。 |