Malformed array causing Chef to error out


#1

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#2

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path],
"#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}"
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#3

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set to
true, but per the docs, it doesn’t apply recursive permissions, and I want
to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this instead:

directory “#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}” do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure enough,
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does not
exist. However I now have a folder directly under / called ‘["’ with the
above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#4

That was less than helpful, let me try again…

When you’re creating your array of directories, the first entry is, itself,
an array. The second entry is based on the first, and since it is an array
being interpolated into a string, you end up with another funky entry in
your dirs array.

You can see this in irb:

irb(main):001:0> data_bags_path = [ “/var/chef/cach/data_bags” ]
=> ["/var/chef/cach/data_bags"]
irb(main):002:0> dirs = [ data_bags_path, “#{data_bags_path}/my_data_bag” ]
=> [["/var/chef/cach/data_bags"],
"["/var/chef/cach/data_bags"]/my_data_bag"]

Assuming you’re not adding any additional custom paths to the data bags
path, you can fix this by taking the first entry from the array and using
that in your dirs variable like this:

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ data_bags_path, “#{data_bags_path}/#{node[‘data_bag’]}” ]

Alternatively, you can use the recursive attribute of the directory
resource, and skip creating the data_bags_path directory, because it would
be recursively created by the resource.

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ “#{data_bags_path}/#{node[‘data_bag’]}” ]

dirs.each do |dir|
directory dir do
recursive true
end
end

On Thu, Feb 26, 2015 at 11:03 AM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#5

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this is
    similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its permission?
    Is that intended? If chef creates one of the parent directory the dafault
    permissions will govern by umask etc… should chef change that? Current
    behavior is to leave them as it is (or let umask govern them). I find it
    safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set to
true, but per the docs, it doesn’t apply recursive permissions, and I want
to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this instead:

directory “#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}” do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure enough,
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does not
exist. However I now have a folder directly under / called ‘["’ with the
above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#6

Ok, thanks. So, really, what I should take away from all this is that
Chef::Config[:data_bag_path], for some reason, is an array and I just have
to deal with it. That it would be an array didn’t make sense to me so I
didn’t even check to see if it was an array, despite the signs.

irb(main):001:0> test = “1”
=> "1"
irb(main):002:0> puts test
1
=> nil
irb(main):003:0> test.push(“2”)
NoMethodError: undefined method push' for "1":String from (irb):3 from /opt/chefdk/embedded/bin/irb:11:in'
irb(main):004:0> test = [“1”]
=> [“1”]
irb(main):005:0> puts test
1
=> nil
irb(main):006:0> test.push(“2”)
=> [“1”, “2”]

So I learned that if there’s only one thing stored in an array, if you
display the contents of the array, you can’t tell that it is an array from
the output, which is why I was confused to begin with:
chef > puts Chef::Config[:data_bag_path]
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags
=> nil
Doesn’t look like an array.

However:
chef > Chef::Config[:data_bag_path].kind_of?(Array)
=> true

Likewise you can push additional values to it.

Alright, so lesson learned! I can work with that now. Thanks!

On Thu, Feb 26, 2015 at 1:20 PM, Brandon Raabe brandocorp@gmail.com wrote:

That was less than helpful, let me try again…

When you’re creating your array of directories, the first entry is,
itself, an array. The second entry is based on the first, and since it is
an array being interpolated into a string, you end up with another funky
entry in your dirs array.

You can see this in irb:

irb(main):001:0> data_bags_path = [ “/var/chef/cach/data_bags” ]
=> ["/var/chef/cach/data_bags"]
irb(main):002:0> dirs = [ data_bags_path, “#{data_bags_path}/my_data_bag” ]
=> [["/var/chef/cach/data_bags"],
"["/var/chef/cach/data_bags"]/my_data_bag"]

Assuming you’re not adding any additional custom paths to the data bags
path, you can fix this by taking the first entry from the array and using
that in your dirs variable like this:

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ data_bags_path, “#{data_bags_path}/#{node[‘data_bag’]}” ]

Alternatively, you can use the recursive attribute of the directory
resource, and skip creating the data_bags_path directory, because it
would be recursively created by the resource.

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ “#{data_bags_path}/#{node[‘data_bag’]}” ]

dirs.each do |dir|
directory dir do
recursive true
end
end

On Thu, Feb 26, 2015 at 11:03 AM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#7

Ranjib,
Perhaps I’m looking at the permissions thing the wrong way, but here’s how
I understand it: I want the files and directories I’m creating to be owned
by the vagrant user and the root group.
/tmp/vagrant-chef is owned by vagrant:root, however the directory
underneath that, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb is
owned by root:root. This is a folder provisioned automatically by
Vagrant/Berkshelf, not by Chef, and I don’t know if I can customize the
ownership, or how I would do that, nor am I really concerned with that so
long as it doesn’t cause me any trouble :slight_smile:

And to your question about why I’m creating data bag paths, well, again,
perhaps I’m doing it wrong, but they currently don’t exist, and I want them
to exist. I’m using Chef-Solo, not Chef-Client. I’m not new to Chef-Client,
but I’ve never used Chef-Solo so I’m still learning the differences.

We saw earlier that Chef::Config[:data_bag_path].first =
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags but before I
run this code to create the directories and files I need, there is no
data_bags directory under
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb, so I need to create it
and place a data bag item in it. The reason why I need a data bag at all is
because I’m trying to store credentials for AWS S3 so I can then use the
aws_s3_file resource from the aws cookbook
https://github.com/opscode-cookbooks/aws. I’ve never had to use that
cookbook but it says I should use a data bag to store the AWS credentials.
I probably don’t have to, but I’m going to need to use data bags to store
some secrets for something else later. Or maybe I don’t actually have to
use data bags for this next thing I’m thinking, but I’ll need to figure out
where to put all the secrets I need to pass to the app for it to work.

On Thu, Feb 26, 2015 at 1:48 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this is
    similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its permission?
    Is that intended? If chef creates one of the parent directory the dafault
    permissions will govern by umask etc… should chef change that? Current
    behavior is to leave them as it is (or let umask govern them). I find it
    safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set to
true, but per the docs, it doesn’t apply recursive permissions, and I want
to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this instead:

directory “#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}” do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure enough,
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does not
exist. However I now have a folder directly under / called ‘["’ with the
above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#8

you can use an execute statement with 'chown -R vagrant:root /path/to/repo’
which will do what you want.
you can try chef-client -z (aka localmode) as well, lot of new work are
happening their, it has solo like semantics.

On Thu, Feb 26, 2015 at 11:30 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Ranjib,
Perhaps I’m looking at the permissions thing the wrong way, but here’s how
I understand it: I want the files and directories I’m creating to be owned
by the vagrant user and the root group.
/tmp/vagrant-chef is owned by vagrant:root, however the directory
underneath that, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb is
owned by root:root. This is a folder provisioned automatically by
Vagrant/Berkshelf, not by Chef, and I don’t know if I can customize the
ownership, or how I would do that, nor am I really concerned with that so
long as it doesn’t cause me any trouble :slight_smile:

And to your question about why I’m creating data bag paths, well, again,
perhaps I’m doing it wrong, but they currently don’t exist, and I want them
to exist. I’m using Chef-Solo, not Chef-Client. I’m not new to Chef-Client,
but I’ve never used Chef-Solo so I’m still learning the differences.

We saw earlier that Chef::Config[:data_bag_path].first =
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags but before I
run this code to create the directories and files I need, there is no
data_bags directory under
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb, so I need to create it
and place a data bag item in it. The reason why I need a data bag at all is
because I’m trying to store credentials for AWS S3 so I can then use the
aws_s3_file resource from the aws cookbook
https://github.com/opscode-cookbooks/aws. I’ve never had to use that
cookbook but it says I should use a data bag to store the AWS credentials.
I probably don’t have to, but I’m going to need to use data bags to store
some secrets for something else later. Or maybe I don’t actually have to
use data bags for this next thing I’m thinking, but I’ll need to figure out
where to put all the secrets I need to pass to the app for it to work.

On Thu, Feb 26, 2015 at 1:48 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this is
    similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its permission?
    Is that intended? If chef creates one of the parent directory the dafault
    permissions will govern by umask etc… should chef change that? Current
    behavior is to leave them as it is (or let umask govern them). I find it
    safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set to
true, but per the docs, it doesn’t apply recursive permissions, and I want
to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this instead:

directory “#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}” do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure
enough, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does
not exist. However I now have a folder directly under / called ‘["’ with
the above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock
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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#9

I prefer to use the execute resource as a last resort, I don’t see what
advantage it would give me over using the directory, file or template
resources with owner and group attributes?

On Thu, Feb 26, 2015 at 5:36 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can use an execute statement with ‘chown -R vagrant:root
/path/to/repo’ which will do what you want.
you can try chef-client -z (aka localmode) as well, lot of new work are
happening their, it has solo like semantics.

On Thu, Feb 26, 2015 at 11:30 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Ranjib,
Perhaps I’m looking at the permissions thing the wrong way, but here’s
how I understand it: I want the files and directories I’m creating to be
owned by the vagrant user and the root group.
/tmp/vagrant-chef is owned by vagrant:root, however the directory
underneath that, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb is
owned by root:root. This is a folder provisioned automatically by
Vagrant/Berkshelf, not by Chef, and I don’t know if I can customize the
ownership, or how I would do that, nor am I really concerned with that so
long as it doesn’t cause me any trouble :slight_smile:

And to your question about why I’m creating data bag paths, well, again,
perhaps I’m doing it wrong, but they currently don’t exist, and I want them
to exist. I’m using Chef-Solo, not Chef-Client. I’m not new to Chef-Client,
but I’ve never used Chef-Solo so I’m still learning the differences.

We saw earlier that Chef::Config[:data_bag_path].first =
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags but before I
run this code to create the directories and files I need, there is no
data_bags directory under
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb, so I need to create it
and place a data bag item in it. The reason why I need a data bag at all is
because I’m trying to store credentials for AWS S3 so I can then use the
aws_s3_file resource from the aws cookbook
https://github.com/opscode-cookbooks/aws. I’ve never had to use that
cookbook but it says I should use a data bag to store the AWS credentials.
I probably don’t have to, but I’m going to need to use data bags to store
some secrets for something else later. Or maybe I don’t actually have to
use data bags for this next thing I’m thinking, but I’ll need to figure out
where to put all the secrets I need to pass to the app for it to work.

On Thu, Feb 26, 2015 at 1:48 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this is
    similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its
    permission? Is that intended? If chef creates one of the parent directory
    the dafault permissions will govern by umask etc… should chef change
    that? Current behavior is to leave them as it is (or let umask govern
    them). I find it safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set
to true, but per the docs, it doesn’t apply recursive permissions, and I
want to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this instead:

directory "#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}"
do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure
enough, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does
not exist. However I now have a folder directly under / called ‘["’ with
the above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock
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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#10

My only experience with chef-solo is while using vagrant or test-kitchen,
and in both of those cases, the cookbooks and data bags are pushed to the
target machine prior to the chef run. It looks like you’re using vagrant,
either directly or via test kitchen. You can set your configuration to
upload the data bags you need for you chef run.

If you’re using vagrant, you’ll want to set the following:

Vagrant.configure(“2”) do |config|
config.vm.provision “chef_solo” do |chef|
chef.data_bags_path = "data_bags"
end
end

If you’re using Test Kitchen, you can set the following:


driver:
name: vagrant

provisioner:
name: chef_solo

suites:

  • name: default
    data_bags_path: “data_bags”

Getting back to your original issue, you were trying to create two
directories. The first was the actual data_bags folder, and the second was
the nested data bag folder. The first failure you received was because the
first element in your dirs array was an array. This caused the directory
resource to fail because the path attribute was not a string value.

The second attempt you hard coded path to the nested data bag folder, but
still received an error because the string interpolation for the array
value gave you a funky path, which was created, but was not what you were
expecting.

If you try the following code in your recipe, does it work?

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ “#{data_bags_path}/#{node[‘data_bag’]}” ]

dirs.each do |dir|
directory dir do
owner "vagrant"
group "root"
mode 0755
recursive true
action :create
end
end

On Thu, Feb 26, 2015 at 4:59 PM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

I prefer to use the execute resource as a last resort, I don’t see what
advantage it would give me over using the directory, file or template
resources with owner and group attributes?

On Thu, Feb 26, 2015 at 5:36 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can use an execute statement with ‘chown -R vagrant:root
/path/to/repo’ which will do what you want.
you can try chef-client -z (aka localmode) as well, lot of new work are
happening their, it has solo like semantics.

On Thu, Feb 26, 2015 at 11:30 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Ranjib,
Perhaps I’m looking at the permissions thing the wrong way, but here’s
how I understand it: I want the files and directories I’m creating to be
owned by the vagrant user and the root group.
/tmp/vagrant-chef is owned by vagrant:root, however the directory
underneath that, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb is
owned by root:root. This is a folder provisioned automatically by
Vagrant/Berkshelf, not by Chef, and I don’t know if I can customize the
ownership, or how I would do that, nor am I really concerned with that so
long as it doesn’t cause me any trouble :slight_smile:

And to your question about why I’m creating data bag paths, well, again,
perhaps I’m doing it wrong, but they currently don’t exist, and I want them
to exist. I’m using Chef-Solo, not Chef-Client. I’m not new to Chef-Client,
but I’ve never used Chef-Solo so I’m still learning the differences.

We saw earlier that Chef::Config[:data_bag_path].first =
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags but before I
run this code to create the directories and files I need, there is no
data_bags directory under
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb, so I need to create it
and place a data bag item in it. The reason why I need a data bag at all is
because I’m trying to store credentials for AWS S3 so I can then use the
aws_s3_file resource from the aws cookbook
https://github.com/opscode-cookbooks/aws. I’ve never had to use that
cookbook but it says I should use a data bag to store the AWS credentials.
I probably don’t have to, but I’m going to need to use data bags to store
some secrets for something else later. Or maybe I don’t actually have to
use data bags for this next thing I’m thinking, but I’ll need to figure out
where to put all the secrets I need to pass to the app for it to work.

On Thu, Feb 26, 2015 at 1:48 PM, Ranjib Dey dey.ranjib@gmail.com
wrote:

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this
    is similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its
    permission? Is that intended? If chef creates one of the parent directory
    the dafault permissions will govern by umask etc… should chef change
    that? Current behavior is to leave them as it is (or let umask govern
    them). I find it safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set
to true, but per the docs, it doesn’t apply recursive permissions, and I
want to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this
instead:

directory "#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}"
do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure
enough, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does
not exist. However I now have a folder directly under / called ‘["’ with
the above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock
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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!


#11

Yes, I’m good to go after adding .first! Sorry if I wasn’t clear in my last
response to you. Thanks!

On Thu, Feb 26, 2015 at 11:52 PM, Brandon Raabe brandocorp@gmail.com
wrote:

My only experience with chef-solo is while using vagrant or test-kitchen,
and in both of those cases, the cookbooks and data bags are pushed to the
target machine prior to the chef run. It looks like you’re using vagrant,
either directly or via test kitchen. You can set your configuration to
upload the data bags you need for you chef run.

If you’re using vagrant, you’ll want to set the following:

Vagrant.configure(“2”) do |config|
config.vm.provision “chef_solo” do |chef|
chef.data_bags_path = "data_bags"
end
end

If you’re using Test Kitchen, you can set the following:


driver:
name: vagrant

provisioner:
name: chef_solo

suites:

  • name: default
    data_bags_path: “data_bags”

Getting back to your original issue, you were trying to create two
directories. The first was the actual data_bags folder, and the second was
the nested data bag folder. The first failure you received was because the
first element in your dirs array was an array. This caused the directory
resource to fail because the path attribute was not a string value.

The second attempt you hard coded path to the nested data bag folder, but
still received an error because the string interpolation for the array
value gave you a funky path, which was created, but was not what you were
expecting.

If you try the following code in your recipe, does it work?

data_bags_path = Chef::Config[:data_bag_path].first
dirs = [ “#{data_bags_path}/#{node[‘data_bag’]}” ]

dirs.each do |dir|
directory dir do
owner "vagrant"
group "root"
mode 0755
recursive true
action :create
end
end

On Thu, Feb 26, 2015 at 4:59 PM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

I prefer to use the execute resource as a last resort, I don’t see what
advantage it would give me over using the directory, file or template
resources with owner and group attributes?

On Thu, Feb 26, 2015 at 5:36 PM, Ranjib Dey dey.ranjib@gmail.com wrote:

you can use an execute statement with ‘chown -R vagrant:root
/path/to/repo’ which will do what you want.
you can try chef-client -z (aka localmode) as well, lot of new work are
happening their, it has solo like semantics.

On Thu, Feb 26, 2015 at 11:30 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Ranjib,
Perhaps I’m looking at the permissions thing the wrong way, but here’s
how I understand it: I want the files and directories I’m creating to be
owned by the vagrant user and the root group.
/tmp/vagrant-chef is owned by vagrant:root, however the directory
underneath that, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb is
owned by root:root. This is a folder provisioned automatically by
Vagrant/Berkshelf, not by Chef, and I don’t know if I can customize the
ownership, or how I would do that, nor am I really concerned with that so
long as it doesn’t cause me any trouble :slight_smile:

And to your question about why I’m creating data bag paths, well,
again, perhaps I’m doing it wrong, but they currently don’t exist, and I
want them to exist. I’m using Chef-Solo, not Chef-Client. I’m not new to
Chef-Client, but I’ve never used Chef-Solo so I’m still learning the
differences.

We saw earlier that Chef::Config[:data_bag_path].first =
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags but before I
run this code to create the directories and files I need, there is no
data_bags directory under
/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb, so I need to create it
and place a data bag item in it. The reason why I need a data bag at all is
because I’m trying to store credentials for AWS S3 so I can then use the
aws_s3_file resource from the aws cookbook
https://github.com/opscode-cookbooks/aws. I’ve never had to use that
cookbook but it says I should use a data bag to store the AWS credentials.
I probably don’t have to, but I’m going to need to use data bags to store
some secrets for something else later. Or maybe I don’t actually have to
use data bags for this next thing I’m thinking, but I’ll need to figure out
where to put all the secrets I need to pass to the app for it to work.

On Thu, Feb 26, 2015 at 1:48 PM, Ranjib Dey dey.ranjib@gmail.com
wrote:

Why is Chef::Config[:data_bag_path] an array?

  • because you can have multiple folder containing the data bags. this
    is similar to cookbook_path. In your case you can just use .first.
    Recursive directory and permission:
  • If any of the parent directory exist should Chef change its
    permission? Is that intended? If chef creates one of the parent directory
    the dafault permissions will govern by umask etc… should chef change
    that? Current behavior is to leave them as it is (or let umask govern
    them). I find it safer

just curious why you are trying to create data bag paths?

On Thu, Feb 26, 2015 at 10:19 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

Thanks.
Why is Chef::Config[:data_bag_path] an array, though?
Also, you may have noticed I already have the recursive attribute set
to true, but per the docs, it doesn’t apply recursive permissions, and I
want to set the right permissions on both folders.

Anyway, I commented out the code I posted earlier and used this
instead:

directory
"#{Chef::Config[:data_bag_path]}/#{node[‘nap’][‘data_bag’]}" do
owner node[‘nap’][‘provisioning_owner’]
group node[‘nap’][‘provisioning_group’]
mode 00440
recursive true
action :create
end

The Chef run ran successfully. The run output says:
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
created directory
["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
owner changed to 500
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
group changed to 0
==> default: [2015-02-26T13:12:59-05:00] INFO:
directory[["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]/nap-vagrant]
mode changed to 440

You can see the brackets are still there and it looks fishy. Sure
enough, /tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags does
not exist. However I now have a folder directly under / called ‘["’ with
the above directory structure underneath it, i.e.
/["/tmp/vagrant-chef/c0f72feb81f20d85ca657a73371fcecb/data_bags"]

So I’m still in trouble :slight_smile:

On Thu, Feb 26, 2015 at 1:03 PM, Brandon Raabe brandocorp@gmail.com
wrote:

It looks like your first entry is an array.

dirs = [ Chef::Config[:data_bag_path], “#{Chef::Config[:data_bag_path]}/#{node[‘data_bag’]}”
]

directory("*["/tmp/vagrant-*chef/c0f72feb81f20d85ca657a73371fce
cb/data_bags"]") do

You can force it to create the entire path by adding the recursive
attribute to you directory resource.

On Thu, Feb 26, 2015 at 10:40 AM, Fabien Delpierre <
fabien.delpierre@gmail.com> wrote:

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:inblock 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:instart’
from /opt/chef/embedded/apps/chef/bin/chef-shell:37:in <top (required)>' from /usr/bin/chef-shell:40:inload’
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!