2017-03-03 47 views
-1

用木偶來修改listener.ora文件:正確的方式正則表達式匹配括號中的木偶file_line後第

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. 傀儡stdlib正則表達式處理回參考嗎?
+0

它的工作原理:'「^ \\ S + \\(地址爲\\(PROTOCOL = TCPS \\)\\(HOST = DB \\)\\(PORT = 1522 \\)\\)「' – MohaMad

回答

0

是的,你需要你的file_line資源的after屬性逃生內正則表達式的括號內部。在正則表達式中使用Parantheses來捕獲部分表達式,以供稍後的變量使用。但是,警告是由於您的正則表達式字符串有雙引號(")而引起的。這導致Puppet解析器最初將\解釋爲內插字符串轉義,而不是正則表達式轉義。因此,您需要將您的正則表達式值更改爲解析器正確解釋它的文字字符串。

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

這將刪除您的Puppet警告。

上述解釋回答您的另外兩個問題爲好,但這裏是一個快速重複摘要:

  1. 是;他們可以並且是。

順便說一句,木偶皮棉本來也抓住了這個給你:https://github.com/rodjek/puppet-lint