LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 11-06-2013, 06:58 AM   #1
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Rep: Reputation: 31
Ruby code on Chef as a "ruby_block" not working


Hi LQ community,

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

Last edited by sparc86; 11-06-2013 at 07:13 AM.
 
Old 11-06-2013, 08:32 AM   #2
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Is this example something you have cobbled from an example someone has shown you?

I am not overly familiar with Chef but after a quick search I found the following:
Quote:
As soon as you wrap your code in ruby_block you cannot use ordinary recipe resource declaration anymore.
Which made me wonder if your reference to Chef::... was valid in this context?

Sorry if this is completely on the wrong path
 
Old 11-06-2013, 09:57 AM   #3
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
Quote:
Is this example something you have cobbled from an example someone has shown you?
Yes, I got it from here: http://sysadvent.blogspot.de/2012/12...ow.html#item_2



Quote:
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.

Last edited by sparc86; 11-06-2013 at 10:09 AM.
 
Old 11-06-2013, 10:08 AM   #4
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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
 
Old 11-06-2013, 10:16 AM   #5
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
Quote:
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.

Last edited by sparc86; 11-08-2013 at 07:41 AM.
 
Old 11-07-2013, 01:46 AM   #6
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well I do know Ruby

Things to consider:

1. Does your /etc/hosts have the line exactly as the regex looking for it?
Code:
/^127\.0\.0\.1 localhost$/
So line starts with 127 and there is only a single space (not tab) between last 1 and localhost and there is no white space after localhost

2. #{new_fqdn} #{new_hostname} - these are variable calls so obviously you need to have already set these somewhere else

See if that helps a bit
 
Old 11-07-2013, 02:25 AM   #7
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
I have tried to change the strings to just 127.0.0.1 and it is not working.

After I run chef-solo, I get the following error:

Quote:
NoMethodError
-------------
undefined method `chef' for Chef::Resource::RubyBlock
Thanks for your help, I really appreciate it.

Last edited by sparc86; 11-07-2013 at 02:58 AM.
 
Old 11-07-2013, 06:57 AM   #8
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well silly question but you do have the Chef gem installed?
 
Old 11-07-2013, 09:23 AM   #9
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
I think so.

Quote:
[root@chef recipes]# whereis gem
gem: /usr/bin/gem

is there any other test I could make in order to verify for libraries or so?
 
Old 11-08-2013, 09:19 AM   #10
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Try:
Code:
$ gem list
$ is my prompt
 
Old 11-08-2013, 09:27 AM   #11
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
Quote:
[root@chef recipes]# gem list

*** LOCAL GEMS ***

chef (11.6.2)
erubis (2.7.0)
highline (1.6.20)
ipaddress (0.8.0)
json (1.7.7)
mime-types (1.25)
mixlib-authentication (1.3.0)
mixlib-cli (1.3.0)
mixlib-config (1.1.2)
mixlib-log (1.6.0)
mixlib-shellout (1.2.0)
net-ssh (2.7.0)
net-ssh-gateway (1.2.0)
net-ssh-multi (1.1)
ohai (6.18.0)
rest-client (1.6.7)
systemu (2.5.2)
yajl-ruby (1.1.0)
[root@chef recipes]#
Any suggestions?

Thanks!
 
Old 11-08-2013, 10:06 AM   #12
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
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?
 
Old 11-11-2013, 03:10 AM   #13
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
Ok, perhaps it would be a good idea if I copy you here all the relevant config files.

/etc/chef/solo.rb:
Quote:
file_cache_path "/var/chef"
cookbook_path "/var/chef/cookbooks"
json_attribs "http://127.0.0.1/node.json"
recipe_url "http://127.0.0.1/chef-solo.tar.gz"


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:
Quote:
{
"run_list": [ "recipe[helloworld]", "recipe[crondemo::remove]", "recipe[ntp]", "recipe[cron]", "recipe[umask]", "recipe[rsync]", "recipe[sudo_env]", "recipe[permissions]" ]
}

Overall, do you see anything potentially wrong?

Last edited by sparc86; 11-11-2013 at 03:12 AM.
 
Old 11-11-2013, 06:33 AM   #14
grail
LQ Guru
 
Registered: Sep 2009
Location: Perth
Distribution: Manjaro
Posts: 10,011

Rep: Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194Reputation: 3194
Well only the obvious question comes to me, does the default recipe need to be in the run_list or is it assumed if present it is always executed?

Note: To save your formatting here use 'code' instead of 'quote' tags
 
Old 11-11-2013, 06:49 AM   #15
sparc86
Member
 
Registered: Jul 2006
Location: Joinville, Brazil
Distribution: Debian, CentOS
Posts: 301

Original Poster
Rep: Reputation: 31
Yes, it must be declared in the run_list, which is the case indeed, as the config being used for testing is the cron recipe.

Thanks for the tip about the CODE tags

So, any thoughts?
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
Find me source code for "iwlist" & "iwconfig" Angela Wu Linux - Kernel 2 09-15-2013 08:44 PM
[SOLVED] "/etc/init.d/apache2" start VS "apache2ctl start" with passenger module (ruby/rails) arond Linux - Server 1 12-13-2010 10:56 AM
[SOLVED] Python "QFileDialog" code not working gary_in_springhill Programming 1 02-03-2010 08:48 AM
net working eth0 eth1 wlan0 "no connection" "no LAN" "no wi-fi" Cayitano Linux - Newbie 5 12-09-2007 07:11 PM
Incorrect PHP code is working, shoud I "fix" it? rblampain Programming 6 04-28-2006 10:33 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 10:34 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration