在嵌套的字符串引号内插入和连接Puppet变量
我正在为服务创建一个Puppet配置文件。 我想在行中添加主机名作为变量。但是,由于行中嵌套了引号(在嵌套的字符串引号内插入和连接Puppet变量,puppet,Puppet,我正在为服务创建一个Puppet配置文件。 我想在行中添加主机名作为变量。但是,由于行中嵌套了引号(“),因此它得到了一个错误 $hostlocal = "${hostname}" file {'puppet_facts_example': ensure => file, path => '/tmp/test.txt', content => "modparam("topology_hiding", "th_callid_prefix", "$hostlo
“
),因此它得到了一个错误
$hostlocal = "${hostname}"
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam("topology_hiding", "th_callid_prefix", "$hostlocal_")"
}
如果我只打印$hostlocal
,它会正确显示主机名。
有没有办法在嵌套的字符串引号(“
)中使用Puppet变量
我还尝试使用模板。
在模板中
modparam("topology_hiding", "th_callid_prefix", "<$= @hostlocal %>_")"
在我看来,主机名是一个变量,所以你们不需要双引号,因为你们想得到变量的值 试一试
这里的基本问题是,在执行字符串插值/转义样式引号(
“
)时,需要转义嵌套的字符串引号。此外,您正在尝试将\
连接到$hostlocal
,但Puppet将其解释为变量$hostlocal\
,因此需要大括号来建立连接。您可以通过以下方法解决问题:
$hostlocal = "${hostname}"
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam(\"topology_hiding\", \"th_callid_prefix\", \"${hostlocal}_\")"
}
但是,我们可以在这方面做进一步的改进因为它没有被插值。同样的情况也适用于topology\u hiding
和th\u callid\u prefix
,除了我们必须将它们更改为单文字字符串引号,因为没有任何内容被插值或转义。此外,如果您正在对事实进行Facter 2样式的查找,那么将事实建立为全局va是更好的样式可与$::
进行变量转换
$hostlocal = $::hostname
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam('topology_hiding', 'th_callid_prefix', \"${hostlocal}_\")"
}
最后,请注意,$hostlocal
变量的使用与事实是冗余的,因此可以安全地删除它以提高清晰度和效率。这将产生下面的最佳解决方案
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam('topology_hiding', 'th_callid_prefix', \"${::hostname}_\")"
}
因为hostname是一个facter变量。 它必须被引用为$hostlocal=$::主机名 谢谢
vinodh无论有无双引号,都可以正常工作。问题是内容部分包含多个双引号。如果我只打印$hostlocal,它会显示正确的主机名。工作非常好!感谢您的其他建议。
$hostlocal = $::hostname
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam('topology_hiding', 'th_callid_prefix', \"${hostlocal}_\")"
}
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam('topology_hiding', 'th_callid_prefix', \"${::hostname}_\")"
}