【问题标题】:Type error in chef recipe厨师食谱中的类型错误
【发布时间】:2012-11-01 07:24:51
【问题描述】:

我有以下节点定义:

{
    "prestashop_sites" : {
       "site1": { "username": "dave", "password" :"password123", "URL":"www.madeup.com" },
       "site2": { "username": "dave2", "password" :"password12", "URL":"www.madeup2.com" }
    },
       "run_list": [
       "recipe[prestashop]"
    ]
}

还有配方:

node["prestashop_sites"].each do |site|
        username site['username']
        Chef::Log.debug("Found a server: #{site['username']}")
end

remote_file "/tmp/prestashop152.zip" do
        source "http://www.prestashop.com/download/old/prestashop_1.5.2.0.zip"
        mode "0644"
        checksum "37aee9ef5388376e4377aeb240ab027e"
        backup false
        not_if "test -f /tmp/prestashop152.zip"
end

execute "unzip -o /tmp/prestashop152.zip -d #{node[:prestashop][:location]}" do
        not_if "test -f /var/www/#{node[:prestashop][:user]}/prestashop/index.php"
end

所以我的目标是安装几个 prestashop 实例(在我完成脚本之后)。

但我被卡住了:

10:  node["prestashop_sites"].each do |site| 
11>>    Chef::Log.debug("Found a server: #{site['username']}")
12:  end

Mon, 12 Nov 2012 21:26:14 +0100] DEBUG: Re-raising exception: 
                                        TypeError - can't convert String into Integer

知道为什么吗?!

【问题讨论】:

    标签: chef-infra recipe


    【解决方案1】:

    你有一个哈希作为 *node["prestashop_sites"]* 的子元素,而不是一个数组。所以你必须为 each 方法提供 2 个变量。 1 代表键,另一个代表值:

    10>>!  node["prestashop_sites"].each do |key, site| 
    11:    Chef::Log.debug("Found a server: name: #{key}, #{site['username']}")
    12:  end
    

    将为第一个站点打印 "Found a server: name: site1, dave"

    你得到的“奇怪”错误:TypeError - can't convert String into Integer,是因为当你只为每个方法提供 1 个变量时,ruby 会尝试将键和值都放入那个变量。这结束了分配的 [key, value] 的值,在您的情况下意味着

    网站 == ["网站", { "用户名": "dave", "密码" :"password123", "URL":"www.madeup.com" }]

    实际上是一个数组,并且您尝试使用字符串索引而不是整数访问数组元素。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-29
      相关资源
      最近更新 更多