Puppet (4)-Class, template language, module code reuse: Class, Module class
A class is a set of resources for public purposes, a named block of code that can be called at puppet Global after creation, and classes can inherit classes that are the basic components of our building blocks
类:命名的puppet代码块,好处就是可以重复调用,需要时可通过名称进行调用; class my_class { ...puppet code... } puppet code: 包含变量赋值、各种条件判断语句、资源声明等等; 注意: 类的名字只能以小写字母开头 调用类: include class1, class2, ... class {‘classname‘: }
Each class introduces a new variable scope (scope), which means that whenever a variable of a class is accessed, its fully qualified name is used, but local scope can re-assign a new value to a variable in the top scope
Example one:
class apache{ package{‘httpd‘: ensure => installed, } file{‘httpd.conf‘: path => ‘/etc/httpd/conf/httpd.conf‘, ensure => file, require => Package[‘httpd‘], } service{‘httpd‘: ensure => running, require => Package[‘httpd‘], subsribe => File[‘httpd.conf‘], } }
Example two:
class nginx{ $webserver=‘nginx‘ package{$webserver: ensure => latest, } file{"/etc/$webserver/nginx.conf": ensure => file, source => "/opt/puppet/modules/$webserver/files/nginx.conf", require => Package[$webserver], # notify => Service[‘nginx‘], } service{$webserver: ensure => running, enable => true, hasstatus => true, hasrestart => true, # restart => ‘systemctl reload nginx.service‘, require => [ Package[$webserver], File["/etc/$webserver/nginx.conf"]], subscribe => File["/etc/$webserver/nginx.conf"], } } class {‘nginx‘: }
There are four ways to declare a class:
1.使用include 2.使用require 3.像声明一个资源一样声明类 4.使用ENC的风格来声明一个类 1&3是最常用的 第一种:include base::linux,apache 可以用逗号隔开声明多个类 include nginx 第二种: require apache这种方式很少用 第三种: class {‘nginx‘: version => ‘2.2.21‘ } 可以传入参数,所以很常用 注意:类只有在声明后方才执行
To define a class with parameters:
class my_class ($para1=value1, $para2=value2) { ...puppet code... }
When invoked, pass parameters:
class {‘class_name‘: para1 => new_value1, para2 => new_value2, }
Example one:
class createuser ($grpname=testgrp,$username=testuser) { group{"$grpname": ensure => present, } user{"$username": ensure => present, gid => $grpname, } } class {‘createuser‘: grpname => mageedu, username => mageedu, }
Example two:
class nginx ($webserver=‘nginx‘){ package{$webserver: ensure => latest, } file{"/etc/$webserver/nginx.conf": ensure => file, source => "/opt/puppet/modules/$webserver/files/nginx.conf", require => Package[$webserver], # notify => Service[‘nginx‘], } service{$webserver: ensure => running, enable => true, hasstatus => true, hasrestart => true, # restart => ‘systemctl reload nginx.service‘, require => [ Package[$webserver], File["/etc/$webserver/nginx.conf"]], subscribe => File["/etc/$webserver/nginx.conf"], } } class {‘nginx‘: $webserver = ‘nginx’, }
Inheritance of the class:
How to define:
class base_class_name { ... puppet code ... } class base_class_name::class_name inherits base_class_name { ...puppet code... }
Base_class_name::class_name This is a specification of the inheritance class naming, and you'll know at a glance that this is a subclass of a class (it's just a name, it doesn't have any effect, you don't think it inherits the other classes, this naming is called fully qualified names)
inherits base_class_name 表示继承哪个类
The function of the inheritance of classes: inheriting an existing class and implementing overriding resource properties, or appending additional values to resource properties
For example, I have defined a class before, and this class defines a package resource-installing Nginx, and I now want to define a class that has the resources to modify the configuration file and start the service, and then inherit the class.
(1) inheriting resource attributes;
(2) covering resource attributes;
=
(3) additional resource attributes;
+>
Classes have several key points in implementing inheritance:
(1) 声明子类时,其基类会被首先声明; (2) 基类成为子类的父作用域,因为基类变量和属性默认值会被子类直接复制一份 (3) 子类可以覆盖父类中同一资源的相同属性值
An instance of an inheriting class:
class nginx { package {‘nginx‘: ensure => latest, } -> service {‘nginx‘: enable => true, ensure => running, hasrestart => true, hasstatus => true, restart => ‘service nginx reload‘ } } class nginx::webserver inherits nginx { file{‘/etc/nginx/nginx.conf‘: source => ‘/opt/puppet/modules/nginx/files/nginx_web.conf‘ ensure => file, notify => Service[‘nginx‘], } } class nginx::haproxy inherits nginx { file{‘/etc/nginx/nginx.conf‘: source => ‘/opt/puppet/modules/nginx/files/nginx_proxy.conf‘ ensure => file, notify => Service[‘nginx‘], } }
The above puppet code defines a base class implementation package installation, in addition we have defined two sub-classes, respectively, to implement the Webserver and proxy functions.
But how do you declare it on a machine? A node can not take two roles, when to use it?
In the future we will do master/agent model, two node, the first node include Nginx::webserver this class, and another node include Nginx::haproxy
可以引用测试:include nginx::webserver
Advanced Inheritance:
After inheritance, the subclass changes the value of the property in the parent class resource:
Class Nginx {package {' Nginx ': ensure = latest,}-service {' Nginx ': Enable = true, ensure = running, Hasrestart = true, Hasstatus = True, Restart = ' Service Nginx reload '}} class Nginx::webserver inherits Nginx {package[' Nginx ']{# #这里是资源引用 uppercase [' tittle '] name = ' Tengine ',} file{'/etc/nginx/nginx.conf ': Source = '/opt/puppet/modules/nginx/files/nginx_web.conf ' ensure = file, notify = Serv ice[' Nginx '],}} class Nginx::haproxy inherits Nginx {file{'/etc/nginx/nginx.conf ': SOURC E = '/opt/puppet/modules/nginx/files/nginx_proxy.conf ' ensure = file, notify = Service[' Nginx '],}} to use a resource reference when redefining a property in a parent resource package[' nginx ']{name = ' Tengine ', the subclass can not only modify properties, but also add such as: package[' Nginx ']{ Name +> ' varnish ',}
With the class we can organize the module, but in order to make the module more powerful, we can use the template in puppet, then we need to learn the puppet template language.
Before we learn the template language, what is the role of the template language before we start?
举个例子,前面我们 定义了nginx类,类中定义了package资源,file资源,service资源。 其中file资源定义的是 nginx的配置文件,配置文件里有一个配置项需要根据服务器的cpu个数,如何灵活的根据服务器cpu的个数来配置nginx中的这一项参数呢?写死是一种方式,然后在拷贝多个配置文件,定义不同的子类,子类在重新定义file资源中的source属性,也可以实现,但是不是很繁琐,所以我们可以想象有这么一种方式,在nginx.conf文件中定义一个变量,变量的值就是取服务器的cpu个数,那么静态文件中定义变量,用什么来解释呢。在puppet中当然就由puppet来解释,而这种在静态文件中定义变量以及写逻辑判断的做法就叫做模版语言。所以我觉得模版语言 其实就是针对file资源使用的语言。puppet是ruby语言开发的,所以puppet 模版文件中嵌套的语言也是ruby语言。
Template: Based on the Erb embedded Ruby template language, he says that programming elements, such as variables, are used in static files to generate text files (profiles) for a variety of environments. Embedded Ruby is referred to as the ERB, which is primarily used to implement embedded Ruby code in a text file. The original text information is not changed back, but Ruby code executes. The results of the execution will be replaced directly with the original code:
语法: <%= Ruby Expression %>: 替换为表达式的值; <% Ruby Expression %>: 仅执行代码,而不替换。 <%# comment %>: 文本注视 <%% 表示转义符 输出为<%这个符号,一般用不到 %%> 输出为 %> ,用不到 <%- Ruby code %>, 忽略空白字符 <% Ruby code -%> ,忽略空白行。 能够用到的只有第一种 模版语言中可以使用puppet中的任意可用变量,但是变量名要以@符号开头。 在模版中还可以使用循环 ,条件判断等各种复杂语法格式。 条件判断: <% if condition -%> some text <% else %> some other text <% end %> 迭代,像for循环 <% @ArrayName.echo do | variable_name %> some text with <%- variable_name %> <% end %> 这里ArrayName是 puppet的一个数组变量。 variable_name 为赋值给的变量,相当于 for i in list ,list是ArrayName , i是 variable_name条件和迭代 不要求会,能看懂别人写的代码就行 了,关键我们会使用变量就行了。比如上面说到nginx的配置文件问题。
How do I reference a template in a resource definition?
First we change the source of the file to the part about the boot process to the following:
user nginx; worker_processes <%= @processorcount %>; error_log /var/log/nginx/error.log; pid /run/nginx.pid;
For this kind of file containing the template language, we apply the property to the file resource to change the usage, you cannot use the Source property, instead of the content property and the puppet built-in template function template (), as follows:
class Nginx {package {' Nginx ': ensure = latest,}-service {' Ngin X ': Enable = true, ensure = running, Hasrestart = True, Hasstatus = = true, restart = ' service Nginx reload '}} class Nginx::webserver inherits Nginx { package[' nginx ']{name = ' Tengine ',} file{'/etc/nginx/nginx.conf ': Source = '/opt/puppet/modules/nginx/files/nginx_web.conf ', ensure = file, notify = Service[' Nginx '], }} class Nginx::haproxy inherits Nginx {file{'/etc/nginx/nginx.conf ': #source = '/op t/puppet/modules/nginx/files/nginx_proxy.conf ' content = Template ('/OPT/PUPPET/MODULES/NGINX/FILES/NGINX_PR Oxy.conf '), ensure = file, notify = Service[' Nginx '],}} include Nginx::hapro XY
The above is the template language, and how the template language is called.
Modules
Puppet module: A separate, self-contained directory structure that is organized to achieve a complete function
Module Name: directory name
Directory structure:
modules/ module_name/: manifests/ init.pp: 必须声明一个类,类名与模块名相同; *.pp: MODULE_NAME::[SUBDIR_NAME]::MANIFESTS_FILE_NAME files/: 静态文件 puppet url: puppet:///modules/MODULE_NAME/[SUBDIR_NAME]/FILE_NAME file{‘nginx.conf‘: source => puppet:///modules/nginx/nginx.conf } templates/: 模板文件:*.erb template(‘MODULE_NAME/TEMPLATE_FILE_NAME‘); file{‘nginx.conf‘: content => template(‘模板文件‘), } lib/: 插件 tests/: 模块使用说明文档,以及事例example spec/: lib目录下的插件使用说明文档
Puppet model
ACTIONS: build Build a module release package. changes Show modified files of an installed module. generate Generate boilerplate for a new module. install Install a module from the Puppet Forge or a release archive. list List installed modules search Search the Puppet Forge for a module. uninstall Uninstall a puppet module. upgrade Upgrade a puppet module.
Puppet (4)-Class, template language, module