【问题标题】:Edit file using chef使用厨师编辑文件
【发布时间】:2017-12-06 04:31:53
【问题描述】:

我知道有很多答案和已解决的答案,但似乎没有什么对我有用,我想做的是在 tomcat 安装完成后添加几行代码,并在 /var/ 中安装lib/tomcat8/conf/catalina.properties 文件。

这是我迄今为止尝试过的简单的 ruby​​ 方法以及 Chef utils 的方法。

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

我尝试将 puts “测试”,但似乎厨师甚至没有进入代码。

【问题讨论】:

    标签: ruby chef-infra chef-recipe chef-solo cookbook


    【解决方案1】:

    查看 poise-fileline 食谱,了解如何更安全地执行此操作的示例。

    我们总体上不推荐这种方法,因为它会创建非常脆弱的代码,而且通常是不收敛的(正如您所注意到的)。更好的方法是使用template资源以收敛的方式控制整个文件。

    【讨论】:

      【解决方案2】:

      这种逻辑存在一个根本缺陷,因为 Chef-client 会在每次运行时附加这些行,无论它们是否已经在文件中。我也不认为 Chef 真的很喜欢这种方法。

      如果您不想将整个文件作为模板进行管理,那么您应该使用sous-chef/line-cookbook,因为它内置了条件/保护以防止出现此问题。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-01-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2017-03-15
        相关资源
        最近更新 更多