HiveBrain v1.2.0
Get Started
← Back to all entries
snippetMinor

How to Chef things that don't exist yet

Submitted by: @import:stackexchange-devops··
0
Viewed 0 times
chefthingsexistyetthathowdon

Problem

Let us say I have some Chef code like:

require 'mixlib/shellout'
yum_package 'somepackage'
myvar =  Mixlib::ShellOut.new('/bin/somecommand').run_command.stdout.strip


Where /bin/somecommand does not exist yet because it is installed by somepackage. This will fail at recipe compile time for that reason, but will obviously work at convergence time providing the package installs successfully (and if it doesn't then obviously the recipe has failed anyway). This also fails if the package is installed be a previous recipe in the runlist since they are all compiled together upfront. How do I include things in a Chef recipe that that recipe or runlist installs itself?

Solution

I'd go with node.run_state to store a transient variable in a run and define it in a ruby_block so it happens at converge time, something like this:

yum_package 'somepackage'

ruby_block 'set myvar' do
  block do
    node.run_state['my_var'] = Mixlib::ShellOut.new('/bin/somecommand').run_command.stdout.strip
  end
end


As far as I know requiring 'mixlib/shellout' is not necessary.

Code Snippets

yum_package 'somepackage'

ruby_block 'set myvar' do
  block do
    node.run_state['my_var'] = Mixlib::ShellOut.new('/bin/somecommand').run_command.stdout.strip
  end
end

Context

StackExchange DevOps Q#2127, answer score: 4

Revisions (0)

No revisions yet.