Puppet 木偶罐头';找不到模板的变量

Puppet 木偶罐头';找不到模板的变量,puppet,Puppet,刚开始使用Puppet,我的第一个模板有问题。这应该很容易,但我想不出来 我有一个“基地”模块 还有其他东西,但不是必须的 base/manifests/init.pp: class base { include base::install, base::service, base::config, base::params } base/manifests/config.pp class base::config { include base::params File {

刚开始使用Puppet,我的第一个模板有问题。这应该很容易,但我想不出来

我有一个“基地”模块

还有其他东西,但不是必须的

base/manifests/init.pp:

class base {
  include base::install, base::service, base::config, base::params
}
base/manifests/config.pp

class base::config {
  include base::params

  File {
    require => Class["base::install"],
    ensure => present,
    owner => root,
    group => root,
  }

  file { "/etc/puppet/puppet.conf":
    mode => 0644,
    content => template("base/puppet.conf.erb"),
    require => Class["base::install"],
    nofity => Service["puppet"],
  }
...
base/manifests/params.pp

class base::params {
  $puppetserver = "pup01.sdirect.lab"
}
最后,模板的有趣部分位于base/templates/puppet.conf.erb

...
server=<% puppetserver %>
。。。
服务器=
错误消息:

错误:无法分析模板base/puppet.conf.erb:找不到 “puppetserver”的值位于 /etc/puppet/modules/base/manifests/config.pp:13在节点上

我不明白问题出在哪里。这部分我是直接从专业木偶书中抄来的


有人能告诉我$puppetserver应该在哪里定义以及如何定义吗?

问题是,名称“puppetserver”需要完全限定,以便Puppet可以找到值,因为它是在与模板求值的范围不同的范围中定义的

该变量在
base::params
中定义,因此在该范围内只能简单地称为“puppetserver”。当您从
base::config
中评估模板时,您处于不同的范围内,因此不能仅通过变量的短名称来引用该变量。“include”将另一个类添加到目录中,但不会更改这些规则

这意味着要访问它,您可以使用类名来完全限定它:
base::params::puppetserver
。如果您在清单本身中使用它,这将是
$base::params::puppetserver
。您将在Pro Puppet中的
ssh::config
ssh::service
类中看到类似的示例,其中它在params类中引用了“ssh\u service\u name”(第43-45页)

要访问模板中的变量,它有点不同,请使用
scope.lookupvar(“base::params::puppetserver”)
。以完整示例为例,在模板中添加缺少的等号(以输出值):

...
server=<%= scope.lookupvar("base::params::puppetserver") %>
。。。
服务器=
页面上有更多关于范围的信息

(编辑:看起来它也使用相同的解决方案列在了上。)

在技术上是正确的,但会产生非常详细的模板

您可以通过将其他类的变量值引入自己的类范围来缩短它们:

class base::config {
  include base::params
  $puppetserver = $base::params::puppetserver
  ...
}
然后按预期在模板中使用它们:

server=<% puppetserver %>
服务器=

您还可以使用继承:

class puppet::config inherits puppet::params {
....

这样,您就不必在这个类中再次定义
$puppetserver

非常感谢。scope.lookupvar工作得很好。我必须在base::config中的初始类定义之后添加“include base::params”。
class puppet::config inherits puppet::params {
....