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

[经验分享] learning puppet 3— Classes and Modules

[复制链接]

尚未签到

发表于 2015-11-26 14:13:33 | 显示全部楼层 |阅读模式
Classes are Puppet's way of separating out chunks of code, and modules are Puppet's way of organizing classes so that you can refer to them by name.Classes
  Classes are named blocks of Puppet code, which can be created in one place and invoked elsewhere.

  • Defining a class makes it available by name, but doesn’t automatically evaluate the code inside it.
  • Declaring a class evaluates the code in the class, and applies all of its resources.
you can declare class in tow ways:include ClassName or class{'ClassName': }Modules
To help you split up your manifests into an easier to understand structure, Puppet uses modules and the module autoloader.  It works like this:

  • Modules are just directories with files, arranged in a specific, predictable structure. The manifest files within a module have to obey certain naming restrictions.
  • Puppet looks for modules in a specific place (or list of places). This set of directories is known as the modulepath, which is a configurable setting.
  • If a class is defined in a module, you can declare that class by name in any manifest. Puppet will automatically find and load the manifest that contains the class definition.
The Other Subdirectories:  We've seen two of the subdirectories in a module, but there are several more available:

  • manifests/ — Contains all of the manifests in the module.
  • files/ — Contains static files, which managed nodes can download.
  • templates/ — Contains templates, which can be referenced from the module’s manifests. More on templates later.
  • lib/ — Contains plugins, like custom facts and custom resource types.
  • tests/ or examples/ — Contains example manifests showing how to declare the module’s classes and defined types.
  • spec/ — Contains test files written with rspec-puppet.
Organizing and Referencing Manifests --the relationship between manifests and its class name
Each manifest in a module should contain exactly one class or defined type. (More on defined types later.)  Each manifest’s filename must map to the name of the class or defined type it contains. The init.pp file, which we used above, is special — it always contains a class (or defined type) withthe same name as the module. Every other file must contain a class (or defined type) named as follows:
  <MODULE NAME>::<FILENAME>
  …or, if the file is inside a subdirectory of manifests/, it should be named:
  <MODULE NAME>::<SUBDIRECTORY NAME>::<FILENAME>
  So for example, if we had an apache module that contained a mod_passenger class:

  • File on disk: apache/manifests/mod_passenger.pp
  • Name of class in file: apache::mod_passenger
  You can see more detail about this mapping at the namespaces and autoloading page of the Puppet reference manual.
Class Parameters
  Example:
  class echo_class ($to_echo = &quot;default value&quot;) {
         notify {&quot;What are we echoing? ${to_echo}.&quot;:}
}
  class {'echo_class':
         to_echo => 'Custom value',
}
  the $to_echo is a class parameter, and you can assign value to it just like set the class's atribute value.
Inheritance
  Classes can be derived from other classes using the inherits keyword. This allows you to make special-case classes that extend the functionality of a more general “base” class.
  You should only use class inheritance when you need to override resource attributes in the base class. This is because you can instantiate a base class by including it inside another class’s definition, and assigning a direct parent scope is rarely necessarysince you can use qualified variable names to read any class’s internal data.
  Additionally, many of the traditional use cases for inheritance (notably the “anti-class” pattern, where you override a service resource’s ensure attribute to disable it) can be accomplished just as easily with class parameters. It is also possible to useresource collectors to override resource attributes.
  class base::freebsd inherits base::unix {
     File['/etc/passwd'] {
     group => 'wheel'
     }
     File['/etc/shadow'] {
     group => 'wheel'
     }
}
  the base class base::unix has resource ”file“ named '/etc/passwd' and '/etc/shadow';

运维网声明 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-143949-1-1.html 上篇帖子: puppet 系统管理工具 下篇帖子: 【puppet】记录笔记
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

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

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

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

扫描微信二维码查看详情

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


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


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


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



合作伙伴: 青云cloud

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