为了通用目标或目的组织在一起的一个或多个资源叫类
[root@puppet manifests]# cat test.pp class nginx { #定义类名 package {'nginx': #定义资源 ensure => present, } service {'nginx': ensure => true, require => Package['nginx'], } } include nginx #声明类 #class {'nginx'} #这种方式也可以声明类
执行
puppet apply test.pp -v -d
一个带传参的类
[root@puppet manifests]# cat test1.pp $webserver = $operatingsystem ? { /^(?i-mx:redhat|centos|fedora)/ => 'httpd', /^(?i-mx:ubuntu|debian)/ => 'apache2', } class httpd ($pkgname='apache2') { package {"$pkgname": ensure=>present, } service {"$pkgname": ensure=>true, require=>Package["$pkgname"], } } class {'httpd': pkgname => $webserver , }
执行
puppet apply test1.pp -v -d
类的继承
[root@puppet manifests]# cat test2.pp class nginx { package {"nginx": ensure=>present, } } class nginx::rproxy inherits nginx { file {'/etc/nginx/nginx.conf': ensure=>file, source=>'/tmp/nginx/nginx.rproxy.conf', notify=>Service['nginx'], } service {'nginx': ensure=>true, } } class nginx::web inherits nginx { file {'/ect/nginx/nginx.conf': ensure=>file, source=>'/tmp/nginx/nginx.web.conf', notify=>Service['nginx'], } service {'nginx': ensure=>true, } } class {'nginx::rproxy'} 或者导入 [root@puppet manifests]# cat node.pp import "/etc/puppet/manifests/test2.pp" include nginx::rproxy
执行
puppet apply node.pp -v -d
模块
[root@puppet puppet]# cd modules/ #创建nginx模块 [root@puppet modules]# ls nginx [root@puppet modules]# mkdir -pv nginx/{manifests,files,templates,lib} #必须包含的文件夹 [root@puppet nginx]# cd manifests/ [root@puppet manifests]# ls init.pp nginx.rproxy.pp nginx.web.pp #init.pp 必须包含的文件 声明类 [root@puppet manifests]# cat init.pp nginx.rproxy.pp nginx.web.pp #查看3个文件内容 都是刚才定义的类 class nginx { package {"nginx": ensure=>file, } } class nginx::rproxy inherits nginx { file {'/etc/nginx/nginx.conf': ensure=>file, source=>'puppet:///modules/nginx/nginx.rproxy.conf', #这个路径不需要files目录 会自动找 notify=>Service['nginx'], } service {'nginx': ensure=>true, } } class nginx::web inherits nginx { file {'/ect/nginx/nginx.conf': ensure=>file, source=>'puppet:///modules/nginx/nginx.web.conf', notify=>Service['nginx'], } service {'nginx': ensure=>true, } } [root@puppet files]# cp /tmp/nginx/nginx.* . #copy 文件到files 依赖或者额外的配置文件都要放到files [root@puppet files]# ls nginx.rproxy.conf nginx.web.conf
单独 [root@puppet manifests]# cat init.pp class nginx { package {"nginx": ensure=>present, } file {'/etc/nginx/nginx.conf': ensure=>file, source=>'puppet:///modules/nginx/nginx.web.conf', } service {'nginx': ensure=>true, } } 测试 [root@puppet manifests]# puppet apply -e 'include nginx' -v -d
一个网络模型下的模块
class jdk{ #声明类名 将相应的rpm包文件 放到模块路径下的files目录里 file {'/opt/jdk-8u45-linux-x64.rpm': source=>'puppet:///modules/jdk/jdk-8u45-linux-x64.rpm', group=>'root', owner=>'root', } file{'/etc/profile.d/java.sh': # ensure=>present, source=>'puppet:///modules/jdk/java.sh', group=>'root', owner=>'root', } exec {'install': cwd=>'/opt', path=>'/usr/local/bin:/usr/bin:/bin:', command=>'rpm -ivh jdk-8u45-linux-x64.rpm', creates=>'/usr/java/jdk1.8.0_45/bin/java', subscribe=>File['/opt/jdk-8u45-linux-x64.rpm'], require=>File['/opt/jdk-8u45-linux-x64.rpm'], } exec {'sh': cwd=>'/etc/profile.d/', creates=>'/etc/profile.d/java.sh', path=>'/usr/local/bin:/usr/bin:/bin', command=>'sh java.sh', require=>File['/etc/profile.d/java.sh'], } }
然后在site.pp里面声明
node 'agent.cc.com'{ include jdk }