puppet之模块详解

简介:

显示当前系统装载的所有模块信息

[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 apache

Notice: 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]# ll

total 100

-rw-r--r-- 1 root  root     175 Aug 21 13:54 1

-rw-r--r-- 1 root  root    4178 Jun 10 05:09auth.conf

drwxr-xr-x 3 root  root    4096 Aug 27 13:35environments

-rw-r--r-- 1 root  root    1462 Jun 10 05:08fileserver.conf

drwxr-xr-x 2 root  root    4096 Jun 10 05:09manifests

drwxr-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.rpmsave

drwxrwx--x 8 puppet puppet  4096 Aug 21 10:21 ssl

其中,/etc/puppet/manifests 为站点清单目录,是用来定义各节点的清单,哪个agent到底使用哪个类,就在这里定义

 

在manifests目录下创建站点清单文件

[root@node3 puppet]# cd manifests

[root@node3 manifests]# ls

site.pp

要声明一个站点就要使用node加节点的fqdn ,确保其一定可以被解析,否则无效

[root@node3 manifests]# hostname

node3.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.pp

node '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 的名称必须保持一致 否则会报错

 


本文转自zuzhou 51CTO博客,原文链接:http://blog.51cto.com/yijiu/1545648

相关文章
|
Java 应用服务中间件 Linux
puppet连载九:linux安装jdk、tomcat模块
安装jdk1.8.0_91和tomcat8.0.36 mkdir -p /etc/puppet/modules/linuxjdktomcat/{manifests,templates,files} vi /etc/puppet/modules/linuxjdktomcat/manifests/init.
1040 0
|
MySQL 关系型数据库 Linux
puppet连载10:linux安装percona57/56/55、sysbench、tpcc模块
在服务端/puppet/soft下建my.cnf,内容为https://www.jianshu.com/p/c63fc6c71279 在服务端/puppet/soft下建changemysql57pass.
1017 0
|
Linux 开发工具 git
puppet连载七:linux基础组件安装模块
linux基础组件安装模块linuxbaseinstall 更换源,安装gcc gcc-c++ glibc-devel make ncurses-devel openssl-devel autoconf git mkdir -p /etc/puppet...
1046 0
puppet连载六:创建测试模块test
创建测试模块 mkdir -p /etc/puppet/modules/test/{manifests,templates,files} vi /etc/puppet/modules/test/manifests/init.
853 0
|
网络协议 Linux 安全
puppet连载八:linux优化模块
linux优化模块 在服务端先建立文件limits.con vi /puppet/soft/limits.conf soft nofile 102400 hard nofile 102400 soft nproc 102400 hard nproc 102400 保存,退出 在服务端建立文件sysctl.
779 0
|
测试技术
puppet cron 模块
转载:http://blog.51cto.com/ywzhou/1577299 Puppet模块章节环境说明 服务端 | 客户端 操作系统:CentOS 6.
945 0
|
安全 Linux 网络协议
puppet yum模块、配置仓储、mount模块
转载:http://blog.51cto.com/ywzhou/1577335 作用:自动为客户端配置YUM源,为使用yum安装软件包提供便捷。 1、服务端配置yum模块 (1)模块清单 [root@puppet ~]# tree /etc/puppe...
1070 0
|
网络安全
puppet puppet模块、file模块
转载:http://blog.51cto.com/ywzhou/1577356 作用:通过puppet模块自动控制客户端的puppet配置,当需要修改客户端的puppet配置时不用在客户端一一设置。
969 0
|
监控 网络协议 网络架构
puppet host模块
转载:http://blog.51cto.com/ywzhou/1577432作用:自动配置客户端的hosts文件,解决网络内的计算机之间计算机名称解析问题,适用于没有部署DNS服务器的内网环境。
1053 0
|
存储 Linux 网络安全
puppet ssh模块
转载:http://blog.51cto.com/ywzhou/1577502 作用:通过SSH模块管理客户端的ssh远程服务,并用key认证方式替代密码认证方式,提高安全性; 本例分两阶段,首先是ssh的安装、配置及服务管理,然后是使用如何转换成key认证方式。
1348 0