[chef] Re: Re: Re: more of a ruby problem


Chronological Thread 
  • From: Torben Knerr < >
  • To:
  • Subject: [chef] Re: Re: Re: more of a ruby problem
  • Date: Wed, 30 Oct 2013 07:31:46 +0100

Hi Manoj,

There's a '\n' in your 'host', you have to 'host.chomp' that away.

Your first example should have worked already, but you compared against a String value rather than the host var:
'line.include? "host"' vs 'line.include? host'

Cheers, Torben

On Oct 30, 2013 7:18 AM, "Manoj Thakkar" < "> > wrote:
sorry Daniel,

Here is the output with Chef log as you advised , as you in logs in bold below it did match but it did not go in the if loop.



hostc=Mixlib::ShellOut.new("hostname -s")
hostc.run_command
host=hostc.stdout

hostp=`hostname -s`
Chef::Log.info " host name is #{hostp} "

puts " hostname is #{host}"


File.open("/local_app/account-rest.properties", "r").each_line do |line|
Chef::Log.info "#{host} Account Rest line: #{line}"
  if line.include?(host)
    Chef::Log.info "Found host #{host} in #{line}"
    account_value = line.split(':').last
    Chef::Log.info "Found account value #{account_value}"
end
end


Logs :: 

Recipe: base_tomcat::default.new
  * ruby_block[acc-deploy-host] action run[2013-10-29T23:15:45-07:00] INFO: Processing ruby_block[acc-deploy-host] action run (base_tomcat::default.new line 9)
[2013-10-29T23:15:45-07:00] INFO:  host name is pdevecsas300
 
 hostname is pdevecsas300
[2013-10-29T23:15:45-07:00] INFO: pdevecsas300
 Account Rest line: pbldejksu300:acc1,acc2

[2013-10-29T23:15:45-07:00] INFO: pdevecsas300
 Account Rest line: pdevecsas300:acc1,acc2




On Tue, Oct 29, 2013 at 11:13 PM, Manoj Thakkar < " target="_blank"> > wrote:
Thanks Daniel,

but i always get the value of hostname when i run my chef client( even the MixLIb shell out did not fix the issue ) , also i want only the hostname not the fullyqualified hostname so unfortunately i can not use fqdn :( 



hostc=Mixlib::ShellOut.new("hostname -s")
hostc.run_command
host=hostc.stdout

hostp=`hostname -s`
Chef::Log.info " host name is #{hostp} "

puts " hostname is #{host}"


File.open("/local_app/account-rest.properties", "r").each_line do |line|
  puts line
  puts host
   line.chomp
if line.include?(host)

puts " found host #{line}"

logs below ::

Recipe: base_tomcat::default.new
  * ruby_block[acc-deploy-host] action run[2013-10-29T23:09:36-07:00] INFO: Processing ruby_block[acc-deploy-host] action run (base_tomcat::default.new line 9)
[2013-10-29T23:09:36-07:00] INFO:  host name is pdevecsas300
 
 hostname is pdevecsas300
pbldejksu300:acc1,acc2
pdevecsas300
pdevecsas300:acc1,acc2
pdevecsas300


On Tue, Oct 29, 2013 at 11:04 PM, Daniel Condomitti < " target="_blank"> > wrote:
Sean brought this up in your previous thread but it’s most likely due to the back ticks being evaluated during the compile phase versus the run phase. Use Chef::Log.info to output debugging information and avoid shelling out in favor of Mixlib::ShellOut. Also, you can get the node hostname through node[:fqdn].

host = node[:fqdn]
Chef::Log.info "Hostname is #{node[:fqdn]}"

File.open("/local_app/account-rest.properties", "r").each_line do |line|
  Chef::Log.debug "#{host} Account Rest line: #{line}"
  if line.include?(host)
    Chef::Log.info "Found host #{host} in #{line}"
    account_value = line.split(':').last
    Chef::Log.info "Found account value #{account_value}"
  end
end

On Tuesday, October 29, 2013 at 10:51 PM, Manoj Thakkar wrote:

Hi All,

i have written this code but it never goes in if condition , i ma not sure why , 
if i replace the host with the real value of the hostname -s it does work , i am not sure what the issue is ,
might be a syntax error but Ruby does not complain about it , it silently ignores the if condition

please advise.


host=`hostname -s`

puts " hostname is #{host}"


File.open("/local_app/account-rest.properties", "r").each_line do |line|
  puts line
  puts host
   line.chomp
if line.include? "host"

puts " found host #{line}"

value = line.split(":").last
puts " found account value  #{value}"
puts value

end
end






Archive powered by MHonArc 2.6.16.

§