Chef:如果模板不存在,则为模板创建一个目录

Mat*_*hew 17 chef chef-solo

如果我有一个正在创建的模板,我如何确保该目录存在?例如:

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
Run Code Online (Sandbox Code Playgroud)

这失败了,因为/var/www/example/shared/config不存在database.yml要复制到。我在考虑 puppet 如何让您“确保”目录存在。

Tim*_*ter 19

在创建模板之前使用目录资源创建目录。诀窍是还要指定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
Run Code Online (Sandbox Code Playgroud)

请注意,ownergroup真实被创建时的目录资源的仅适用于叶目录。目录其余部分的权限未定义,但可能是 root.root 以及您的 umask 是什么。