有条件地将站点特定的configuration片段加载到基本configuration文件模板中

我的最终目标是将特定于站点的configuration片段包含到通用基本模板中。 我的第一个想法是从基本模板中调用模板函数,例如:

performance:

... $domain = "www.example.com" ... 

模板:

 server { # Common configuration items here. ... # Load site-specific configurations here. <%= scope.function_template("sites/$domain/config.erb") %> } 

但是,在函数调用中似乎没有可能使用variables(请纠正我,如果我错了)。 还有一个特定于站点的模板必须存在的问题。

所以,我的下一个想法是在清单中使用一个包含从模板加载的站点特定configuration内容的variables。

performance:

 ... $domain = "www.example.com" $site_specific_content = template("sites/$domain/config.erb") .... 

模板:

 server { # Common configuration items here. ... # Load site-specific configurations here. <%= site_specific_content %> } 

但是,如果模板不存在,我还找不到方法来呈现模板,但是如果模板不存在,则将该variables设置为空string。 (虽然有点不雅,我想这里的解决方法可能是为网站创build一个空的站点特定的模板,不需要额外的configuration。)

有任何想法吗?

但是,在函数调用中似乎没有可能使用variables(请纠正我,如果我错了)。 还有一个特定于站点的模板必须存在的问题。

可以在函数调用中使用一个variables,但要记住你的函数参数是一个rubystring,而不是一个Puppet模板。 你可以这样做:

 <%= scope.function_template("sites/" + domain + "/config.erb") %> 

或者使用Ruby的stringreplace:

 <%= scope.function_template("sites/#{domain}/config.erb") %> 

…但是如果定义的模板不存在,这仍然会产生一个错误。

为了避免这个错误,你可以在你的模板中使用Ruby的exception处理机制,如下所示:

 <%= begin scope.function_template("sites/#{domain}/config.erb") rescue end %> 

达达! 这样做可能有更好的方法; 我既不是ruby也不是木偶专家,我刚刚做了这个。