Forum: Ruby on Rails Ruby code on Chef as a "ruby_block" not working

Aad043792267d0f82abc1ec9c53e7447?d=identicon&s=25 Rodrigo de Campos (Guest)
on 2013-11-07 18:17
(Received via mailing list)
Hi Ruby hackers,

I have very limited knowledge of the Ruby programming language, however,
I need to implement on Chef (check-solo) 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?

Always when I re-run chef-solo, I get the following error:

*NoMethodError**
**-------------**
**undefined method `chef' for Chef::Resource::RubyBlock*


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

--
Rodrigo de Campos
Unix System Administrator
Aa082c8b00a50928e5860dcd70bf2368?d=identicon&s=25 tamouse m. (tamouse_m)
on 2013-11-07 18:28
(Received via mailing list)
You should really ask about this in a chef support area, as it is
something very specific to chef.

As it is, you cannot define local functions in chef recipes, they have
to be defined in libraries and called from such.

Stackoverflow is really your best bet for help on chef things, or the
#chef channel on irc.freenode.net
Please log in before posting. Registration is free and takes only a minute.
Existing account

NEW: Do you have a Google/GoogleMail, Yahoo or Facebook account? No registration required!
Log in with Google account | Log in with Yahoo account | Log in with Facebook account
No account? Register here.