在 URL 中使用 puppet:// 的 Puppet 内联模板

Puppet inline template with puppet:// in URL

在我的 Puppet 模块中,我有这样的东西:

file {'myfile':
  ensure => 'file',
  path => '/whatever/myfile',
  content =>  inline_template(file(
    "puppet:///modules/my_module/templates/${domain}/${::hostname}_myfile.erb",
    "puppet:///modules/my_module/templates/${domain}/myfile.erb"
  ))
}

我的规格如下:

require 'spec_helper'

describe 'my_module' do
  context 'with defaults for all parameters' do
    it { should compile }
  end
end

如果尝试 运行 它,我得到:

Failure/Error: it { should compile }
  error during compilation: Evaluation Error: Error while evaluating a Function Call, Could not find any files from puppet:///modules/my_module/templates/dev.contaazul.local/myfile.erb, puppet:///modules/my_module/templates/dev.contaazul.local/myfile.erb at /home/carlos/Code/Puppet/modules/my_module/spec/fixtures/modules/my_module/manifests/init.pp:48:33 on node becker.local

显然,它找不到任何 ERB 模板。如果我将 puppet:// 部分替换为 /home/carlos/Code/Puppet/(代码实际所在的位置),它会通过,但在生产中它是 /etc/puppet/,因此,它将不起作用。

我怎样才能完成这项工作?

RI Pienaar 已针对具有此行为的函数发布了 code snippet。您需要将此代码复制到路径 lib/puppet/parser/functions/multi_source_template.rb.

中的一个模块中的文件中

完成后,您应该可以像这样使用它:

file {'myfile':
  ensure => 'file',
  path => '/whatever/myfile',
  content =>  multi_source_template(
    "my_module/${domain}/${::hostname}_myfile.erb",
    "my_module/${domain}/myfile.erb"
  )
}

至于为什么原来的方法不行:URLs通常只和source一起使用属性并转移按原样发送给代理。代理使用 URL 并向 Puppet 文件服务器(只是另一个主服务器)发出相应请求。

另一方面,file 函数(此处与 content 属性 结合 inline_template 使用)不会处理 URLs并期望使用本地路径。

综上所述,通过指定测试盒和生产系统的路径可能可以回避这个问题,后者只是承认缺少 /home/carlos。但这远不是一个干净的解决方案。