Puppet erb-如何替换包含合法反斜杠的字符串(gsub)?
我在结合Puppet、Hiera和templates时遇到了以下erb问题: 通过Hiera,我获得了以下字符串作为变量: 首先是数组中的变量Puppet erb-如何替换包含合法反斜杠的字符串(gsub)?,puppet,erb,hiera,eruby,Puppet,Erb,Hiera,Eruby,我在结合Puppet、Hiera和templates时遇到了以下erb问题: 通过Hiera,我获得了以下字符串作为变量: 首先是数组中的变量example(数据[示例]) 和变量示例信息 some kind of \1 and maybe also a \2 some kind of \1 and maybe also a \2 现在,我想用第二个字符串替换傀儡模板中的\u变量,其中包含一个合法的反斜杠()。所以我是这样做的: result=data['example'].gsub('_V
example
(数据[示例])
和变量示例信息
some kind of \1 and maybe also a \2
some kind of \1 and maybe also a \2
现在,我想用第二个字符串替换傀儡模板中的\u变量
,其中包含一个合法的反斜杠()。所以我是这样做的:
result=data['example'].gsub('_VARIABLE_', @example_information)
因此,我从数组中取出example
,并用@example\u信息填充占位符
结果如下:
something with some kind of and maybe also a in it
something with some kind of \1 and maybe also a \2 in it
没有反斜杠,因为gsub
将它们解释为反引用。那么,我如何解决我的问题来保留我的反斜杠,而不在Hiera文件中重复转义它们呢?我需要在代码中进一步使用Hiera变量,而不使用双转义反斜杠。我现在这样做是为了解决这个特定问题,如下所示:
something with some kind of and maybe also a in it
something with some kind of \1 and maybe also a \2 in it
变量再次示例
和变量示例信息
some kind of \1 and maybe also a \2
some kind of \1 and maybe also a \2
模板中的代码部分:
# we need to parse out any backslashes
info_temp=example_information.gsub('\\', '__BACKSLASH__')
# now we substitute the variables with real data (but w/o backslashes)
result_temp=data['example'].gsub(/__ITEM_NAME__/, info_temp)
# now we put together the real string with backslashes again as before
result=result_temp.gsub('__BACKSLASH__', '\\')
现在,结果如下所示:
something with some kind of and maybe also a in it
something with some kind of \1 and maybe also a \2 in it
注
也许有更好的方法,但在我的研究中,我没有发现更好的解决方案,所以如果你知道更好的方法,请添加评论