Search code examples
rubyvariableschef-infrachef-solo

Passing variables between chef resources


i would like to show you my use case and then discuss possible solutions:

Problem A: i have 2 recipes, "a" and "b".. "a" installs some program on my file system (say at "/usr/local/bin/stuff.sh" and recipe "b" needs to run this and do something with the output.

so recipe "a" looks something like:

execute "echo 'echo stuff' > /usr/local/bin/stuff.sh" 

(the script just echo(es) "stuff" to stdout)

and recipe "b" looks something like:

include_recipe "a"
var=`/usr/local/bin/stuff.sh` 

(note the backquotes, var should contain stuff)

and now i need to do something with it, for instance create a user with this username. so at script "b" i add

user "#{node[:var]}"

As it happens, this doesn't work.. apparently chef runs everything that is not a resource and only then runs the resources so as soon as i run the script chef complains that it cannot compile because it first tries to run the "var=..." line at recipe "b" and fails because the "execute ..." at recipe a did not run yet and so the "stuff.sh" script does not exist yet. Needless to say, this is extremely annoying as it breaks the "Chef runs everything in order from top to bottom" that i was promised when i started using it. However, i am not very picky so i started looking for alternative solutions to this problem, so:

Problem B: i've run across the idea of "ruby_block". apparently, this is a resource so it will be evaluated along with the other resources. I said ok, then i'd like to create the script, get the output in a "ruby_block" and then pass it to "user". so recipe "b" now looks something like:

include_recipe "a"

ruby_block "a_block" do
  block do
    node.default[:var] = `/usr/local/bin/stuff.sh`
  end
end

user "#{node[:var]}"

However, as it turns out the variable (var) was not passed from "ruby_block" to "user" and it remains empty. No matter what juggling i've tried to do with it i failed (or maybe i just didn't find the correct juggling method)

To the chef/ruby masters around: How do i solve Problem A? How do i solve Problem B?


Solution

  • You have already solved problem A with the Ruby block.

    Now you have to solve problem B with a similar approach:

    ruby_block "create user" do
      block do
        user = Chef::Resource::User.new(node[:var], run_context)
        user.shell '/bin/bash' # Set parameters using this syntax
        user.run_action :create
        user.run_action :manage # Run multiple actions (if needed) by declaring them sequentially
      end
    end
    

    You could also solve problem A by creating the file during the compile phase:

    execute "echo 'echo stuff' > /usr/local/bin/stuff.sh" do
      action :nothing
    end.run_action(:run)
    

    If following this course of action, make sure that:

    • /usr/local/bin exist during Chef's compile phase;
    • Either:
      • stuff.sh is executable; OR
      • Execute it through a shell (e.g.: var=`sh /usr/local/bin/stuff.sh`