【发布时间】:2017-02-10 03:41:01
【问题描述】:
我正在尝试编写我的第一个厨师自定义资源。但是,我无法访问 一些 其他嵌入式资源中的属性。
我创建了以下资源
property :template_shortname, String
action :setup do
node.set['apache']['version'] = '2.4'
node.set['apache']['package'] = 'apache2'
include_recipe "apache2::default"
print "the shortname is " + template_shortname
web_app "sample_app" do
server_name "server.com"
server_aliases []
docroot "/www-data"
template "prefix " + template_shortname
end
end
但是当我执行这个时,我得到:
来自调试日志
The shortname is: server
但是 template_shortname 的参数包含一个空数组。所以它退出:
TypeError
---------
no implicit conversion of Array into String
Cookbook Trace:
---------------
/tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `+'
/tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:21:in `block (2 levels) in class_from_file'
/tmp/kitchen/cache/cookbooks/phpapp/resources/setup.rb:17:in `block in class_from_file'
我正在使用 chef-dk Chef 开发工具包版本:1.1.16 厨师客户端版本:12.17.44 发货版本:master (83358fb62c0f711c70ad5a81030a6cae4017f103) 伯克斯版本:5.2.0 厨房版本:1.14.2
【问题讨论】:
-
template "prefix #{template_shortname}"使用字符串插值,而不是加法 -
可能来自doc 的
new_resource.template_shortname。总而言之,设置这样的属性并调用 include_recipe 操作听起来很脆弱,可能很快就会咬你。这些应该在你的 phpapp 食谱属性文件和食谱中,总而言之,我不明白你在这里想要做什么。 -
对不起,我应该提到 new_resource 也是空的。据我了解,这篇博文blog.backslasher.net/chef-custom-resources.html 不再需要。如果我正确阅读了厨师文档,则仅在名称冲突的情况下才需要。
-
确实如此,但我认为最好保持一致。总而言之,您的问题来自
web_app的templateproperty 是一个数组,因此+运算符充当数组而不是字符串。使用字符串插值,它应该可以工作,我仍然认为属性和include_recipe绝对不应该在自定义资源中定义(尝试为两个应用程序安装两次 apache 是没有意义的)。
标签: chef-infra