Ruby syntax issue or may be chef command

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:‘acc1’,'acc2’
pdevecsas300:‘acc1’,‘acc2’

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:infrom_file’

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method `each’
for "“acc1”,“acc2” ":String

Hi Manoj, it looks like you're assigning a string to node['account_deploy']
via '.last': .

deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set['account_deploy']=deploy

And then calling .each on it, which is not a member of String:

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

[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method `each'

for ""acc1","acc2" ":String

--
Marc Paradise
Software Engineer
Opscode

Twitter: @MarcParadise
freenode: mparadise

Thanks Mark,

i thought i am setting a node attribute of type string, All i want to do is
set an array of string in my ruby block and then use it outside the ruby
block.

is there another way to do it ?

Thanks
Manoj

On Wed, Oct 30, 2013 at 5:08 AM, Marc Paradise marc@opscode.com wrote:

Hi Manoj, it looks like you're assigning a string to
node['account_deploy'] via '.last': .

deploy = line.split(':').last
deploy = deploy.gsub(/\n/," ")
node.set['account_deploy']=deploy

And then calling .each on it, which is not a member of String:

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

[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method `each'

for ""acc1","acc2" ":String

--
Marc Paradise
Software Engineer
Opscode

Twitter: @MarcParadise
freenode: mparadise

That doesn't work because the ruby_block's block doesn't execute until
converge time and the loop you have there that is outside of the
ruby_block runs at compile time.

Douglas William Thrift
douglaswth@gmail.com
http://douglasthrift.net/

On 10/30/2013 1:47 AM, Manoj Thakkar wrote:

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:'acc1','acc2'
pdevecsas300:'acc1','acc2'

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:in from_file'

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method
`each' for ""acc1","acc2" ":String

Thanks Doug,

but i see the value of the node attribute though in the logs ,

Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError:* undefined method `each'
for ""acc1","acc2" ":String*
*
*
looks like its getting the value but not treating it as an array of strings
or something.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:03 AM, Douglas Thrift douglaswth@gmail.comwrote:

That doesn't work because the ruby_block's block doesn't execute until
converge time and the loop you have there that is outside of the
ruby_block runs at compile time.

Douglas William Thrift
douglaswth@gmail.com
http://douglasthrift.net/

On 10/30/2013 1:47 AM, Manoj Thakkar wrote:

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:'acc1','acc2'
pdevecsas300:'acc1','acc2'

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:in from_file'

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method
`each' for ""acc1","acc2" ":String

No, it's definitely a string you're assigning, not an array.

#split returns an array of strings. #last returns the last string in the

array. deploy is currently a string
deploy = line.split(':').last

You are doing a gsub on a string, so deploy is still a string. As an

aside, you could use #gsub! here to avoid the extra assignment.
deploy = deploy.gsub(/\n/," ")

node['account_deploy'] is being assigned to deploy which is a string

node.set['account_deploy']=deploy

What is unclear? The string happens to be %q["acc1","acc2"], which looks
like an array in the logs, but you can see in your logs that the " are
escaped, so you do in fact have a string.

On Wed, Oct 30, 2013 at 11:21 AM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Thanks Doug,

but i see the value of the node attribute though in the logs ,

Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError:* undefined method
`each' for ""acc1","acc2" ":String*
*
*
looks like its getting the value but not treating it as an array of
strings or something.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:03 AM, Douglas Thrift douglaswth@gmail.comwrote:

That doesn't work because the ruby_block's block doesn't execute until
converge time and the loop you have there that is outside of the
ruby_block runs at compile time.

Douglas William Thrift
douglaswth@gmail.com
http://douglasthrift.net/

On 10/30/2013 1:47 AM, Manoj Thakkar wrote:

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:'acc1','acc2'
pdevecsas300:'acc1','acc2'

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:in from_file'

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method
`each' for ""acc1","acc2" ":String

Thanks Josiah,

what is it i have to do to make it an array of strings ? the values are
comma separated so its an array essentially.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:57 AM, Josiah Kiehl bluepojo@gmail.com wrote:

No, it's definitely a string you're assigning, not an array.

#split returns an array of strings. #last returns the last string in the

array. deploy is currently a string
deploy = line.split(':').last

You are doing a gsub on a string, so deploy is still a string. As an

aside, you could use #gsub! here to avoid the extra assignment.
deploy = deploy.gsub(/\n/," ")

node['account_deploy'] is being assigned to deploy which is a string

node.set['account_deploy']=deploy

What is unclear? The string happens to be %q["acc1","acc2"], which looks
like an array in the logs, but you can see in your logs that the " are
escaped, so you do in fact have a string.

On Wed, Oct 30, 2013 at 11:21 AM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Thanks Doug,

but i see the value of the node attribute though in the logs ,

Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError:* undefined method
`each' for ""acc1","acc2" ":String*
*
*
looks like its getting the value but not treating it as an array of
strings or something.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:03 AM, Douglas Thrift douglaswth@gmail.comwrote:

That doesn't work because the ruby_block's block doesn't execute until
converge time and the loop you have there that is outside of the
ruby_block runs at compile time.

Douglas William Thrift
douglaswth@gmail.com
http://douglasthrift.net/

On 10/30/2013 1:47 AM, Manoj Thakkar wrote:

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:'acc1','acc2'
pdevecsas300:'acc1','acc2'

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:in from_file'

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method
`each' for ""acc1","acc2" ":String

Use the built in CSV class in Ruby's standard library:
http://www.ruby-doc.org/stdlib-2.0.0/libdoc/csv/rdoc/

irb(main):020:0> require 'csv'
=> true
irb(main):021:0> CSV.parse_line(""acc1","acc2"")
=> ["acc1", "acc2"]

On Wed, Oct 30, 2013 at 12:31 PM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Thanks Josiah,

what is it i have to do to make it an array of strings ? the values are
comma separated so its an array essentially.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:57 AM, Josiah Kiehl bluepojo@gmail.com wrote:

No, it's definitely a string you're assigning, not an array.

#split returns an array of strings. #last returns the last string in

the array. deploy is currently a string
deploy = line.split(':').last

You are doing a gsub on a string, so deploy is still a string. As an

aside, you could use #gsub! here to avoid the extra assignment.
deploy = deploy.gsub(/\n/," ")

node['account_deploy'] is being assigned to deploy which is a string

node.set['account_deploy']=deploy

What is unclear? The string happens to be %q["acc1","acc2"], which looks
like an array in the logs, but you can see in your logs that the " are
escaped, so you do in fact have a string.

On Wed, Oct 30, 2013 at 11:21 AM, Manoj Thakkar manoj.thakkar@gmail.comwrote:

Thanks Doug,

but i see the value of the node attribute though in the logs ,

Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError:* undefined method
`each' for ""acc1","acc2" ":String*
*
*
looks like its getting the value but not treating it as an array of
strings or something.

Thanks
Manoj

On Wed, Oct 30, 2013 at 11:03 AM, Douglas Thrift douglaswth@gmail.comwrote:

That doesn't work because the ruby_block's block doesn't execute until
converge time and the loop you have there that is outside of the
ruby_block runs at compile time.

Douglas William Thrift
douglaswth@gmail.com
http://douglasthrift.net/

On 10/30/2013 1:47 AM, Manoj Thakkar wrote:

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 please advise

Here is the code i am writing:

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

properties file data :

pbldejksu300:'acc1','acc2'
pdevecsas300:'acc1','acc2'

logs :

Cookbook Trace:

/var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:26:in block in from_file' /var/chef/cache/cookbooks/base_tomcat/recipes/new.rb:9:in from_file'

[2013-10-30T01:45:07-07:00] ERROR: Running exception handlers
[2013-10-30T01:45:07-07:00] FATAL: Saving node information to
/var/chef/cache/failed-run-data.json
[2013-10-30T01:45:07-07:00] ERROR: Exception handlers complete
Chef Client failed. 0 resources updated
[2013-10-30T01:45:07-07:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
[2013-10-30T01:45:07-07:00] FATAL: NoMethodError: undefined method
`each' for ""acc1","acc2" ":String