在嵌套字符串引号内插入和连接 Puppet 变量
Interpolate and concatenate Puppet variable inside nested string quotes
我正在为服务创建 Puppet 配置文件。
我想在行中添加主机名作为变量。但是,由于行中的嵌套引号 ("
) 而出错。
$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 %>_")"
但是结果没有任何价值。
modparam("topology_hiding", "th_callid_prefix", "_")"
在我看来主机名是一个变量所以你不需要双引号因为你想得到变量的值。
尝试
$hostlocal = $hostname
这里的基本问题是当你在做字符串 interpolation/escape 样式引号 ("
) 时需要转义嵌套字符串引号。此外,您正试图将 _
连接到 $hostlocal
,但 Puppet 将其解释为变量 $hostlocal_
,因此您需要花括号来建立连接。您可以通过以下方式解决您的问题:
$hostlocal = "${hostname}"
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam(\"topology_hiding\", \"th_callid_prefix\", \"${hostlocal}_\")"
}
不过,我们可以进一步改进。我们在这里可以做的第一件事是删除 ${hostname}
周围的引号,因为它没有被插值。 topology_hiding
和 th_callid_prefix
也是如此,除了我们必须将它们更改为单个文字字符串引号,因为没有任何内容被插入或转义。此外,如果您正在对事实进行 Facter 2 样式查找,那么最好将事实建立为具有 $::
.
的全局变量
$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}_\")"
}
因为主机名是一个因子变量。
它必须被引用为 $hostlocal = $::hostname
谢谢
维诺
我正在为服务创建 Puppet 配置文件。
我想在行中添加主机名作为变量。但是,由于行中的嵌套引号 ("
) 而出错。
$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 %>_")"
但是结果没有任何价值。
modparam("topology_hiding", "th_callid_prefix", "_")"
在我看来主机名是一个变量所以你不需要双引号因为你想得到变量的值。
尝试
$hostlocal = $hostname
这里的基本问题是当你在做字符串 interpolation/escape 样式引号 ("
) 时需要转义嵌套字符串引号。此外,您正试图将 _
连接到 $hostlocal
,但 Puppet 将其解释为变量 $hostlocal_
,因此您需要花括号来建立连接。您可以通过以下方式解决您的问题:
$hostlocal = "${hostname}"
file {'puppet_facts_example':
ensure => file,
path => '/tmp/test.txt',
content => "modparam(\"topology_hiding\", \"th_callid_prefix\", \"${hostlocal}_\")"
}
不过,我们可以进一步改进。我们在这里可以做的第一件事是删除 ${hostname}
周围的引号,因为它没有被插值。 topology_hiding
和 th_callid_prefix
也是如此,除了我们必须将它们更改为单个文字字符串引号,因为没有任何内容被插入或转义。此外,如果您正在对事实进行 Facter 2 样式查找,那么最好将事实建立为具有 $::
.
$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}_\")"
}
因为主机名是一个因子变量。 它必须被引用为 $hostlocal = $::hostname
谢谢 维诺