自动化运维工具puppet三:puppet的类

puppet是适用于中大型企业的自动化运维工具,相比Ansible来说功能更加丰富,所以为了方便显示浏览,笔者将其分开来写;


puppet的资源及单机模型
puppet的变量及流程控制语句


puppet的类


类:puppet中命名的代码模块,常用于定义一组通用的目标资源,可在puppet全局调用;定义类是为了代码复用的,功能相似于bash中的函数功能;所以调用时直接调用模块中的类名即可;
类可以被继承,也可以包含子类;

语法格式:
            class NAME {
                ...puppet code...
            }

            class NAME(parameter1, parameter2) {
                ...puppet code...
            }

类代码只有声明后才会执行,调用方式:

(1) include CLASS_NAME1, CLASS_NAME2, ...
(2) class{'CLASS_NAME':
           attribute => value,
    }

eg1:

class apache2 {
        $webpkg = $operatingsystem ? {
        /(?i-mx:(centos|redhat|fedora))/        => 'httpd',
        /(?i-mx:(ubuntu|debian))/       => 'apache2',
        default => 'httpd',
        }
        package{"$webpkg":
            ensure  => installed,
        }

        file{'/etc/httpd/conf/httpd.conf':
            ensure  => file,
            owner   => root,
            group   => root,
            source  => '/tmp/httpd.conf',
            require => Package["$webpkg"],
            notify  => Service['httpd'],
        }

        service{'httpd':
            ensure  => running,
            enable  => true,
        }
}

include apache2 

注意:在单主机上不能一次调用两个class;
eg2:

class dbserver($pkg,$srv) {
          package{"$pkg":
              ensure  => latest,
          }

          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",
}

puppet类的继承


方式:
class SUB_CLASS_NAME inherits PARENT_CLASS_NAME {
        ...puppet code...
}
其实类的继承,就是将共同需要的代码块集中放置,需要时继承即可,也是代码的复用;

eg1:

class nginx {
    package{'nginx':
            ensure  => installed,
    }

    service{'nginx':
            ensure  => running,
            enable  => true,
            restart => '/usr/sbin/nginx -s reload',
    }
}
#在nginx类中都需要用到安装包、启动服务,然后在不同的主机上执行不同的配置文件
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',
        }
}

eg2:

class redis {
       package{'redis':
              ensure  => latest,
       } ->

       service{'redis':
               ensure  => running,
               enable  => true,
       }
}
class redis::master inherits redis {
        file{'/etc/redis.conf':
                ensure  => file,
                source  => '/root/manifests/redis-master.conf',
                owner   => redis,
                group   => root,
        }

        Service['redis'] {
               subscribe => File['/etc/redis.conf'],
        }
}

class redis::slave inherits redis {
       file{'/etc/redis.conf':
              ensure  => file,
              source  => '/root/manifests/redis-slave.conf',
              owner   => redis,
              group   => root,
       }

       Service['redis'] { #将父类redis中新增依赖关系属性
              subscribe => File['/etc/redis.conf'],
       }
 }      

在子类中为父类的资源新增属性或覆盖指定的属性的值:

                Type['title'] {
                    attribute1 => value,
                    ...
                }

在子类中为父类的资源的某属性增加新值:

                Type['title'] {
                    attribute1 +> value,
                    ...
                }   
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值