Puppet: Puppet是如何找到你写在Manifests文件的配置的

简介: 今天群里有人问了我问题,其实说的我挺晕,因为好多基础知识点都理解的有问题,或者说还没有仔细看文档。
今天群里有人问了我问题,其实说的我挺晕,因为好多基础知识点都理解的有问题,或者说还没有仔细看文档。于是,我想在这里说说Puppet Master环境下,Master是如何查找你写的Manifests文件、模块和类并编译的。

限定:我们这次只讨论Master - Agent环境中Master在接受到一个主机的请求时的情况,而且也只讨论pp文件,也不涉及缓存的内容。


Puppet: <wbr>Puppet是如何找到你写在Manifests文件的配置的

从上面的图,我们可以看到,Master接收到Agent的请求后,会查找自己的Manifests及相关的配置,并找到请求节点相关的内容,然后编译成Catalog,发回给Agent执行。
这个过程大概有这样几个步骤:
1. Master加载配置文件
2. 查找节点定义,查看节点中声明的资源和类
3. 查找到这些资源和类,编译生成Catalog


首先,我们先从配置文件说起,下面的URL是Puppet的配置说明页面。
http://docs.puppetlabs.com/references/latest/configuration.html

其中有一个值叫manifests, 用途是指定Puppet Master的Manifests入口,意思是说Master是从这个文件开始读取Manifests的。

manifest

The entry-point manifest for puppet master.

  • Default: $manifestdir/site.pp

这个参数的默认值是$manifestdir/site.pp,这里的$manifestdir是什么呢?它也是Puppet的一个配置项,默认值是$confdir/manifests。讨厌的是里面又有一个$confdir变量,同样它也是配置项,默认值是/etc/puppet

manifestdir

Where puppet master looks for its manifests.

  • Default: $confdir/manifests

confdir

The main Puppet configuration directory. The default for this setting is calculated based on the user. If the process is running as root or the user that Puppet is supposed to run as, it defaults to a system directory, but if it’s running as any other user, it defaults to being in the user’s home directory.

  • Default: /etc/puppet

所以Master默认情况下Manifests入口就是/etc/puppet/manifests/site.pp。它会读取这个文件来查找请求主机的节点的定义的类和资源。

一般情况下,我们建议让site.pp的内容为"import nodes/*.pp",这样,我们就在同级目录下创建一个nodes目录,并创建相应的pp文件来书写每个(组)节点的配置。

同样,我建议在节点pp文件中,只写节点有关的变量,具体的实现由加载不同的模块来完成。比如
production.pp
nodes /^web\d+\.xxx\.com$/ {
    $www_home="/data/www"
    include httpd
    include httpd::vhost
}
这样是指定所有web开头的主机安装Apache应用,并指定了$www_home变量的内容。

而Puppet是如何去加载httpd的类的呢?

http://docs.puppetlabs.com/puppet/3/reference/modules_fundamentals.html
这篇文章写了Puppet模块的基本原理,推荐阅读。
在上面的配置列表中,我们可以查到模块默认可以存在 $confdir/modules和/usr/share/puppet/modules中,一般情况下,我们是保存在/etc/puppet/modules下。
模块中的结构如下
Puppet: <wbr>Puppet是如何找到你写在Manifests文件的配置的
其中manifests/init.pp要求定义同模块名相同的类,然后其它的pp文件中应使用my_module::other_class形式的类名。
原因是nodes中声明加载的类会被::打散,用来告诉自动加载器如何从模块中找到对应的文件。

When a class or defined resource is declared, Puppet will use its full name to find the class or defined type in your modules. Names are interpreted as follows:

当类或自定义资源类型被声明时,Puppet将使用全名来查找它们在你的模块中的位置。查找过程如下:

  • The first segment in a name (excluding the empty “top” namespace) identifies the module. Every class and defined type should be in its own file in the module’s manifests directory, and each file should have the .pp file extension.
  • 名字中的第一节用来识别模块,每个类都应该在自己的文件夹下有一个manifests文件名字的pp文件
  • If there are no additional namespaces, Puppet will look for the class or defined type in the module’sinit.pp file.
  • Otherwise, Puppet will treat the final segment as the file name and any interior segments as a series of subdirectories under the manifests directory.
  • 如果类名只有第一节,那么Puppet将会找init.pp,如果有两节,将会查找第二节名字对应的pp文件,如果有3节则会查找第2节名字目录下的第3节名字的pp文件,以此类推。

Thus, every class or defined type name maps directly to a file path within Puppet’s modulepath:

如此,所有的类或自定义类型都对应能找到一个文件

name file path
apache /apache/manifests/init.pp
apache::mod /apache/manifests/mod.pp
apache::mod::passenger /apache/manifests/mod/passenger.pp

Note again that init.pp always contains a class or defined type named after the module, and any other.pp file contains a class or type with at least two namespace segments. (That is, apache.pp would contain a class named apache::apache.)

记住只有init.pp中定义与模块名相同的类,其它的类名都至少有::分隔的两节。


由此,我们能看到,Puppet Master是首先加载site.pp,然后通过import nodes/*.pp来加载节点的定义,然后通过命名空间的自动加载来找到对应模块的类的定义。大概过程就是这样。


PS:Puppet对语法和manifests的检查没有那么的严格,比如说,你可以把你的配置都写进site.pp,也可以在init.pp中写多个类,但是为了管理方便,我还是建议你按照它的最佳实践来写。

多看文档,现在Puppet里还有不少坑没解决,多看才能尽量避开。例如,命名空间和自动加载文件里写了如下内容:

Important note: Earlier versions of Puppet used namespaces to navigate nested class/type definitions, and the code that resolves names still behaves as though this were their primary use.This can sometimes result in the wrong class being loaded. This is a major outstanding design issue (issue #2053) which will not be resolved in Puppet 3. See below for a full description of the issue.

目录
相关文章
puppet替换文件中的string
版权声明:本文可能为博主原创文章,若标明出处可随便转载。 https://blog.csdn.net/Jailman/article/details/84783855 ...
838 0
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1072 0
|
网络协议 安全 网络安全
|
Perl 存储 数据挖掘