我需要获取用户的主目录。我决定通过解析::getent_passwd 字符串来获取它(这是一个自定义事实构建为
/etc/passwd
内容的串联)
并借助正则表达式提取相关信息。
当我使用固定字符串(“adam
”)测试 ::getent
时,提取有效:
if "$::getent_passwd" =~ /\|adam:x:[^:]+:[^:]+:[^:]*:([^:]*):/ {
$user_home = $1
notify{"This works":}
}
但是当我使用 $user
变量构建正则表达式时,没有任何内容匹配:
if "$::getent_passwd" =~ /\|${user}:x:[^:]+:[^:]+:[^:]*:([^:]*):/ {
$user_home = $1
} else {
fail{"this fails":}
}
客户端和服务器在 Ubuntu 14.04 64 位上使用 Puppet 3.7.3。 Puppetserver 使用 puppetdb。
最佳答案
看来,在 puppet 正则表达式中使用普通变量是不可能的: https://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#regular-expressions https://projects.puppetlabs.com/issues/4155
上面的链接中提出了解决方法:
regex.erb:
<% if /(^|\s+)#{string2}(\s+|$)/.match(string1) then %>yes<% else %>no<% end %>
manifest:
$testre = template('regex.erb')
if $testre == "yes\n" { notify{"success!": } }
关于regex - 如何在 puppet 上使用参数化正则表达式(例如/${user}/)进行测试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27781170/