How to pass the variable from my shell script to my chef template recipe?

chef bash resource
chef execute
chef run python script
chef notify
chef template variables hash
chef file resource
chef template if else
chef bash resource return value

I have a shell script whose output is a variable. I am running that shell script through a bash resource in a recipe. I want to be able to access that variable through my chef template. Is there anyway to do this?

bash resource:
  "My script which outputs a variable"

I want to output this variable to template resource.

Save the output as node attribute if you want to use across Multiple recipes.

ruby_block 'get output' do
   block do

template '/tmp/' do
   source 'test.erb'
   owner 'root'
   group 'root'
   mode '0644'
   variables( lazy {{:config_var => node['return_val']}})

Kitchen Output:-

     * ruby_block[get output] action run
       - execute the ruby block reload client config
     * template[/tmp/] action create
       - create new file /tmp/
       - update content in file /tmp/ from none to 2c9c1f
       --- /tmp/  2016-08-05 13:20:21.678786687 +0000
       +++ /tmp/.chef-new.sh20160805-12338-81wvgw   2016-08-05 13:20:21.678786687 +0000
       @@ -1 +1,2 @@
       - change mode from '' to '0644'
       - change owner from '' to 'root'
       - change group from '' to 'root'
       - restore selinux security context

The Chef documentation on the bash resource has an example of passing attribute data to a script. You should be able to adapt it to your needs  I have recipe which is of powershell commands powershell_script "run-isci" do code <<-EOH Set-Service -Name msiscsi -StartupType Automatic Start-Service msiscsi New-IscsiTargetPortal -TargetPortalAddress 165.xx.xx.xx Connect-IscsiTarget

To refine a bit on what @Mrigesh posted:

Resources don't have an output or return value per se so you need to use a lower-level helper to capture the command output. We offer a nice API called shell_out!() to most of the heavy lifting for you. The simplest option would be something like this

template '/whatever' do
  # other stuff here
  variables lazy { {config_var: shell_out!('bash /').stdout} }

This also uses the lazy{} helper to make sure the command is run at converge time instead of compile time, which might not be needed depending on your scenario.

The Chef client enables you to run shell commands by using the execute resource. Using pure Ruby in templates for conditionals and iterations Using custom bootstrap scripts to calculate a value you need to pass to the shell command in your recipe. Let's see how we can pass Ruby variables into shell commands:. Environment variable is a key way to make Chef recipe run on any particular node successfully. There are multiple ways of doing it, either manually setting them up or by using a Shell script. Setting them via recipe is what we need to perform here.

There is a simpler method, maybe not so ellegant, but tested. Within template You put

some_value = <%= `some_bash_command`.strip %>

I.e. You may want to choose the dynds_iface according to the default route on the machine, to the template you simply put:

dyndns_iface = <%= `ip r|awk '/default via/ {print $5}'`.strip %>

Back-tick does the job here. It get the shell output of the command. Strip simply removes unnecessary white spaces.

Therefore I've created a template that is the `` file with a Control Script for the CATALINA Server. Then in my recipe I have this:  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

A cookbook template is an Embedded Ruby (ERB) template that is used to dynamically generate static text files. UNIX- and Linux-based systems: A quoted 3-5 character string that defines the octal mode that is passed to chmod. A Hash of variables that are passed into a Ruby template file. "my name is #{​$ruby}" %>. The powershell_script resource has the following properties:. architecture Ruby Type:. Symbol The architecture of the process under which a script is executed. If a value is not provided, Chef Infra Client defaults to the correct value for the architecture, as determined by Ohai.

Using why-run mode to find out what a recipe might do 67 Debugging Chef client results of your last Chef run 72 Using Reporting to keep track of all your Chef client configure recipes 88 Using templates 91 Mixing plain Ruby with Chef DSL 94 Setting environment variables 118 Passing arguments to shell commands  interpreter specifies the command shell to use; cwd specifies the directory from which the command is run; code specifies the command to run. It is more common to use the script-based resource that is specific to the command shell. Chef has shell-specific resources for Bash, csh, Perl, Python, and Ruby.

template '/etc/nagios3/configures-nagios.conf' do # other parameters notifies :run, The bash script resource (which is based on the script resource) is different The resource allows arguments to be passed to the chef-client, which then in a Chef recipe, as well as any custom resources that have been added to your  These variables are defined by users. A shell script allows us to set and use our own variables within the script. Setting variables allows you to temporarily store data and use it throughout the script, making the shell script more like a real computer program. User variables can be any text string of up to 20 letters, digits, or an underscore

  • I am using vagrant vms as my nodes
  • Please remove this, use of backticks for command execution in Chef is very discouraged because it leads to really bad code.
  • Also storing large data in node attibutes is super bad :)
  • Thanks for pointing out to use shell_out instead of back ticks. It just didnt crossed my mind. :) Anyhow, for use of node attributes, I have assumed that value needs to be used across multiple recipes. But, thanks for help.