Installing Multiples Packages with Chef

When I try to install multiples packages with a wildcard naming I got the following error:

 * yum_package[mysql-server] action install (up to date)
 * yum_package[mysql*] action install
 * No candidate version available for mysql*
    ============================================================================                                                                                        ====
    Error executing action `install` on resource 'yum_package[mysql*]'
    ============================================================================                                                                                        ====

Recipe code is:

package 'mysql-server' do
  action :install
end

package 'mysql*' do
  action :install
end
0

2 Answers

You have to use the exact package name. The chef package resource does no magic to find matching packages.

The name of the resource (the part just after package) is used as the package name and given to the underlying system (yum on RH like systems, apt on debian like systems)

If you have multiples packages to install and a common configuration you can loop over them in your recipe instead:

['mysql-server','mysql-common','mysql-client'].each do |p|
  package p do
    action :install
  end
end

The array creation could be simplified with some ruby syntax as the words builder %w:

%w(mysql-server mysql-common mysql-client).each [...]

Since chef 12.1 the package resource accept an array of packages directly like this:

package %w(mysql-server mysql-common mysql-client)

This can be resolved using chef cases. Please see below

add the following to your attributes file:

packages = []

case node[:platform_family]
when 'rhel' #based on your OS
  packages = [
    "package1",
    "package2",
    "package3",
    "package4",
    "package5",
    "package6",
    "package7" ## Last line without comma
  ]
end

default[:cookbookname][:packages] = packages

Then, add the following to your recipe file (recipes/default.rb):

node[:cookbookname][:packages].each do |pkg|
  package pkg do
    action :install
    retries 3
    retry_delay 5
  end
end
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest