Rspec 警告:从以前的资源克隆资源属性

Rspec 警告:从以前的资源克隆资源属性,rspec,chef-infra,Rspec,Chef Infra,我有下面的配方代码 # change permissions and group execute 'set group' do command 'sudo chgrp -R tomcat /opt/tomcat/conf' end %w(/opt/tomcat/conf /opt/tomcat/conf/*).each do |tomcat_directory| execute 'set directory permissions' do command 'sudo chmod

我有下面的配方代码

# change permissions and group
execute 'set group' do
  command 'sudo chgrp -R tomcat /opt/tomcat/conf'
end

%w(/opt/tomcat/conf /opt/tomcat/conf/*).each do |tomcat_directory|
  execute 'set directory permissions' do
    command 'sudo chmod g+rwx ' + tomcat_directory
  end
end
当我运行chef exec rspec--color时,我看到所有测试都通过了,但是还有一个警告

[2017-05-10T21:12:36+05:30] WARN: Cloning resource attributes for execute[set directory permissions] from prior resource
Previous execute[set directory permissions]: C:/Users/AASJD~1/AppData/Local/Temp/chefspec20170510-14248-1faza0file_cache_path/cookbooks/tomcat/recipes/installtomcat.rb:25:in `block in from_file'
Current  execute[set directory permissions]:/Users/AASJD~1/AppData/Local/Temp/chefspec20170510-14248-1faza0file_cache_path/cookbooks/tomcat/recipes/installtomcat.rb:25:in `block in from_file' 
(CHEF-3694)C:/Users/AASJD~1/AppData/Local/Temp/chefspec20170510-14248-1faza0file_cache_path/cookbooks/tomcat/recipes/installtomcat.rb:25:in `block in from_file'.
Please see https://docs.chef.io/deprecations_resource_cloning.html for further details and information on how to correct this problem. at C:/opscode/chefdk/embedded/lib/ruby/gems/2.3.0/gems/chef-12.19.36-universal-mingw32/lib/chef/event_dispatch/dispatcher.rb:43:in `call'

根据此链接,资源不应具有相同的名称,或者目标不应相同,但在本例中并非如此。那么为什么会出现这个错误呢?

这是因为循环阵列。下面的代码修复了这个问题

%w(/opt/tomcat/conf /opt/tomcat/conf/*).each do |tomcat_directory|
  execute 'sudo chmod g+rwx ' + tomcat_directory
end

从您的第一个代码开始,您可以将
tomcat_目录
插入到资源名称中:
execute“set#{tomcat_目录}目录权限”do
会起到相反的作用,由于ruby的Duck类型,添加字符串可能会导致陌生人的行为,即:x+“A”如果将A添加到x而不是“1A”,则很可能以十进制ASCII值结束。如果x是1,则字符串插值避免一堆内存拷贝和临时字符串对象,在stackoverflow上有一堆q/A。特别是在chef菜谱中,您应该坚持使用插值,以避免DSL解释器出现问题和一些边缘情况