How to pass a variable to another recipe?

Hello Chef,

I have a recipe that copies a file over from
/files/default/myscript.rb to the node locally. Right now I have
this hard coded where it’s using the name listed in the recipe, but I would
like to make this use a variable listed in another recipe.

So the end goal would be a recipe like launch_script.rb would call the
script copy the file over using the copy recipe, only based upon a variable
name listed in the launch_script.rb recipe.

From what I found, this is only possible using a node runtime like

node.run_state[:script_1] = "check_httpd.rb"
filename = node.run_state[:script_1]

But this still requires me to add the "node.run_state[:script_1] added to
the copy recipe.

Is there any way to send a variable to a recipe without hardcoding the name
in the recipe? Or what is really the correct Chef way? Create a new
resource?

Thanks,
Robert

Wouldn’t an attribute suffice to share the location? You have to define where the cookbook_file is going and if you use an attribute to define the target location, it’ll be available for other recipes as well.

Steve

Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com [http://stevenmurawski.com/]
On 3/13/2015 4:14:37 PM, Robert Freiberger rfreiberger@gmail.com wrote:
Hello Chef,

I have a recipe that copies a file over from /files/default/myscript.rb to the node locally. Right now I have this hard coded where it’s using the name listed in the recipe, but I would like to make this use a variable listed in another recipe.

So the end goal would be a recipe like launch_script.rb would call the script copy the file over using the copy recipe, only based upon a variable name listed in the launch_script.rb recipe.

From what I found, this is only possible using a node runtime like

node.run_state[:script_1] = "check_httpd.rb"
filename = node.run_state[:script_1]

But this still requires me to add the "node.run_state[:script_1] added to the copy recipe.

Is there any way to send a variable to a recipe without hardcoding the name in the recipe? Or what is really the correct Chef way? Create a new resource?

Thanks,
Robert

I have that defined in the recipe, but I want to make the steps easier by
only changing one instance of the script name. Here's what I'm thinking
(also not even sure it's recommended for the Chef way).

/recipes/default.rb

  1. Defines a runstate as follows, this is the script name the user wants to
    copy to the host.

node.run_state[:script_1] = "check_httpd.rb"

  1. Includes the second recipe that would copy over the script defined in
    step 1.

include mycookbook::copyscript

/recipes/copyscript.rb

  1. Would first assign the node run_state to the filename.

filename = node.run_state[:script_1]

  1. Copy this from the cookbook_file resource.

cookbook_file "/bin/tools/#{filename}" do
source "#{filename}"
mode "0755"
end

The problem I'm having is that I still need to define the name "script_1"
in my copy recipe. So it's not able to share across other recipes that may
want to use this recipe. Ideally I would like to somehow call copyscript.rb
and send the variable name when I include it in a recipe. But I think this
is better suited for a lwrp? Apologies as it was confusing to explain over
word.

Thanks,
Robert

On Fri, Mar 13, 2015 at 2:29 PM Steven Murawski steven.murawski@gmail.com
wrote:

Wouldn't an attribute suffice to share the location? You have to define
where the cookbook_file is going and if you use an attribute to define the
target location, it'll be available for other recipes as well.

Steve

Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com

On 3/13/2015 4:14:37 PM, Robert Freiberger rfreiberger@gmail.com wrote:
Hello Chef,

I have a recipe that copies a file over from /files/default/myscript.rb
to the node locally. Right now I have this hard coded where it's using the
name listed in the recipe, but I would like to make this use a variable
listed in another recipe.

So the end goal would be a recipe like launch_script.rb would call the
script copy the file over using the copy recipe, only based upon a variable
name listed in the launch_script.rb recipe.

From what I found, this is only possible using a node runtime like

node.run_state[:script_1] = "check_httpd.rb"
filename = node.run_state[:script_1]

But this still requires me to add the "node.run_state[:script_1] added to
the copy recipe.

Is there any way to send a variable to a recipe without hardcoding the
name in the recipe? Or what is really the correct Chef way? Create a new
resource?

Thanks,
Robert

Robert, you might try something with attributes. I'm still a little
confused as to what you're actually trying to accomplish.

attributes/default.rb

default['myapp']['custom_scripts_path'] = '/bin/tools'
default['myapp']['script_names']['check_http'] = 'check_http.rb'
default['myapp']['script_names']['check_disk'] = 'check_disk.rb'

recipes/copyscript.rb

target = File.join(node['myapp']['custom_scripts_path'],
node['myapp']['script_names']['check_http'])
cookbook_file target do
source node['myapp']['script_names']['check_http']
mode "0755"
end

Or if you have many scripts that will need to be copied

node['myapp']['script_names'].each do |k, script|
target = File.join(node['myapp']['custom_scripts_path'], script)
cookbook_file target do
source script
mode "0755"
end
end

On Fri, Mar 13, 2015 at 5:18 PM, Robert Freiberger rfreiberger@gmail.com
wrote:

I have that defined in the recipe, but I want to make the steps easier by
only changing one instance of the script name. Here's what I'm thinking
(also not even sure it's recommended for the Chef way).

/recipes/default.rb

  1. Defines a runstate as follows, this is the script name the user wants
    to copy to the host.

node.run_state[:script_1] = "check_httpd.rb"

  1. Includes the second recipe that would copy over the script defined in
    step 1.

include mycookbook::copyscript

/recipes/copyscript.rb

  1. Would first assign the node run_state to the filename.

filename = node.run_state[:script_1]

  1. Copy this from the cookbook_file resource.

cookbook_file "/bin/tools/#{filename}" do
source "#{filename}"
mode "0755"
end

The problem I'm having is that I still need to define the name "script_1"
in my copy recipe. So it's not able to share across other recipes that may
want to use this recipe. Ideally I would like to somehow call copyscript.rb
and send the variable name when I include it in a recipe. But I think this
is better suited for a lwrp? Apologies as it was confusing to explain over
word.

Thanks,
Robert

On Fri, Mar 13, 2015 at 2:29 PM Steven Murawski steven.murawski@gmail.com
wrote:

Wouldn't an attribute suffice to share the location? You have to
define where the cookbook_file is going and if you use an attribute to
define the target location, it'll be available for other recipes as well.

Steve

Steven Murawski
Community Software Development Engineer @ Chef
Microsoft MVP - PowerShell
http://stevenmurawski.com

On 3/13/2015 4:14:37 PM, Robert Freiberger rfreiberger@gmail.com wrote:
Hello Chef,

I have a recipe that copies a file over from /files/default/myscript.rb
to the node locally. Right now I have this hard coded where it's using the
name listed in the recipe, but I would like to make this use a variable
listed in another recipe.

So the end goal would be a recipe like launch_script.rb would call the
script copy the file over using the copy recipe, only based upon a variable
name listed in the launch_script.rb recipe.

From what I found, this is only possible using a node runtime like

node.run_state[:script_1] = "check_httpd.rb"
filename = node.run_state[:script_1]

But this still requires me to add the "node.run_state[:script_1] added to
the copy recipe.

Is there any way to send a variable to a recipe without hardcoding the
name in the recipe? Or what is really the correct Chef way? Create a new
resource?

Thanks,
Robert