Hi there,
I can’t figure out what I’m doing wrong with this. It seems trivial enough
but I’m not a software developer and this is puzzling me right now. I think
my issue has more to do with pure Ruby than Chef but either way, I can’t
figure it out.
If it matters, I’m working with chef-solo 12.0.3 and provisioning things
via Vagrant 1.7.2.
I’m doing something that should be pretty straightforward: creating a
number of directories declared in an array:
dirs = [ Chef::Config[:data_bag_path],
"#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}" ]
dirs.each do |dir|
directory dir do
owner "foo"
group "foo"
mode 00440
recursive true
action :create
end
end
This results in the following error during provisioning
==> default: Compiled Resource:
==> default: ------------------
==> default: # Declared in
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/cookbooks/nap/recipes/default.rb:90:in
`block in from_file’
==> default:
==> default:
directory("["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]")
do
==> default: action [:create]
==> default: retries 0
==> default: retry_delay 2
==> default: default_guard_interpreter :default
==> default: path
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]
==> default: recursive true
==> default: declared_type :directory
==> default: cookbook_name :nap
==> default: recipe_name “default”
==> default: owner “foo”
==> default: group “foo”
==> default: mode 288
==> default: end
==> default:
==> default: [2015-02-26T12:09:20-05:00] INFO: Running queued delayed
notifications before re-raising exception
==> default: [2015-02-26T12:09:20-05:00] ERROR: Running exception handlers
==> default: [2015-02-26T12:09:20-05:00] ERROR: Exception handlers complete
==> default: [2015-02-26T12:09:20-05:00] FATAL: Stacktrace dumped to
/var/chef/cache/chef-stacktrace.out
==> default: [2015-02-26T12:09:20-05:00] ERROR:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]]
(nap::default line 90) had an error: Chef::Exceptions::ValidationFailed:
Option path must be a kind of String! You passed
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"].
==> default: [2015-02-26T12:09:21-05:00] FATAL:
Chef::Exceptions::ChildConvergeError: Chef run process exited
unsuccessfully (exit code 1)
I ran this through chef-shell as well:
$ chef-shell -s -c solo.rb
loading configuration: solo.rb
Session type: solo
Loading…done.
Ohai2u vagrant@nap-cookbook!
chef > puts Chef::Config[:data_bag_path]
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags
=> nil
chef > dirs = [ Chef::Config[:data_bag_path],
"#{Chef::Config[:data_bag_path]}/#{node[‘foo’][‘bar’]}" ]
NoMethodError: undefined method []' for nil:NilClass from (irb):3 from /opt/chef/embedded/apps/chef/lib/chef/shell.rb:75:in
block in
start’
from /opt/chef/embedded/apps/chef/lib/chef/shell.rb:74:in catch' from /opt/chef/embedded/apps/chef/lib/chef/shell.rb:74:in
start’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:in
load’
from /usr/bin/chef-shell:40:in `'
chef > dirs = []
=> []
chef > dirs = [ Chef::Config[:data_bag_path] ]
=> [["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]]
chef > puts dirs
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags
=> nil
chef > dirs = [ Chef::Config[:data_bag_path],
"#{Chef::Config[:data_bag_path]}/test" ]
=> [["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"],
"["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/test"]
chef > puts dirs
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/test
=> nil
chef >
So as you can see in the above output, it’s adding brackets around some of
the things stored in the array and that seems to be why the Chef run is
failing, but I don’t understand why adding the brackets to begin with.
I also have no idea why this line produces an error:
chef > dirs = [ Chef::Config[:data_bag_path],
"#{Chef::Config[:data_bag_path]}/#{node[‘foo’][‘bar’]}" ]
Would somebody please mind explaining what I’m doing wrong? Thank you!