簡體   English   中英

使用廚師編輯文件

[英]Edit file using chef

我知道有很多答案加上解決的問題,但似乎沒有什么對我有用,我想要做的是在tomcat完成之后我想在/ var / lib / tomcat8中安裝幾行代碼/conf/catalina.properties文件。

這是我到目前為止嘗試過的簡單的紅寶石方式和廚師工具。

File.open("/var/lib/tomcat8/conf/catalina.properties", "a+") do |f|
  f << "\n"
  f << "javax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"
end


ruby_block "insert_line" do
  block do
    file = Chef::Util::FileEdit.new("/var/lib/tomcat8/conf/catalina.properties")
    file.insert_line_if_no_match("/javax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl/", "javax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl")
    file.write_file
  end
end



file '/var/lib/tomcat8/conf/catalina.properties' do
   action :create
   owner 'root'
   group 'root'
   content File.open("/var/lib/tomcat8/conf/catalina.properties", "a+") do |f|
      f << "\n"
      f << "javax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"
    end
end



file '/var/lib/tomcat8/conf/catalina.properties' do
   action :create
   owner 'root'
   group 'root'
   content File.open("/var/lib/tomcat8/conf/catalina.properties", "a+") do |f|
      f << "\n"
      f << "javax.xml.parsers.DocumentBuilderFactory=com.sun.org.apache.xerces.internal.jaxp.DocumentBuilderFactoryImpl"
    end
end

我嘗試把put“測試”,似乎廚師甚至沒有進入代碼。

查看poise-fileline cookbooks,了解如何更安全地完成此操作。

我們總體上不推薦這種方法,因為它創建了非常脆弱的代碼,通常是非收斂的(正如您所注意到的)。 更好的方法是使用template資源以收斂的方式控制整個文件。

這個邏輯存在一個根本性缺陷,因為不管每次運行時它們是否已存在於文件中,chef-client都會附加行。 我也不認為廚師真的喜歡這種方法。

如果您不想將整個文件作為模板進行管理,那么您應該使用sous-chef / line-cookbook,因為它內置了條件/警衛以防止出現此問題。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM