Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092190
  • 博文数量: 186
  • 博客积分: 4939
  • 博客等级: 上校
  • 技术积分: 2075
  • 用 户 组: 普通用户
  • 注册时间: 2010-04-08 17:15
文章分类

全部博文(186)

文章存档

2018年(1)

2017年(3)

2016年(11)

2015年(42)

2014年(21)

2013年(9)

2012年(18)

2011年(46)

2010年(35)

分类: 系统运维

2015-02-12 14:54:02


        所谓参数化类(Parameterized Class),就是一个class带有参数,使用的时候可以传递参数。先看例子,
  1. #init.pp
  2. class mysql (
  3. $user=mysql,
  4. $group=mysql,
  5. $service_running=true,
  6. ){
  7. class {'Your_CLass here:
  8. user =>$user,
  9. ......}
  10. }

参数可以被override.我们先看看如何用:

node 'db.example.com' {
      include mysql 
    }
这样mysql这个class的参数的值会传递到db.example.com。

看下Override的例子

node 'xxx' {
   include xxx
  class { 'mysql':   #调用class
       user            => me, # override了
       service_running =>false,
   ...}
  }

再看一例:

# modules/build_instances/manifests/init.pp

class build_instances ($instance_list={}) {

  

  define run {

    $temp = split($name, '\|')

    $server      = $temp[0]

    $instance    = $temp[1]

    $type        = inline_template("<%= @instance[0, @instance.length - 2]%>")

    $instance_id = inline_template("<%= @instance[@instance.length - 2, 2] %>")

    $shutdown_port    = 8100 + $instance_id

    $http_port        = 8300 + $instance_id

    $ajp_port         = 9100 + $instance_id

    $proxy_port       = 9500 + $instance_id

    $jmx_port         = 8500 + $instance_id

    $soffice_cron_min = 0 + $instance_id

    $max_threads   = 100

    $accept_count  = 5

    $appdynamics   = member(hiera('appdynamics_enabled', []), $instance)

    if $type == 'aspose' {

      $xmx_size     = 32G

      $xms_size     = 32G

      $new_size     = 24G

      $max_new_size = 24G

      $aspose       = "true"

      if ! defined(Package['aspose-fonts']) {

        package { 'aspose-fonts':

          ensure => installed,

          require => Yumrepo["zuoraextrasrepo"],

        }

      }

    } elsif $type == 'bet_pdf' {

      $xmx_size      = 8G

      $xms_size      = 8G

      $new_size      = 3200M

      $max_new_size  = 3200M

      $aspose        = undef

    } else {

      $xmx_size      = 8G

      $xms_size      = 8G

      $perm_size     = 1G

      $max_perm_size = 1G

      $aspose        = undef

    }

    if $server =~ /.*prod.*/ or $server =~ /.*sbx.*/ {

      $jstack_cron_if_prod  = "true"

    }

    else {

      $jstack_cron_if_prod  = undef

    }

    if $soffice_cron_min >= 60 {

      $soffice_cron_min_n = $soffice_cron_min - 60

    } else {

      $soffice_cron_min_n = $soffice_cron_min

    }

    if $fqdn == "$server" {

      notice ("Buidling instance on $instance")

      include tomcat

        tomcat::instance { $instance:

        instance_id         => $instance_id,

        shutdown_port       => $shutdown_port,

        http_port           => $http_port,

        ajp_port            => $ajp_port,

        proxy_port          => $proxy_port,

        jmx_port            => $jmx_port,

        max_threads         => $max_threads,

        accept_count        => $accept_count,

        xmx_size            => $xmx_size,

        xms_size            => $xms_size,

        perm_size           => $perm_size,

        max_perm_size       => $max_perm_size,

        new_size            => $new_size,

        max_new_size        => $max_new_size,

        soffice_cron_min    => $soffice_cron_min_n,

        jstack_cron_if_prod => $jstack_cron_if_prod,

        aspose              => $aspose,

        appdynamics         => $appdynamics,

      }

      include groundworkjdma

      groundworkjdma::instance { $instance:

        server              => $server,

        instance_id         => $instance_id,

        type                => $type,

      }

      include jolokia

      jolokia::multi_jolokia { "jolokia_${server}_${instance_id}_${type}":

        jolokia_version     =>  "1.2.3",

        jolokia_path        =>  "/home/zapp/apps/${instance_id}",

        server              => $server,

        instance_id         => $instance_id,

        type                => $type,

      }

    }

  }

  define hashDebug($server, $list) {

    $instance = split($list, ' ')

    $new_instance = inline_template("<%=

                      newlist= @list.split(' ')

                      updated_list = String.new

                      for item in newlist

                        new_value = @server + \"|\" + item

                        updated_list = updated_list + new_value + \",\"

                      end

                      updated_list.chomp(',')

                    %>")

    $new = split($new_instance, ',')

    run { $new: }

  }
###    

  create_resources(hashDebug, $instance_list)

}


然后在manifests/production.pp

node /\d+\.(fet|bet|).prod.*/ {

  $instance_list = {

     instance_set1 => { server => '5.bet.prod.slv.zuora', list => 'bet00 bet01 bet03 bet08 bet20 bet23 bet24 bet25 bet26 bet29 bet30 bet33 bet34 bet69' },

     instance_set2 => { server => '6.bet.prod.slv.zuora', list => 'bet00 bet01 bet03 bet08 bet20 bet23 bet25 bet29 bet30 bet33 bet34 bet64 bet65 bet66' },

     

     instance_set14 => { server => '4.fet.prod.slv.zuora', list => 'fet03 fet04 fet05' },

     instance_set15 => { server => '13.bet.prod.slv.zuora', list => '' },

     instance_set16 => { server => '14.bet.prod.slv.zuora', list => '' }

  }


  class { 'build_instances':

    instance_list => $instance_list,

  }

}

#这里用到了member/inline_template函数,

Templates are a powerful way of using embedded Ruby to help build config files dynamically and iterate over arrays, for example. But you can embed Ruby in your manifests directly without having to use a separate file by calling the inline_template function.

How to do it…

Here's an example of using inline_template:

Pass your Ruby code to inline_template within the Puppet manifest, as follows:

cron { 'chkrootkit':
  command => '/usr/sbin/chkrootkit >
    /var/log/chkrootkit.log 2>&1',
  # inline_template("<%= Time.now.gmtime.wday %>") hour    => inline_template('<%= @hostname.sum % 24 %>'),
  minute  => '00',
}

How it works…

Anything inside the string passed to inline_template is executed as if it were an ERB template. That is, anything inside <%= and %> delimiters will be executed as Ruby .



阅读(994) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~