ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
I have very limited knowledge of the Ruby programming language, however, I need to implement on Chef something similar to "augeas" (which works with Puppet, but here I need a solution for Chef).
I got the example code below but it's not working and I am now for a few days trying to figure out what is wrong.
Basically I need to be able to select specific strings in a text file and modify these values. I could use sed but perhaps I can do it in a more elegant way using the ruby_block from Chef.
Please let me know what can be possibly wrong with the code below. Why is my /etc/hosts not being updated with new values?
Thanks for your help.
Follows my default.rb file:
Code:
ruby_block "edit etc hosts" do
block do
rc = Chef::Util::FileEdit.new("/etc/hosts")
rc.search_file_replace_line(
/^127\.0\.0\.1 localhost$/,
"127.0.0.1 #{new_fqdn} #{new_hostname} localhost"
)
rc.write_file
end
end
Which made me wonder if your reference to Chef::... was valid in this context?
Do you suggest that in the same default.rb file I cannot use chef code, even outside of the ruby_block function? Just in case, I have tried to comment the whole default.rb file, leaving ONLY the ruby_block function alone, but the problem persist.
hmmm ... seems this is an added module via gems hence Chef is part of your Ruby installation Hence you are using ruby code only inside the ruby_block.
I have only looked over a few examples, but the default.rb seems to be something that sets up the recipe where the task you have set seems to be more of something the recipe would do
after being setup (Could be completely off here btw)
Sorry I can't be of more help as again I am not familiar enough with Chef's workings
Sorry I can't be of more help as again I am not familiar enough with Chef's workings
Chef is a BEAST, they manage to make it even more complicated than Puppet. Configuration Management rocks and I personally like bcfg2 but where I work today we need Chef.
Anyway, thanks for your help, let's wait, perhaps some ruby/chef hacker may appear sooner or later.
Sorry ... no ... will need to wait for a Chef person
I mean the error makes sense to me but we do not call 'chef' in lower case in this code ... which to me would point to the error being elsewhere??
Maybe there are other files you have edited ... or not edited?
Here I understand it is non-sense to run such code in a cron recipe but it is just for testing purposes. /var/chef/cookbooks/cron/recipes/default.rb:
Quote:
ruby_block "edit etc hosts" do
require 'chef/util/file_edit'
block do
rc = Chef::Util::FileEdit.new("/etc/hosts")
rc.search_file_replace_line(
/^127\.0\.0\.1 localhost/,
"127.0.0.1 #{chef.hostname.local} #{hostname} localhost"
)
rc.write_file
end
end
The following line deserves a brief explation. Here I have setup a local webserver on /root/ and therefore chef-solo can read the "/etc/chef/solo.rb" file and can call for node.json and other files. In the future we plan to use a dedicated webserver but at the moment it is ok to keep it local for testing purposes. /root/node.json:
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.