Puppet中的字符串替换?

ric*_*ler 23 ruby puppet

是否可以使用正则表达式在Puppet中进行字符串替换/转换?

如果$ hostname是"web1",我希望$ hostname_without_number为"web".以下是无效的Puppet语法,但我想我需要这样的东西:

$hostname_without_number = $hostname.gsub(/\d+$/, '')
Run Code Online (Sandbox Code Playgroud)

fre*_*eit 39

对的,这是可能的.

检查puppet函数参考:http://docs.puppetlabs.com/references/2.7.3/function.html

内置了正则表达式替换函数.它可能调用相同的底层gsub函数.

$hostname_without_number = regsubst($hostname, '\d+$', '')
Run Code Online (Sandbox Code Playgroud)

或者,如果您希望实际调用Ruby,则可以使用内联ERB模板:

$hostname_without_number = inline_template('<%= hostname.gsub(/\d+$/, "") %>')
Run Code Online (Sandbox Code Playgroud)