More of a ruby problem

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

Manoj,

You need to provide more debugging information such as the output and the file contents etc.

On Oct 29, 2013, at 10:51 PM, Manoj Thakkar manoj.thakkar@gmail.com 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

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

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 :frowning:

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
daniel@condomitti.comwrote:

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

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 manoj.thakkar@gmail.comwrote:

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 :frowning:

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 <daniel@condomitti.com

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

If you can’t use node[:fqdn] due to it including the rest of the node’s hostname then you need to call .chomp on the output from ShellOut.run_command:

chef > Mixlib::ShellOut.new('hostname -s').run_command.stdout
=> "disrupt\n”

Otherwise you’ll never match that ‘account-rest.properties’ file since it contains “pdevecsas300:acc1” and not “pdevecsas300\n:acc1”. I’d really recommend leveraging as much of chef’s helpers like node attributes and Ruby classes (File, Dir) as possible instead of shelling out. Look at node.hostname, it may return the result you’re looking for without shelling out; otherwise, use node.hostname.split(‘.’).first so you just get the first part of the hostname that’s contained in the file you’re loading.

On Tuesday, October 29, 2013 at 11:13 PM, Manoj Thakkar 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 :frowning:

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

hostp=hostname -s
Chef::Log.info (http://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 <daniel@condomitti.com (mailto:daniel@condomitti.com)> 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 (http://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 (http://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 (http://Log.info) "Found host #{host} in #{line}"
account_value = line.split(':').last
Chef::Log.info (http://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

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" manoj.thakkar@gmail.com 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 manoj.thakkar@gmail.comwrote:

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 :frowning:

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 <
daniel@condomitti.com> 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

Thanks a lot for the help Daniel,

it worked , i used node.hostname as you suggested and it provided me the
value i was looking for, Thanks a lot for the help,

its been troubling me from last 3-4 hours now, i am not using Mixlib Shell
out rather i wrote ruby_block and ruby for this, i hope thats the right way
to do it,

all this time this host.chomp was the issue.

Thanks again everyone for the help

Manoj

On Tue, Oct 29, 2013 at 11:23 PM, Daniel Condomitti
daniel@condomitti.comwrote:

If you can’t use node[:fqdn] due to it including the rest of the node’s
hostname then you need to call .chomp on the output from
ShellOut.run_command:

chef > Mixlib::ShellOut.new('hostname -s').run_command.stdout
=> "disrupt\n”

Otherwise you’ll never match that ‘account-rest.properties’ file since it
contains “pdevecsas300:acc1” and not “pdevecsas300\n:acc1”. I’d really
recommend leveraging as much of chef’s helpers like node attributes and
Ruby classes (File, Dir) as possible instead of shelling out. Look at
node.hostname, it may return the result you’re looking for without shelling
out; otherwise, use node.hostname.split(‘.’).first so you just get the
first part of the hostname that’s contained in the file you’re loading.

On Tuesday, October 29, 2013 at 11:13 PM, Manoj Thakkar 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 :frowning:

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 <daniel@condomitti.com

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

Sorry for asking it , but i am stuck again , looks like i am not able to
read the variable i got from ruby_block

i am not sure what's wrong :

ruby_block "acc-deploy-host" do
block do
Chef::Log.info " host name is #{node['hostname']} "
host=node['hostname']
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}"
deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set['account_deploy']=deploy
Chef::Log.info "Found account value #{node['account_deploy']}"
end
end
end

node['account_deploy'].each do |x|

puts #x

end
end

Logs :
*
*
Chef Client failed. 0 resources updated
[2013-10-30T01:14:01-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:14:01-07:00] FATAL: NoMethodError: undefined method `each'
for "acc1,acc2\n":String
You have mail in /var/spool/mail/deploy

On Tue, Oct 29, 2013 at 11:37 PM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Thanks a lot for the help Daniel,

it worked , i used node.hostname as you suggested and it provided me the
value i was looking for, Thanks a lot for the help,

its been troubling me from last 3-4 hours now, i am not using Mixlib Shell
out rather i wrote ruby_block and ruby for this, i hope thats the right way
to do it,

all this time this host.chomp was the issue.

Thanks again everyone for the help

Manoj

On Tue, Oct 29, 2013 at 11:23 PM, Daniel Condomitti <daniel@condomitti.com

wrote:

If you can’t use node[:fqdn] due to it including the rest of the node’s
hostname then you need to call .chomp on the output from
ShellOut.run_command:

chef > Mixlib::ShellOut.new('hostname -s').run_command.stdout
=> "disrupt\n”

Otherwise you’ll never match that ‘account-rest.properties’ file since it
contains “pdevecsas300:acc1” and not “pdevecsas300\n:acc1”. I’d really
recommend leveraging as much of chef’s helpers like node attributes and
Ruby classes (File, Dir) as possible instead of shelling out. Look at
node.hostname, it may return the result you’re looking for without shelling
out; otherwise, use node.hostname.split(‘.’).first so you just get the
first part of the hostname that’s contained in the file you’re loading.

On Tuesday, October 29, 2013 at 11:13 PM, Manoj Thakkar 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 :frowning:

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 <
daniel@condomitti.com> 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

On Wednesday, October 30, 2013 at 1:16 AM, Manoj Thakkar wrote:

Sorry for asking it , but i am stuck again , looks like i am not able to read the variable i got from ruby_block

i am not sure what's wrong :

ruby_block "acc-deploy-host" do
block do
Chef::Log.info (http://Log.info) " host name is #{node['hostname']} "
host=node['hostname']
puts " hostname is #{host}"

File.open("/local_app/account-rest.properties", "r").each_line do |line|
Chef::Log.info (http://Log.info) "#{host} Account Rest line: #{line}"
if line.include?(host)
Chef::Log.info (http://Log.info) "Found host #{host} in #{line}"
deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set['account_deploy']=deploy
Chef::Log.info (http://Log.info) "Found account value #{node['account_deploy']}"
end
end
end

node['account_deploy'].each do |x|

puts #x

end
end

An important thing to keep in mind about Ruby blocks is that it may or may not be executed at a different time than the surrounding code. In this case, Chef holds on to the code you pass into block do so it can evaluate it during the converge phase of the chef run. The surrounding code is executed immediately.

--
Daniel DeLeo

Thanks Daniel,

What i am trying to achieve is set an array of strings as an attribute or
variable inside the ruby block and then use it outside in various chef
resource,

How do i do that ? please advise.

Thanks
Manoj

On Wed, Oct 30, 2013 at 8:55 AM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, October 30, 2013 at 1:16 AM, Manoj Thakkar wrote:

Sorry for asking it , but i am stuck again , looks like i am not able to
read the variable i got from ruby_block

i am not sure what's wrong :

ruby_block "acc-deploy-host" do
block do
Chef::Log.info " host name is #{node['hostname']} "
host=node['hostname']
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}"
deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set['account_deploy']=deploy
Chef::Log.info "Found account value #{node['account_deploy']}"
end
end
end

node['account_deploy'].each do |x|

puts #x

end
end

An important thing to keep in mind about Ruby blocks is that it may or may
not be executed at a different time than the surrounding code. In this
case, Chef holds on to the code you pass into block do so it can evaluate
it during the converge phase of the chef run. The surrounding code is
executed immediately.

--
Daniel DeLeo

On Wednesday, October 30, 2013 at 9:41 AM, Manoj Thakkar wrote:

Thanks Daniel,

What i am trying to achieve is set an array of strings as an attribute or variable inside the ruby block and then use it outside in various chef resource,

How do i do that ? please advise.

Thanks
Manoj

Looking at your recipe code, it looks like you read some file, grep the relevant information out of it, and then you want to create chef resources to configure your system based on the contents. So you you take a deeper look.

Where does this file come from? Is it managed with Chef? If it comes from some other process, then you could read the file directly in the recipe without using a ruby_block resource.

If, on the other hand, you manage this file with Chef, then you should try to pull the relevant data in the file into chef attributes (or a data bag item or whatever). Then you’ll have all the data you’re trying to extract from the file already available in the compile phase of the chef run and you won’t have to parse the file at all.

--
Daniel DeLeo

Thanks Daniel,

The file has dynamic content provided by end users, so i have to read this
file, you mean not to use the ruby_block and put the code directly in the
recipe ?
like this , it did not work for me , may be i am doing something wrong

Thanks for your help

-Manoj

cookbook_file "/local_app/#{node[:tcat][:war_name]}.properties" do
source "#{node[:tcat][:war_name]}.properties"
mode 0777
owner "deploy"
group "deploy"
end

Chef::Log.info " host name is #{node['hostname']} "
host=node['hostname']
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}"
deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set[:account_deploy]=deploy
Chef::Log.info "Found account value #{node['account_deploy']}"
end
end

On Wed, Oct 30, 2013 at 9:56 AM, Daniel DeLeo dan@kallistec.com wrote:

On Wednesday, October 30, 2013 at 9:41 AM, Manoj Thakkar wrote:

Thanks Daniel,

What i am trying to achieve is set an array of strings as an attribute or
variable inside the ruby block and then use it outside in various chef
resource,

How do i do that ? please advise.

Thanks
Manoj

Looking at your recipe code, it looks like you read some file, grep the
relevant information out of it, and then you want to create chef resources
to configure your system based on the contents. So you you take a deeper
look.

Where does this file come from? Is it managed with Chef? If it comes from
some other process, then you could read the file directly in the recipe
without using a ruby_block resource.

If, on the other hand, you manage this file with Chef, then you should try
to pull the relevant data in the file into chef attributes (or a data bag
item or whatever). Then you’ll have all the data you’re trying to extract
from the file already available in the compile phase of the chef run and
you won’t have to parse the file at all.

--
Daniel DeLeo