使用自定义属性安装多个包时出现 Chef 错误

Chef Error while installing multiple packages using custom attributes

我正在尝试使用自定义属性使用 Chef 的 'package' 资源安装多个包。 当我尝试时:

package %w(python3 python3-pip) do
action :install
end

上面的代码对我来说工作正常,但同样的代码在尝试使用自定义属性时出错。

请看-

我的 recipe_code :

package %w(node['python']['pkg_name'] node['python-pip']['pkg_name']) do
    action :install
end

Attribute_code :

default['python']['pkg_name'] = 'python3'
default['python-pip']['pkg_name'] = 'python3-pip'

错误日志:

  Compiling Cookbooks...
   Converging 3 resources
   Recipe: odoo_setup::odoo_linux
     * apt_update[update_ubuntu_pkg_lib] action update
       * directory[/var/lib/apt/periodic] action create (up to date)
       * directory[/etc/apt/apt.conf.d] action create (up to date)
       * file[/etc/apt/apt.conf.d/15update-stamp] action create_if_missing (up to date)
       * execute[apt-get -q update] action run
         - execute ["apt-get", "-q", "update"]
       - force update new lists of packages
     * apt_package[node['python']['pkg_name'], node['python-pip']['pkg_name']] action install
       * No candidate version available for node['python']['pkg_name'], node['python-pip']['pkg_name']
       ================================================================================
       Error executing action `install` on resource 'apt_package[node['python']['pkg_name'], node['python-pip']['pkg_name']]'
       ================================================================================

       Chef::Exceptions::Package
       -------------------------
       No candidate version available for node['python']['pkg_name'], node['python-pip']['pkg_name']

       Resource Declaration:
       ---------------------
       # In /tmp/kitchen/cache/cookbooks/odoo_setup/recipes/odoo_linux.rb

        32: package %w(node['python']['pkg_name'] node['python-pip']['pkg_name']) do
        33: # package node['python']['pkg_name'] do
        34:     action :install
        35: end
        36:

%w 是 Ruby 中的百分号文字,它从括号中的单独单词创建一个数组。您的第一个示例有效,因为

%w(python3 python3-pip) == ['python3', 'python3-pip']

但是这个%w不支持字符串插值。你的第二个例子实际上是:

%w(node['python']['pkg_name'] node['python-pip']['pkg_name']) == ["node['python']['pkg_name']", "node['python-pip']['pkg_name']"]

看看你的值实际上是字符串,而不是变量。如果您需要使用变量,请使用普通的 Array 初始化,而不是 %w literal.

package [node['python']['pkg_name'], node['python-pip']['pkg_name']]