如果一个变量是双引号并且它包含一个 \n
$test="hello\nworld"
file { '/tmp/hello':
content => $test
}
Run Code Online (Sandbox Code Playgroud)
然后创建一个新行:
/tmp/hello
hello
world
Run Code Online (Sandbox Code Playgroud)
问题
如果由于 hiera 输入或 regsubst 结果输入不是双引号怎么办:
$test2=hiera("hiera::input")
file { '/tmp/hello':
content => $test2
}
Run Code Online (Sandbox Code Playgroud)
结果是:
hello\nworld
Run Code Online (Sandbox Code Playgroud)
尝试解决问题
假设是双引号内容变量将解决问题。两者都不:
file { '/tmp/hello':
content => "$test2"
}
Run Code Online (Sandbox Code Playgroud)
也不:
file { '/tmp/hello':
content => "\"$test2\""
}
Run Code Online (Sandbox Code Playgroud)
解决了这个问题。后者导致:
"hello\nworld"
Run Code Online (Sandbox Code Playgroud)
第二次尝试
在 StackOverflow 上阅读此答案后尝试了另一种尝试。
层级文件
---
bla: haha\nblabla
Run Code Online (Sandbox Code Playgroud)
清单文件
$test=hiera('bla')
$quoted = regsubst($test, '(.*)', '"\1"')
file { '/tmp/hello':
content => $quoted
}
Run Code Online (Sandbox Code Playgroud)
结果是:
"haha\nblabla"
Run Code Online (Sandbox Code Playgroud)
我们正在使用 Hiera 的 YAML 后端,但我相信 JSON 和其他后端也是如此。
你可以在 YAML 中按照你想要的方式格式化你的字符串,Puppet 会很乐意接受它。
例如:
---
hiera::input: |
my string
with newlines
preserved
Run Code Online (Sandbox Code Playgroud)
注意键名|
后面的hiera::input
。它告诉 YAML 解析器将以下值视为一个块,保留换行符和任何其他缩进。
有关详细信息,请参阅YAML 规范的第2.3和8.1.2节。
归档时间: |
|
查看次数: |
5607 次 |
最近记录: |