Puppet内联模板与puppet://在URL中

时间:2015-06-11 14:46:02

标签: puppet

在我的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/,因此,它将无效。

我该如何做到这一点?

2 个答案:

答案 0 :(得分:2)

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"
  )
}

答案 1 :(得分:1)

至于为什么原始方法不起作用:URL通常仅与source属性一起使用,并按原样传输给代理。代理使用URL并向Puppet文件服务器(这只是另一个主服务器)发出相应请求。

另一方面,file函数(此处与content属性一起使用,与inline_template一起使用)将不会处理URL并希望改为使用本地路径。

所有这一切,通过指定测试框和生产系统的路径,问题可能是侧面步骤,后者只是承认/home/carlos缺失。但这远不是一个干净的解决方案。