您的位置:首页 > 其它

puppet之模块详解

2014-08-27 14:33 169 查看
显示当前系统装载的所有模块信息[root@node3 class]# puppet module list/etc/puppet/modules (no modules installed)/usr/share/puppet/modules (no modulesinstalled)no modules installed说明没有任何模块
在puppet的官方站点已经有许多模块提供给我们使用https://forge.puppetlabs.com 第三方模块未必使用,但是对其框架进行修改有的时候是必要的,所以精确理解模块是什么而且在master/agent模型下,几乎都是基于模块进行实现的 如果我们想查看有哪些模块提供使用只要使用命令puppetmodule search [关键字]如下所示[root@node3 class]# puppet module search apacheNotice: Searching https://forgeapi.puppetlabs.com ...NAME DESCRIPTION AUTHOR KEYWORDS example42-apache Puppet module forapache @example42 example42,apache 如果看到有需要的则进行安装就可以了,如下所示[root@node3 ~]# puppet module install 模块名模块的查看[root@node3 ~]# puppet module list这个命令上面已经提到了查看帮助信息[root@node3 class]# puppet help master/agent 模块的创建假如我们想创建一个能够管理nginx的模块,那么这个模块名也叫nginx,由此:[root@node3 class]# mkdir -p./nginx/{manifests,files,lib,templates,test,spec}创建完其模块对应目录后,我们来查看模块列表[root@node3 ~]# mkdir -p/etc/puppet/modules/nginx/{manifests,files,lib,templates,test,spec}[root@node3 ~]# puppet module list/etc/puppet/modules└──nginx (???)/usr/share/puppet/modules (no modules installed)在/usr/share/puppet/modules/nginx/manifests这个目录下至少需要一个清单文件,叫init.pp 并且能有一个类,这个类还得与当前模块同名因此我们为其创建一个空类[root@node3 ~]# cd /etc/puppet/modules/nginx/manifests/创建清单文件,文件名必须是init.pp
拷贝配置文件置modules目录[root@node3 manifests]# cp /etc/nginx/nginx.conf /etc/puppet/modules/nginx/files/执行模块[root@node3 manifests]# puppet apply/etc/puppet/modules/nginx/manifests/init.pp [root@node3 manifests]# cat init.pp class nginx { package{'nginx': ensure =>installed, }} class nginx::web inherits nginx { file{'/etc/nginx/nginx.conf': ensure =>file, mode =>0644, owner =>root, group =>root, source =>'puppet:///modules/nginx/nginx.conf', require =>Package['nginx'], } service{'nginx': ensure =>true, subscribe=> File['/etc/nginx/nginx.conf'], }}include nginx::web
配置站点清单每个节点在所谓的master/agent的模式下,angent需要联系master,通过master为自己生成catalog,所以在这种场景下为某个节点定义配置的时候,需要通过站点清单来实现的像刚才我们定义的manifests目录,是定义在模块中的而在puppet配置文件目录下还需有一个目录为master/angent模式提供清单 安装puppet-server[root@node3 manifests]# yum install puppet-server[root@node3 manifests]# cd /etc/puppet/[root@node3 puppet]# lltotal 100-rw-r--r-- 1 root root 175 Aug 21 13:54 1-rw-r--r-- 1 root root 4178 Jun 10 05:09auth.confdrwxr-xr-x 3 root root 4096 Aug 27 13:35environments-rw-r--r-- 1 root root 1462 Jun 10 05:08fileserver.confdrwxr-xr-x 2 root root 4096 Jun 10 05:09manifestsdrwxr-xr-x 3 root root 4096 Aug 27 11:22 modules-rw-r--r-- 1 root root 853 Jun 10 05:08puppet.conf-rw-r--r-- 1 root root 63344 Aug 22 15:52 puppet.conf.rpmsavedrwxrwx--x 8 puppet puppet 4096 Aug 21 10:21 ssl其中,/etc/puppet/manifests 为站点清单目录,是用来定义各节点的清单,哪个agent到底使用哪个类,就在这里定义 在manifests目录下创建站点清单文件[root@node3 puppet]# cd manifests[root@node3 manifests]# lssite.pp要声明一个站点就要使用node加节点的fqdn ,确保其一定可以被解析,否则无效[root@node3 manifests]# hostnamenode3.test.com
清单文件名必须要以site.pp来命名,否则模块是不会生效的
node 'node3' { #明确指定哪个节点使用哪个模块 include nginx #说明文件使用哪个类
}之后还需要定义模块[root@node3 manifests]# vim /etc/puppet/modules/nginx/manifests/init.pp将include nginx 去掉,因为模块只有被调用的时候才执行,这样才是真正意义上的模块应用模块[root@node3 manifests]# puppet apply/etc/puppet/manifests/site.pp 使用继承类[root@node3 manifests]# pwd
/etc/puppet/modules/nginx/manifests将其改名[root@node3 manifests]# mv init.pp nginxweb.pp声明一个空类[root@node3 manifests]# cat init.pp class nginx { }而后再编辑nginxweb.pp更改参数如下class nginx::web inherits nginx 使其继承父类 再创建一个类为tengine.pp[root@node3 manifests]# cp nginxweb.pp tengine.pp[root@node3 manifests]# vim tengine.pp更改参数:class nginx { package{'nginx': ensure=> installed, }} class nginx::tengine inherits nginx { file{'/etc/nginx/nginx.conf': ensure => file, mode =>0644, owner =>root, group =>root, source=> 'puppet:///modules/nginx/tengine.conf', require=> Package['nginx'], } service{'nginx': ensure=> true, subscribe =>File['/etc/nginx/nginx.conf'], }}include nginx::tengine 这样我们想将站点1安装nginx,而不是安装tengine在我们站点清单的里声明即可[root@node3 manifests]# vim site.ppnode 'node3.test.com' { includenginx::tengine}应用测试
puppet apply site.pp
Warning: Could not retrieve fact fqdn
Warning: Host is missing hostname and/or domain: node3
Notice: Compiled catalog for node3 in environment production in 0.92 seconds
Error: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine' returned 1: Error:No matching Packages to list
Error: /Stage[main]/Nginx::Tengine/Package[tengine]/ensure: change from absentto present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y list tengine'returned 1: Error: No matching Packages to list
Notice: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: DependencyPackage[tengine] has failures: true
Warning: /Stage[main]/Nginx::Tengine/File[/etc/nginx/nginx.conf]: Skippingbecause of failed dependencies
Notice: /Stage[main]/Nginx::Tengine/Service[nginx]: Dependency Package[tengine]has failures: true
Warning: /Stage[main]/Nginx::Tengine/Service[nginx]: Skipping because of faileddependencies
Notice: Finished catalog run in 1.38 seconds很显然我们是定义的tengine类 而且yum源中没有这个rpm 所以是不会被安装的这样一来我们可以将各个类放在不同的文件中了假如node1运行的时候期望运行的是webserver 而另外一个节点则装载的是反向代理的配置很显然它们之间的配置是不一样的,但是它们有些东西是一样的,比如安装server 但是装载的文件不一样,因此我们可以更进一步分割类 区分两者不同puppet类不支持多重继承,因此不能多次继承,也不能直接继承多个类但是我们可以使init.pp进行安装nginx ,其他模块继承init.pp[root@node3 manifests]# pwd/etc/puppet/modules/nginx/manifests使init.pp只负责安装class nginx { package {'nginx': allow_virtual => false, ensure=> installed, }} 修改nginxweb.pp使其只配置nginx web server 其父类是nginx#只需要将之前的package类剪切至init.pp即可[root@node3 manifests]# cat nginx_web.pp class nginx::web inherits nginx { file{'/etc/nginx/nginx.conf': ensure =>file, mode =>0644, owner =>root, group =>root, source =>'puppet:///modules/nginx/nginx.conf', require =>Package['nginx'], } service{'nginx': ensure =>true, subscribe=> File['/etc/nginx/nginx.conf'], }} 创建nginx_proxy.pp 文件,使其只配置proxy功能class nginx::nginx_proxy inherits nginx {file {'/etc/nginx/nginx.conf': ensure=> file, source=> 'puppet:///modules/nginx/nginx-proxy.conf', mode =>0644, owner =>root, group =>root, require=> Package['nginx'], notify=> Service['nginx'], } service {'nginx': ensure => running, }} 回到/etc/puppet/manifests目录我们让其安装nginx_web,如下所示:[root@node3 manifests]# vim site.pp node 'node3' {
include nginx::nginx_web
}执行脚本并查看结果[root@node3 manifests]# puppet apply site.pp
Notice: Compiled catalog for node3 in environment production in 1.25 seconds
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: /Stage[main]/Nginx::Nginx_web/Service[nginx]/ensure: ensure changed'stopped' to 'running'
Notice: Finished catalog run in 21.85 seconds 定义模块需要注意的是site.pp 文件中的inculude 、模块的名称 、还有定义的class 的名称必须保持一致 否则会报错
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  puppet puppet模块