厨师:为模板创build一个目录,如果它尚不存在

如果我有一个模板被创build,我怎样才能确保该目录存在? 例如:

template "#{node[:app][:deploy_to]}/#{node[:app][:name]}/shared/config/database.yml" do source 'database.yml.erb' owner node[:user][:username] group node[:user][:username] mode 0644 variables({ :environment => node[:app][:environment], :adapter => node[:database][:adapter], :database => node[:database][:name], :username => node[:database][:username], :password => node[:database][:password], :host => node[:database][:host] }) end 

由于/var/www/example/shared/config不存在,因此database.yml将被复制到此目录中。 我在想如何木偶让你“确保”一个目录存在。

使用目录资源在创build模板之前创build目录。 诀窍是还要指定recursive属性,否则操作将失败,除非目录中的所有部分都已经存在。

 config_dir = "#{node[:app][:deploy_to]}/#{node[:app][:name]}/shared/config" directory config_dir do owner node[:user][:username] group node[:user][:username] recursive true end template "#{config_dir}/database.yml" do source "database.yml.erb" ... end 

请注意,目录资源的ownergroup仅在创build时才应用于叶目录。 目录其余部分的权限是不确定的,但可能是root.root,无论你的umask是什么。

我没有意识到在template资源之前使用directory资源的其他方法:

 directory "#{node[:app][:deploy_to]}/#{node[:app][:name]}/shared/config/ owner node[:user][:username] group node[:user][:username] end