设为首页 收藏本站
查看: 1293|回复: 0

[经验分享] Puppet Class(类)特性(十一)

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2017-9-21 09:35:46 | 显示全部楼层 |阅读模式
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类.
1
2
3
4
5
6
7
8
9
10
11
12
13
node /sh-(proxy|web)\d+/ {
  case $::hostname {
    "sh-proxy2": {
         include apache
         user {"test1":
            ensure => present,
            }
      }
     "sh-web1": {
            include nginx
         }
    }
}



puppet代码nginx class.
1
2
3
4
5
6
modules/nginx/manifests/init.pp文件(无参数的类定义).
class nginx {
  package {"nginx":
    ensure=> present,
  }
}



puppet代码apche class.
1
2
3
4
5
6
modules/apache/manifests/init.pp文件(有参数的类定义).
class apache ($sta = "present") {
  package {"httpd":
    ensure=> $sta,
  }
}



sh-web1更新agent安装nginx报错如下:
1
2
3
4
5
6
7
8
9
[iyunv@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源即可.
1
# yum install epel-release -y




更新安装nginx.
1
2
3
4
5
6
7
8
[iyunv@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
1
2
3
4
5
6
7
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:
1
2
3
4
5
6
7
8
9
10
11
12
13
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.
1
2
3
4
5
6
7
8
9
[iyunv@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




查看更新内容:
1
2
[iyunv@sh-web1 nginx]# cat nginx.conf | grep 65535
    worker_connections  65535;




类的嵌套:

模块下的init.pp文件类必须和模块名字相同且唯一,当然Class(类)中也支持嵌套声明其他类或者定义空类.
示例:以apache为例.
wKiom1m_ZVSxAaZEAAF-SrgT_O0652.png
test\modules\apache\manifests
1
2
3
4
5
cat init.pp
class apache {
    include install
    include service
}



1
2
3
4
5
6
cat install.pp
    class install {
        package {"httpd":
        ensure=> present,
    }
}



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



报错:
1
2
3
4
5
6
7
8
9
[iyunv@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正确的更新:
1
2
3
4
5
6
7
8
9
10
11
12
[iyunv@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
[iyunv@sh-proxy2 puppet]# /etc/init.d/httpd status
httpd (pid  88530) is running...







运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-404823-1-1.html 上篇帖子: Puppet C/S初探 site.pp文件介绍(十) 下篇帖子: Puppet node节点的特性(十二)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表