【问题标题】:Correct way to regex match parenthesis in puppet file_line after clausepuppet file_line after 子句中正则表达式匹配括号的正确方法
【发布时间】:2017-07-24 03:33:11
【问题描述】:

使用puppet修改listener.ora文件:

file_line { 'addFloatingListenerTCPS':
  ensure  => present,
  path    => "${LSNR_PATH}/listener.ora",
  line    => "      (ADDRESS = (PROTOCOL = TCPS)(HOST = ${FLOATING_IP})(PORT = 1522))",
  after   => "^\s+(ADDRESS = (PROTOCOL = TCPS)(HOST = DB)(PORT = 1522))",
  require => Class["othernode"]
}

这不会导致任何 puppet 错误,但会将新条目一直放在文件的末尾,而不是放在侦听器块中应有的位置。 (对我来说,这表明 after 属性值与文件内容不匹配,因此 puppet 默认为附加模式)。

更改after 属性值以转义所有括号:

after   => "^\s+\(ADDRESS = \(PROTOCOL = TCPS\)\(HOST =  DB\)\(PORT = 1522\)\)",

在控制台中生成多个警告:

Warning: Unrecognised escape sequence '\(' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\(' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\)' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\(' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\)' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\(' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\)' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30
Warning: Unrecognised escape sequence '\)' in file /etc/puppet/modules/test_conf/manifests/init.pp at line 30

使其无法在卫星中使用。但是,执行完成后,新条目就在它应该在的块内。

这引发了多个问题:

  1. 括号是否被解释为特殊字符,如果是,为什么不能转义?
  2. puppet stdlib 正则表达式是否处理反向引用?

【问题讨论】:

  • 是否有效:"^\\s+\\(ADDRESS = \\(PROTOCOL = TCPS\\)\\(HOST = DB\\)\\(PORT = 1522\\)\\)"

标签: regex oracle puppet standard-library


【解决方案1】:

是的,您确实需要在 file_line 资源的 after 属性中转义正则表达式的括号内。正则表达式中使用括号来捕获表达式的一部分以供以后变量使用。但是,出现警告是因为您的正则表达式字符串有双引号 (")。这导致 Puppet 解析器最初将 \ 解释为插值字符串转义而不是正则表达式转义。因此,您需要将您的正则表达式值更改为文字字符串,以便解析器正确解释它。

after   => '^\s+\(ADDRESS = \(PROTOCOL = TCPS\)\(HOST =  DB\)\(PORT = 1522\)\)',

这将删除您的 Puppet 警告。

上面的解释也回答了你的另外两个问题,但这里是一个快速重复的总结:

  1. 是的;他们可以而且是。
  2. 是的

顺便说一句,puppet-lint 也会为你捕捉到这个:https://github.com/rodjek/puppet-lint

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-18
    • 1970-01-01
    • 1970-01-01
    • 2022-06-21
    • 1970-01-01
    • 2011-08-03
    相关资源
    最近更新 更多