Talks is cheap, show me the code.
-
class sample($users, $password, $port) {
-
-
# defaults for a resource
-
File {
-
owner => root,
-
group => root,
-
mode => "0644",
-
}
-
-
file { "/tmp/t1.conf":
-
# source =>puppet:///, a sample needs to be located at ${modulename}/files/
-
content => template("${module_name}/t1.conf.erb"),
-
#content => "hello,$ports",
-
}
-
-
notify { "$users":}
-
-
-
-
}
# /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
-
---
-
classes:
-
sample:
-
port: 9999
-
password: fake
-
users:
-
alex:
-
home: /home/alex
-
uid: 502
-
jane:
-
home: /home/jane
-
uid: 201
-
-
environment: production
-
parameters:
-
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
-
you come from [beijing]
-
Server passed == [fake]
-
Userlist:
-
Home for alex is /home/alex
-
userid == 502
-
Home for jane is /home/jane
-
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,
-
define user::add_user($comment, $home, $shell, $uid, $gid, $password)
-
{
-
-
include user
-
-
user { "$name":
-
name =>"$name",
-
ensure => absent,
-
comment => "$comment",
-
home => "$home",
-
shell => "$shell",
-
uid => "$uid",
-
password => "$password",
-
gid => "$gid"
-
}
-
-
-
}
-
然后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) |