1
votes

I have write the simple recipe here which is to create a directory in the node. But it shows error while running the chef-client.

directory "~/build" do

action :create

end

* Parent directory ~ does not exist, cannot create ~/build
================================================================================
Error executing action `create` on resource 'directory[~/build]'
================================================================================

Chef::Exceptions::EnclosingDirectoryDoesNotExist
------------------------------------------------
Parent directory ~ does not exist, cannot create ~/build

thanks for your valuable comments.

2
Its becuase of ~ sign. I think you need to use .. instead of that - qamar
but normally can make directory with this command: mkdir ~/build Why not in chef? - VijayVishnu
Because ~ is a shell shortcut for $HOME. Ruby has no special meaning for ~ so replace it by $HOME and it will create the directory in the home of the user running chef (usually root). - Tensibai
again it shows the same error * Parent directory $HOME does not exist, cannot create $HOME/build - VijayVishnu
bash "create directory" do code <<-EOH mkdir ~/build EOH end This recipe create the directory build. But using directory resource getting the above error.. - VijayVishnu

2 Answers

2
votes

As mentioned above, ~ has no context in Ruby, but you can use File.expand_path to alter it to the correct directory...

directory File.expand_path("~/build") do
  action :create
end

This looks cleaner than the approach above, at least to me, but they are still correct.

http://www.ruby-doc.org/core-2.1.2/File.html#method-c-expand_path

1
votes

~ or $HOME have no meaning in ruby, they're ok in shell context.

directory "#{ENV['HOME']}/build" would be better.

it will create the directory in the home dir of the user running chef.