Puppet 木偶定义的类型和变量

Puppet 木偶定义的类型和变量,puppet,Puppet,我是puppet新手,我正在尝试编写一个模块来管理10个用户的.bashrc文件。以下代码可以管理1个用户的文件。但是,我无法更改代码以管理10个用户的文件。我尝试使用定义的类型和变量,但运气不佳。请告诉我做这件事的正确方法好吗 init.pp: class profile ( $bashrc = $profile::params::bashrc, $bashrc_host = $profile::params::bash

我是puppet新手,我正在尝试编写一个模块来管理10个用户的.bashrc文件。以下代码可以管理1个用户的文件。但是,我无法更改代码以管理10个用户的文件。我尝试使用定义的类型和变量,但运气不佳。请告诉我做这件事的正确方法好吗

        init.pp:
        class profile (
        $bashrc      = $profile::params::bashrc,
        $bashrc_host = $profile::params::bashrc_host,

        ) inherits profile::params {

      anchor { 'profile::begin': } ->
      class { '::profile::config': } ->
      anchor { 'profile::end': }
      }

     config.pp:

     class profile::config inherits profile {

     file { $bashrc:
     ensure => file,
     source  => "puppet:///$bashrc_host",
     }
     params.pp:

    class profile::params {

    $bashrc_host        = "modules/profile/$fqdn_user1_bashrc"

    }
    case $::osfamily {

    'RedHat': {
     $bashrc = '/home/user1/.bashrc'
    }

    }

如果本模块的目的是学习木偶,那么:

将param用户添加到类配置文件::params

class profile::params {

$bashrc_host        = "modules/profile/$fqdn_user1_bashrc"
$user               = 'user1',

}
case $::osfamily {

'RedHat': {
 $bashrc = "/home/$user/.bashrc"
}

}
在这之后,您可以使用数组或hiera的组合,这仍然不是最优雅的解决方案,而是婴儿步骤


如果您打算为各种用户实际管理bashrc,我建议您使用预先存在的模块,例如

,这根本不是一个类的任务。正如您在报告中指出的,这实际上需要一个
define

请不要在你的名字中使用动词。不要定义用户,只需执行

define profile::user($host_name) {
}
恕我直言,我不知道在定义中使用模块参数有什么好模式。但是,您可以使用以下模式:

class profile(
  $default_shell = $profile::params::default_shell,
  $default_prompt = $profile::params::default_prompt,
  $users = {}
) inherits profile::params {

  $defaults = { shell => $default_shell, prompt => $default_prompt }

  create_resources('profile::user', $users, $defaults)
}
发生的是

  • 值取自params、hiera或调用清单
  • 这些值收集在
    $defaults
    数组中
  • 对于
    $users
    散列中没有
    shell
    prompt
    的任何资源,使用此默认值

  • 感谢Prateek的回复。我使用您要求安装的模块中的相同格式编写了执行此任务的代码。但是,考虑到使用params.pp和现有环境的最佳实践,代码只能使用params、config和init文件放置。因此,我对如何在params文件中使用定义的类型或数组感到困惑。另外,我们还没有实现heira。下面的代码可以工作。但是,应该使用params.pp:class profile{profile::defineuser{“user”:host_name=>host1,}defineuser.pp define-appuserprofile::defineuser(username=$name){file{/home/${username}/.bashrc:sure=>file,source=>puppet:///modules/profile/${fqdn}{username}{u bashrc“,}}Frank,我收到以下错误。不确定这是否与我使用的puppet版本或ruby版本有关:无法检索包\u提供程序:未初始化的常量Gem错误:无法从远程服务器检索目录:服务器上的错误400:无法创建未知类型的资源profile::user at/etc/puppet/modules/profile/manifests/init.pp:12在我使用的nodeuppet版本上是2.7。该代码段假设您有一个
    define profile::user
    ,并且它接受参数
    shell
    prompt
    ,以及在
    users
    散列中传递的任何内容。我建议在尝试之前仔细阅读
    create\u resources
    函数使用此代码。Frank,我试图阅读有关create_resource的内容。但是,由于我对puppet的基础知识还不太熟悉,所以这一点让我不知所措。下面是我如何调整代码的。请告诉我下面代码中的错误。我的要求是将.bashrc文件放入不同用户的主目录中。params.pp:define profile::user(username=$name){case$::osfamily{'RedHat':{$bashrc='/home/$name/.bashrc'}init.pp:$users=('user1'))继承profile::params{create_resources('profile::user',$users)它是“Felix”,实际上。这不是一个很好的论坛来教你这个主题,因为你缺乏更广泛的理解。请在寻求更具体的帮助之前完成。我的个人资料还链接到一本书,虽然这不是最温和的介绍,更像是一个速成课程。