正确的方法是在puppet file_line after子句中使用正则表达式匹配括号

时间:2017-03-03 21:32:26

标签: regex oracle puppet standard-library

使用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"]
}

这不会导致任何傀儡错误,但会将新条目一直放在文件的末尾,而不是放在应该在的监听器块内。 (对我而言,这表明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正则表达式处理后引用吗?

1 个答案:

答案 0 :(得分:0)

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

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

这将删除您的Puppet警告。

以上解释也回答了您的另外两个问题,但这里有一个快速重复的摘要:

  1. 是;他们可以而且是。
  2. 顺便说一句,puppet-lint也会为你抓住这个:https://github.com/rodjek/puppet-lint