Skip to content

Instantly share code, notes, and snippets.

@RulerOf
Last active June 29, 2023 04:30
Show Gist options
  • Save RulerOf/8a2f2b2b9317fc1579e56aef0917ce58 to your computer and use it in GitHub Desktop.
Save RulerOf/8a2f2b2b9317fc1579e56aef0917ce58 to your computer and use it in GitHub Desktop.
Chef fails with 'No candidate version available' when using amazon-linux-extras on Amazon Linux 2

The Problem

We have a few packages we like to install through Amazon Linux Extras repo, but we discovered that this doesn't work right:

execute 'Enable java 11 via amazon-linux-extras' do
  command 'amazon-linux-extras enable java-openjdk11'
end

package 'java-11-openjdk'

Details

The chef run would always terminate with No candidate version available, and frustratingly, a simple yum install java-11-openjdk on the server's command line would work just fine. Further, a subsequent run of Chef on the same machine would also converge successfully.

This is caused by the python yum helper that Chef uses to maintain an in-memory copy of the available packages on the system. If you modify yum repos during a Chef run but don't use chef itself to make this change—by using amazon-linux-extras for example—this in-memory copy of available packages doesn't get updated, and Chef barfs at you. This has caused problems elsewhere.

Fix

The fix is to forcibly reset the helper inside of your chef code via a ruby block. Using the example above, it looks like this:

execute 'Enable java 11 via amazon-linux-extras' do
  command 'amazon-linux-extras enable java-openjdk11'
end

ruby_block 'reset yum python helper for java' do
  block do
    Chef::Provider::Package::Yum::PythonHelper::instance.restart
  end
end

package 'java-11-openjdk'

Additional Thoughts

If you want to get fancy, we can add a guard and a notifier to the execute resource. Even though these actions are idempotent, it takes several seconds to restart the helper and you may want to avoid that on subsequent runs.

execute 'Enable java 11 via amazon-linux-extras' do
  command 'amazon-linux-extras enable java-openjdk11'
  notifies :run, 'ruby_block[reset yum python helper for java]', :immediately
  not_if 'rpm -qa | grep java-11-openjdk'
end

ruby_block 'reset yum python helper for java' do
  block do
    Chef::Provider::Package::Yum::PythonHelper::instance.restart
  end
  action :nothing
end

package 'java-11-openjdk'

To be frank, this would probably make for a good LWRP cookbook.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment