Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1092732
  • 博文数量: 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)

分类: 系统运维

2016-08-27 12:30:22

  Talks is cheap, show me the code. 

  1. class sample($users, $password, $port) {

  2. # defaults for a resource
  3.   File {
  4.     owner => root,
  5.     group => root,
  6.     mode => "0644",
  7.    }

  8.   file { "/tmp/t1.conf":
  9.          # source =>puppet:///, a sample needs to be located at ${modulename}/files/
  10.          content => template("${module_name}/t1.conf.erb"),
  11.         #content => "hello,$ports",
  12.      }

  13.     notify { "$users":}



  14. }

#  /templates/t1.conf.erb
you come from [<%= @location %>]
Server passed == [<%= @password %>]
Userlist:
<% @users.each do |key,val| -%>
    Home for <%= key %> is <%= @users[key]['home'] %>
    userid == <%= val['uid'] %>
<% end -%>

# ENC

  1. ---
  2. classes:
  3.   sample:
  4.     port: 9999
  5.     password: fake
  6.     users:
  7.       alex:
  8.         home: /home/alex
  9.         uid: 502
  10.       jane:
  11.         home: /home/jane
  12.         uid: 201

  13. environment: production
  14. parameters:
  15.   location: beijing
puppet agent -t
Info: Using configured environment 'production'
Info: Retrieving pluginfacts
Info: Retrieving plugin
Info: Caching catalog for   xxxx
Info: Applying configuration version '1472271742'
Notice: {alex => {home => /home/alex, uid => 502}, jane => {home => /home/jane, uid => 201}}
Notice: /Stage[main]/Sample/Notify[{alex => {home => /home/alex, uid => 502}, jane => {home => /home/jane, uid => 201}}]/message: defined 'message' as '{alex => {home => /home/alex, uid => 502}, jane => {home => /home/jane, uid => 201}}'
Notice: Applied catalog in 0.05 seconds

 cat /tmp/t1.conf

  1. you come from [beijing]
  2. Server passed == [fake]
  3. Userlist:
  4.     Home for alex is /home/alex
  5.     userid == 502
  6.     Home for jane is /home/jane
  7.     userid == 201
可以看到 1. parameter在ENC里面是top scope,  2. ENC的classes可以带参数。并且是node scope的。

     现在parameterized class有3种办法传递参数了,第一个是默认值,第二个就是本例子所示,还有一个就是hiera的自动lookup了。只要你参数写好,然后对应的hiera的yaml文件有参数的就可以了。之前可能还需要scope.lookupvar之类的,现在4.6版本已经改成scope[ ] 了,而且只在ERB里面用。

    顺带说下parameterized class,
  1. define user::add_user($comment, $home, $shell, $uid, $gid, $password)
  2. {

  3. include user

  4. user { "$name":
  5. name =>"$name",
  6. ensure => absent,
  7. comment => "$comment",
  8. home => "$home",
  9. shell => "$shell",
  10. uid => "$uid",
  11. password => "$password",
  12. gid => "$gid"
  13. }


  14. }

然后init.pp
   class user ($lists
){
create_resources(user::add_user, $lists)

}
### ENC
---
classes:
  user:
    list:
      jeckman:
        comment: Jack
        gid: 100
        home: /home/jeckman
        name: jeckman
        password: xxxxxx
        shell: /bin/bash
        uid: 10146
      saga:
        comment: Arun
        gid: 100
        home: /home/saga
        name: saga
        password: xxxxx
        shell: /bin/bash
        uid: 70960

environment: production
parameters:

结果是:
Notice: /Stage[main]/User/Notify[{jeckman => {comment => Jack, gid => 100, home => /home/jeckman, name => jeckman, password => xxxxxx, shell => /bin/bash, uid => 10146}, saga => {comment => Arun, gid => 100, home => /home/saga, name => saga, password => xxxxx, shell => /bin/bash, uid => 70960}}]/message: defined 'message' as '{jeckman => {comment => Jack, gid => 100, home => /home/jeckman, name => jeckman, password => xxxxxx, shell => /bin/bash, uid => 10146}, saga => {comment => Arun, gid => 100, home => /home/saga, name => saga, password => xxxxx, shell => /bin/bash, uid => 70960}}'
Notice: Applied catalog in 0.05 seconds

此时$list就是参数了,它从ENC读取,然后是个hash给create_resources()用。

注意define里面的参数不能用$name和$title,
Note that the special variables $title and $name are both set to the defined type’s name automatically, so they can’t be used as parameters.

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