Regex 连接文件行资源中的字符串
我正在尝试将一个字符串连接到木偶清单中,如下所示:Regex 连接文件行资源中的字符串,regex,puppet,interpolation,string-interpolation,Regex,Puppet,Interpolation,String Interpolation,我正在尝试将一个字符串连接到木偶清单中,如下所示: file_line {'Append to /etc/hosts': ensure => present, line => "${networking['ip'] + '\t'}${networking['fqdn'] + '\t'}${networking['hostname']}", match => "${'^#?'+ networking['ip'] + '\s+' + networking['fqdn'
file_line {'Append to /etc/hosts':
ensure => present,
line => "${networking['ip'] + '\t'}${networking['fqdn'] + '\t'}${networking['hostname']}",
match => "${'^#?'+ networking['ip'] + '\s+' + networking['fqdn'] + '\s+' + networking['hostname']}",
path => '/etc/hosts'
}
我要么得到语法错误,要么在上述情况下:
无法将该值转换为数字
我猜这意味着它不喜欢加号操作符。
那么如何在match和line属性中插入字符串呢?这里的问题是运算符+仅限于数字类型。它不能与字符串类型一起使用。但是,间距表达式和正则表达式仍然可以正常使用,而无需尝试字符串连接。这些只需要放在变量插值之外。因此:
file_line { 'Append to /etc/hosts':
ensure => present,
line => "${networking['ip']}\t${networking['fqdn']}\t${networking['hostname']}",
match => "^#?${networking['ip']}\s+${networking['fqdn']}\s+${networking['hostname']}",
path => '/etc/hosts'
}
应该解决类型不匹配和+运算符的问题。这里的问题是运算符+仅限于数字类型。它不能与字符串类型一起使用。但是,间距表达式和正则表达式仍然可以正常使用,而无需尝试字符串连接。这些只需要放在变量插值之外。因此:
file_line { 'Append to /etc/hosts':
ensure => present,
line => "${networking['ip']}\t${networking['fqdn']}\t${networking['hostname']}",
match => "^#?${networking['ip']}\s+${networking['fqdn']}\s+${networking['hostname']}",
path => '/etc/hosts'
}
应该可以解决类型不匹配和+运算符的问题。我知道您已经得到了答案,但您可以使用主机管理/etc/hosts使生活更轻松:
我知道您已经得到了答案,但您可以通过使用主机来管理/etc/hosts使生活更轻松: