Puppet 之 類的介紹
puppet的類:把多個相關的資源定義在一起,組成一個類.可以在其他的代碼段include這個類.puppet還支持有限制的類的繼承,作用就是在子類裏面的屬性可以覆蓋父類裏面的屬性.
類:puppet中命名的代碼模塊,常用於定義一組通用目標的資源,可在puppet全局調用;
類可以被繼承,也可以包含子類;
類的名稱只能以小寫字母開頭,可以包含小寫字母、數字和下劃線
每個類都會引入一個新的變量scope,這意味著任何時候訪問類中的變量時,都得使用其完全限定名稱,不過,在本地scope可以重新為top scope中的變量賦予一個新值
2 語法介紹
語法格式:
class NAME { ...puppet code... }
#以下定義類,並傳遞參數
class NAME(parameter1, parameter2) { ...puppet code... }
類的調用
類代碼只有聲明後才會執行,調用方式:
(1) include CLASS_NAME1, CLASS_NAME2, ...
(2) class {'CLASS_NAME':
attribute => value,
}
例子
定義類並通過include調用
class instwebsvr { $webserver = $osfamily ?{ "RedHat" => 'httpd', /(?i-mx:debian)/ => 'apache2', default => 'httpd', } package {"$webserver": ensure => installed, } } include instwebsvr
示例2:傳遞參數,
class dbserver($pkg,$srv){ package {"$pkg": ensure => latest, before => Service["$srv"], } service{"$srv": ensure => running, } } if $operatingsystem =="CentOS" or $operatingsystem == "RedHat" { case $operatingsystemmajrelease { '7':{$pkgname = 'mariadb-server' $srvname='mariadb'} default:{$pkgname = 'mysql-server' $srvname='mysqld'} } } #聲明類,向形參傳遞參數,這裏實參$pkgname和形參$pkg不同名。可以同名,但是建議不同名比較合理,容易區分 class {'dbserver': pkg => "$pkgname", srv => "$srvname", }
定義賦值,如果變量沒有傳值為空值,就直接使用括號內的值,如果有傳值,就使用變量傳過來的為準,例子如下
class dbserver($pkg='mariadb-server',$srv='mariadb'){ package {"$pkg": ensure => latest, before => Service["$srv"], } service{"$srv": ensure => running, } } if $operatingsystem =="CentOS" or $operatingsystem == "RedHat" { case $operatingsystemmajrelease { '7':{$pkgname = 'mariadb-server' $srvname='mariadb'} default:{$pkgname = 'mysql-server' $srvname='mysqld'} } } class {'dbserver': pkg => "$pkgname", srv => "$srvname", }
類繼承的方式
如父類中的參數不完整,在子類中在豐富一些參數,子類可以繼承父類的參數,實現代碼的復用
class SUB_CLASS_NAME inherits PARENT_CLASS_NAME { ...puppet code... }
#引用父類中的service,引用用中括號,後面用花括號增加選項,花括號裏的選項優先級比引用的高。符號+>表示在原來值的基礎上增加選項
在子類中為父類的資源新增屬性或覆蓋指定的屬性的值:
Type['title'] { attribute1 => value, ... }
在子類中為父類的資源的某屬性增加新值:
Type['title'] { attribute1 +> value, ... }
示例1:
當nginx的子配置文件發生變化時,子類調用父類中的資源和參數,實現代碼復用
class nginx { package{'nginx': ensure => installed, } service{'nginx': ensure => running, enable => true, restart => '/usr/sbin/nginx -s reload', } } #nginx::web:是指 父類::子類 class nginx::web inherits nginx { Service['nginx'] { subscribe => File['ngx-web.conf'], } file{'ngx-web.conf': path => '/etc/nginx/conf.d/ngx-web.conf', ensure => file, source => '/root/manifests/ngx-web.conf', } } class nginx::proxy inherits nginx { Service['nginx'] { subscribe => File['ngx-proxy.conf'], } file{'ngx-proxy.conf': path => '/etc/nginx/conf.d/ngx-proxy.conf', ensure => file, source => '/root/manifests/ngx-proxy.conf', } }
示例2:
使用puppet分別實現主從redis
準備主從節點的配置文件
主節點73配置文件修改如下兩處
vim redis-slave.conf bind 0.0.0.0 port 6379
從節點72配置文件修改如下三處
vim redis-slave.conf bind 0.0.0.0 port 6379 slaveof 172.18.50.72 6379
這裏使用單機模式,所以需要把文件復制到對應機器,然後調用定義都不一樣,如下,67和68都要安裝puttet.註意如果是主從模式,就不需要這麽操作,這裏主要是演示單機模式(standalone)的情況。將主從的配置文件模板拷貝到對應的機器的/root/puppet/redis.module目錄下。進行puppet配置
主節點73上配置
vim redis_master.pp class redis { package{'redis': ensure => latest, before => Service['redis'], } service{'redis': ensure => running, enable => true, hasrestart => true, restart => 'service redis restart', require => Package['redis'], } } class redis::master inherits redis { file{'/etc/redis.conf': ensure => file, source => '/root/puppet/redis.module/redis-master.conf', owner => redis, group => root, require => Package['redis'], } Service['redis'] { restart => 'systemctl restart redis.service', subscribe => File['/etc/redis.conf'], } } include redis::master
從節點72上配置
vim redis_slave.pp class redis { package{'redis': ensure => latest, } service{'redis': ensure => running, enable => true, hasrestart => true, restart => 'service redis restart', require => Package['redis'], } } class redis::slave inherits redis { file{'/etc/redis.conf': ensure => file, source => '/root/puppet/redis.module/redis-slave.conf', owner => redis, group => root, require => Package['redis'], } Service['redis'] { restart => 'systemctl restart redis.service', subscribe => File['/etc/redis.conf'], } } include redis::slave
測試:
在主從節點上分別運行
puppet apply redis-master.pp puppet apply redis-slave.pp
然後在主節點上操作
redis-cli 127.0.0.1:6379> SET mykey 'hello,i am master 73' OK 127.0.0.1:6379> get mykey
從節點上驗證
redis-cli 127.0.0.1:6379> get mykey "hello,i am master 73"
則redis主從通過puppet配置成功
Puppet 之 類的介紹