chef-infrachef-recipechef-attributes

Chef Error while installing multiple packages using custom attributes


I am trying to install multiple packages with Chef's 'package' resource by using custom attributes. When I try:

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

The above code works fine for me, but same code is giving error while trying with custom attributes.

Please have a look-

My 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'

Error log:

  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:

Solution

  • %w is a percent literal in Ruby, that creates an array from separate words in the brackets. Your first example works, because

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

    But this %w does not support string interpolation. Your second example is actually:

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

    See how your values are actually strings, not variables. If you need to use variables, use ordinary Array initialization, and not %w literal.

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