Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
202 views
in Technique[技术] by (71.8m points)

How can I change a file with Chef?

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.password= sfsfs
alfresco.password=sfgsf

alfresco.rutaAnexos=/gthtfdh/dfgdf/cm:

#atributo.type.anexo=ANEXO_INFO_OBJETO
atributo.type.anexo=AN
atributo.type.observaciones=OBSERVACIONES

I need to comment some lines and uncomment some other lines. Then I need to make seven templates and put variables depending on the environments and create a file in the recipe.

How can I do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...