Passing variables between chef resources

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`