我是傀儡世界的新手。我必须创建一个模块来创建多个用户,参数化。
我创建了一个模块,如下所示。不确定是否正确。
class users () {
group { "sysadmin":
ensure => present,
}
define users::add ($username, $groupname= "" ,$shell= "",$ensure, $login= false) {
user { "$username":
ensure => "$ensure" ,
groups => "$groupname" ,
shell => "$shell" ,
require => Group["sysadmin"] ,
}
}
}
我必须使用上面的代码创建多个用户。如何以及在何处创建。我可以在同一个文件中调用吗?(init.pp)
我尝试在同一个文件中添加以下行
class users::add {
users::add { "user1": username => "myuser1" , groupname => "sysadmin" , shell => "/bin/bash" , ensure => present , login => true }
users::add { "user2": username => "myuser2" , groupname => "sysadmin" , shell => "/bin/bash" , ensure => present , login => true }
}
它不起作用。
任何帮助都非常值得赞赏。提前谢谢。
答案 0 :(得分:0)
对于多项操作,您最好使用define
代替class
,如下所示:
define users::account::add(
$userid,
$fullname,
$groups,
$shell,
$password = '',
$home_path = hiera('global::home_path')
) {
group { $name:
gid => $userid,
ensure => 'present',
} ->
user { $name:
uid => $userid,
gid => $userid,
comment => $fullname,
home => "${home_path}/${name}",
shell => $shell,
password => $password,
ensure => 'present',
groups => $groups,
}
}
这个定义可以直接使用一次或多次:
users::account::add { 'root':
userid => 0,
groups => [ ],
fullname => 'kroot',
shell => "${global::bash}",
password => '',
home_path => '',
}
users::account::add { 'raven':
userid => 1001,
groups => [ "${global::def_group}" ],
fullname => 'super admin',
shell => "${global::bash}",
password => '*',
}
users::account::add { 'admin':
userid => 1002,
groups => [ "${global::def_group}" ],
fullname => 'other admin',
shell => "${global::bash}",
password => '*',
}
非常漂亮的变体是使用这个定义与hiera:
class users::accounts {
create_resources('add', hiera('users'))
}
而hieradata应该包含用户数据:
{
"users": {
"root": {
"userid": "0",
"groups": [ ],
"fillname": "kroot",
"shell": "/bin/sh",
"password": "",
"home_path": ""
},
"raven": {
"userid": "1001",
"groups": [ "root", "wheel" ],
"fillname": "super admin",
"shell": "/usr/local/bin/bash",
"password": ""
},
"admin": {
"userid": "1002",
"groups": [ "root", "wheel" ],
"fillname": "other admin",
"shell": "/usr/local/bin/bash",
"password": ""
}
}
}