如果一个variables是双引号,并且包含一个\n
$test="hello\nworld" file { '/tmp/hello': content => $test }
然后创build一个新行:
/tmp/hello hello world
问题
如果由于hierainput或regsubst结果导致input不是双引号,该怎么办:
$test2=hiera("hiera::input") file { '/tmp/hello': content => $test2 }
结果是:
hello\nworld
试图解决这个问题
假设是双引号内容variables将解决问题。 无论是:
file { '/tmp/hello': content => "$test2" }
也不:
file { '/tmp/hello': content => "\"$test2\"" }
解决了这个问题。 后者导致:
"hello\nworld"
第二次尝试
在StackOverflow上阅读此答案后,尝试了另一个尝试。
hiera.yaml
--- bla: haha\nblabla
清单文件
$test=hiera('bla') $quoted = regsubst($test, '(.*)', '"\1"') file { '/tmp/hello': content => $quoted }
结果是:
"haha\nblabla"
我们正在使用Hiera的YAML后端,但我相信JSON和其他人也是一样。
你可以用YAML格式化你想要的string,Puppet会高兴地接受它。
例如:
--- hiera::input: | my string with newlines preserved
注意| 在hiera::input密钥名称之后。 它告诉YAMLparsing器把下面的值作为一个块,保留换行符和其他缩进。
有关详细信息,请参阅YAML规范的第2.3和8.1.2节。