Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/visual-studio-2008/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
puppet,根据主机名值或唯一随机值设置模板变量_Puppet - Fatal编程技术网

puppet,根据主机名值或唯一随机值设置模板变量

puppet,根据主机名值或唯一随机值设置模板变量,puppet,Puppet,我想在多台linux机器上部署一个配置文件,并需要确保该配置文件中的某个变量对每台机器都有唯一的值。到目前为止,我一直以以下方式发送相同的配置文件: file { "/etc/nxserver/node.conf": mode => 644, ensure => file, source => "puppet:///modules/nxserver/nxserver-node.conf"; } # This file is managed by

我想在多台linux机器上部署一个配置文件,并需要确保该配置文件中的某个变量对每台机器都有唯一的值。到目前为止,我一直以以下方式发送相同的配置文件:

file {
    "/etc/nxserver/node.conf":
    mode => 644,
    ensure => file,
    source => "puppet:///modules/nxserver/nxserver-node.conf";
}
# This file is managed by puppet
... random config stuff ...
somevariable = <%= hostname %>
我假设切换到模板就像用内容替换源一样简单:

在nxserver-node.erb文件中,我可以通过以下方式设置感兴趣的变量:

file {
    "/etc/nxserver/node.conf":
    mode => 644,
    ensure => file,
    source => "puppet:///modules/nxserver/nxserver-node.conf";
}
# This file is managed by puppet
... random config stuff ...
somevariable = <%= hostname %>

我尝试使用+和add来添加偏移量,但+似乎是为字符串保留的,添加函数未知。

以下内容正是我所需要的:

<% Puppet::Parser::Functions.function('fqdn_rand') -%>
<% offset = "1000".to_i -%>
<% value = scope.function_fqdn_rand(['10000']).to_i + offset -%>
somevariable=<%= value.to_s %>

在ERB模板中,您可以做Ruby所能做的一切。这些方法特别有用

例如,要从主机名中删除前缀,可以使用

somevariable = <%= @hostname.sub(/^desktop-/, '') %>

这不是说somevariable=的一种精心设计的方式吗?是的,但您将得到以下错误详细信息:无法将Fixnum转换为字符串。上面的解决方案是有效的。对了,忘了你的期末考试。第一行应该可以安全地跳过。不幸的是,这还不够。+1000实际上是问题的根源,我猜ruby将+解释为必须连接两个字符串。因此,偏移量和值都需要显式地转换为带有to_i的整数。只有到了最后,你才做了一件事。