Reputation: 1
I am using a recipe to edit a file. If I want to edit a file I am using ruby_block resource in the recipe where the values are defined in attributes. Below is my sample recipe.
ruby_block 'edit conf' do
block do
rc = Chef::Util::FileEdit.new("#{node['Installation']['file1']}")
rc.search_file_replace_line("Dir","Dir=#{node['Installation']['home']}")
rc.search_file_replace_line("max","max=#{node['Installation']['log_home']}")
rc.write_file
end
end
This will search and replace certain values where the values are defined in attributes. In future if the values are changed, then I have to edit the values in attribute file. Is there any way I don't touch my recipe or attribute file and replace the values. It should be like I refer a vfile which contains sets of values which needs to be changed. Attributes or recipes picks the values from that file and replace it. So that I change the values in file anytime and recipes and attributes are not changes. Or any other thoughts.
Appreciate your help !!!
Upvotes: 0
Views: 800
Reputation: 5738
I'm not sure I understand your use case correctly, but it looks like you could use Data Bags for that.
For example, you can create the following data bag (installation.json
):
{
'id': 'installation',
'home': '/installpath',
'log_home': 'whatever'
}
Then upload it to the Chef Server:
$ knife data bag create myapp
$ knife data bag from file myapp installation.json
Now you can read this information from the recipe:
conf = data_bag_item('admins', 'installation')
ruby_block 'edit conf' do
block do
rc = Chef::Util::FileEdit.new("#{node['Installation']['file1']}")
rc.search_file_replace_line("Dir","Dir=#{conf['home']}")
rc.search_file_replace_line("max","max=#{conf['log_home']}")
rc.write_file
end
end
Hereinafter, when you want to change these values you can do the following:
$ knife data bag edit myapp installation
Note that those values will be the same for every server if everyone reads the same data bag.
Upvotes: 1
Reputation: 15784
You're heading toward a bad pattern (and problems in mid-long term) by no willing to update your cookbook attribute file and bumping the cookbook version.
You're loosing the value of versionning the cookbooks and the ability to revert if there's a problem somewhere.
Moreover it's highly discouraged to edit files, prefer managing the whole file with a template resource.
Upvotes: 2