Puppet Class(类)特性(十一)


puppet类:

        为了通用目标或目的的组织在一起的一个或者多个资源;因此它是命名的代码块,在某个位置创建之后可在puppet全局使用.类似于其他编程语言中的类的功能,puppet的类可以继承,也可以包含子类。

        类的定义使用class name,模块下的init.pp文件定义和模块同名的类且唯一,可以为空或嵌套其他类.


puppet class(类)有两种:

1、有参数的类

2、无参数的类


puppet class(类)的特性:

1、class 类只有调用才会执行,调用称作:声明一个类

     使用include class_name

2、类的继承:

   class B_name inherits A_name   

3、子类的命名方式:

   父类::子类


示例:

修改node.pp文件,增加sh-web1主机匹配.

注释:sh-web1声明nginx类.

node /sh-(proxy|web)\d+/ {
  case $::hostname {
    "sh-proxy2": {
         include apache
         user {"test1":
            ensure => present,
            }
      }
     "sh-web1": {
            include nginx
         } 
    }
}

puppet代码nginx class.

modules/nginx/manifests/init.pp文件(无参数的类定义).
class nginx {
  package {"nginx":
    ensure=> present,
  }
}

puppet代码apche class.

modules/apache/manifests/init.pp文件(有参数的类定义).
class apache ($sta = "present") {
  package {"httpd":
    ensure=> $sta,
  }
}

sh-web1更新agent安装nginx报错如下:

[root@sh-web1 puppet]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505454999'
Error: Execution of '/usr/bin/yum -d 0 -e 0 -y list nginx' returned 1: Error: No matching Packages to list
Error: /Stage[main]/Nginx/Package[nginx]/ensure: change from absent to present failed: Execution of '/usr/bin/yum -d 0 -e 0 -y list nginx' returned 1: Error: No matching Packages to list
Notice: Finished catalog run in 6.93 seconds


原因:没有release源,更新安装eple-release源即可.

# yum install epel-release -y


更新安装nginx.

[root@sh-web1 puppet]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505454999'
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: Finished catalog run in 34.99 seconds


Class类的继承(inherits)

注意:子类的命名不支持特殊符号'.',支持下划线'_'


示例:

新建:modules\nginx\manifests\nginx.conf.pp

class nginx::nginxconf inherits nginx {
    file {"/etc/nginx/nginx.conf":
        ensure=> file,
        mode=> 0644,
        source=> 'puppet:///modules/nginx/nginx.conf',
    }
}

wKioL1m_OlbyJVDWAAF2RcDROqE318.png

注意:puppet:///modules/nginx/nginx.conf  #不需要写files目录,指定模块文件后puppet默认回去files目录下找对应的文件.


files目录存放puppet代码调用的源文件.

\modules\nginx\files\nginx.conf

wKiom1m_OlaS87UsAAFHvUDo_oI244.png

修改主机声明的class:

node /sh-(proxy|web)\d+/ {
  case $::hostname {
    "sh-proxy2": {
         include apache
         user {"test1":
            ensure => present,
            }
      }
     "sh-web1": {
        include nginx::nginxconf
 } 
    }
}

wKioL1m_OnSxHwjhAAFl3ALSdhQ673.png

注释:声明nginx::nginxconf子类后,会继承父类nginx,安装nginx软件包,然后替换文件.


sh-web1主机更新puppet agent.

[root@sh-web1 nginx]# puppet agent -t
Notice: Ignoring --listen on onetime run
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-web1.localdomain
Info: Applying configuration version '1505457148'
Notice: /Stage[main]/Nginx::Nginxconf/File[/etc/nginx/nginx.conf]/ensure: defined content as '{md5}142cb8f11006c0d78607c66cb82ae83d'
Notice: /Stage[main]/Nginx/Package[nginx]/ensure: created
Notice: Finished catalog run in 8.69 seconds


查看更新内容:

[root@sh-web1 nginx]# cat nginx.conf | grep 65535
    worker_connections  65535;


类的嵌套:


模块下的init.pp文件类必须和模块名字相同且唯一,当然Class(类)中也支持嵌套声明其他类或者定义空类.

示例:以apache为例.

wKiom1m_ZVSxAaZEAAF-SrgT_O0652.png

test\modules\apache\manifests

cat init.pp
class apache {
    include install
    include service
}

cat install.pp
    class install {
        package {"httpd":
        ensure=> present,
    }
}

cat service.pp
    class service {
        service {"httpd":
        name=> "httpd",
        ensure=> running,
        enable=> true,
        provider => init,
        require=> Class["install"],#注意:此处要求启动服务的前提时要完成install类的操作,如果不定义可能会报下面的错.
    }
}

报错:

[root@sh-proxy2 puppet]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-proxy2.localdomain
Info: Applying configuration version '1505714213'
Error: /Service[httpd]: Could not evaluate: Could not find init script for 'httpd'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Install/Package[httpd]/ensure: created
Notice: Finished catalog run in 2.75 seconds


添加完require正确的更新:

[root@sh-proxy2 puppet]# puppet agent -t
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for sh-proxy2.localdomain
Info: Applying configuration version '1505714421'
Notice: /Stage[main]/Admin/Exec[selinux]/returns: executed successfully
Notice: /Stage[main]/Install/Package[httpd]/ensure: created
Notice: /Stage[main]/Service/Service[httpd]/ensure: ensure changed 'stopped' to 'running'
Info: /Service[httpd]: Unscheduling refresh on Service[httpd]
Notice: Finished catalog run in 3.27 seconds
[root@sh-proxy2 puppet]# /etc/init.d/httpd status
httpd (pid  88530) is running...



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值