How can I change a file with Chef?

后端 未结 4 1049
闹比i
闹比i 2020-11-29 04:24

I have 7 files and 1 war. I need to change values when I deploy them. I have this:

##usuario
#alfresco.user=*****
alfresco.user=********
##pass 
#alfresco.p         


        
相关标签:
4条回答
  • 2020-11-29 04:30

    Chef actually allows and uses this. You can find an example in the opscode's

    cookbooks/chef-server/recipes/default.rb:

    ruby_block "ensure node can resolve API FQDN" do
      block do
        fe = Chef::Util::FileEdit.new("/etc/hosts")
        fe.insert_line_if_no_match(/#{node['chef-server']['api_fqdn']}/,
                                   "127.0.0.1 #{node['chef-server']['api_fqdn']}")
        fe.write_file
      end
      not_if { Resolv.getaddress(node['chef-server']['api_fqdn']) rescue false }
    end
    

    Here's the use-case. After the installation from source I had to uncomment lines in some created configuration file that hadn't been the same in all versions of software, therefore use of templates hadn't been appropriate. The methods I used were:

    • (Object) search_file_replace(regex, replace)
    • (Object) search_file_replace_line(regex, newline)

    You may find the full documentation here:

    • http://rubydoc.info/gems/chef/Chef/Util/FileEdit

    TO STRESS: This method is to be used only when using templates and partials is inappropriate. As @StephenKing already said, templates are common way of doing this.

    0 讨论(0)
  • 2020-11-29 04:50

    Here is an example of how you can use Chef to uncomment a line in a configuration file. The ruby_block is protected with a ::File::grep. The test for Debian is just for fun.

    pam_config = "/etc/pam.d/su"
    commented_limits = /^#\s+(session\s+\w+\s+pam_limits\.so)\b/m
    
    ruby_block "add pam_limits to su" do
      block do
        sed = Chef::Util::FileEdit.new(pam_config)
        sed.search_file_replace(commented_limits, '\1')
        sed.write_file
      end
      only_if { ::File.readlines(pam_config).grep(commented_limits).any? }
    end if platform_family?('debian')
    
    0 讨论(0)
  • 2020-11-29 04:50

    As of 2020, the solutions provided are discouraged - see here and here.

    The modern way of performing file manual editing is to use the official, community-maintained, cookbook line.

    Example:

    replace_or_add "why hello" do
      path "/some/file"
      pattern "Why hello there.*"
      line "Why hello there, you beautiful person, you."
    end
    

    It's crucial, before using such strategy, to be 100% sure that manual editing is really required, but this is strictly dependent on the use case.

    0 讨论(0)
  • 2020-11-29 04:52

    By design, you don't modify files with Chef. Instead you put placeholders (<%= ..%>) into the file templates, which are then replaced with the dynamic values (so called attributes).

    0 讨论(0)
提交回复
热议问题