Book Image

Chef Infrastructure Automation Cookbook

By : Matthias Marschall
Book Image

Chef Infrastructure Automation Cookbook

By: Matthias Marschall

Overview of this book

Table of Contents (14 chapters)
Chef Infrastructure Automation Cookbook Second Edition
About the Author
About the Reviewers

Passing arguments to shell commands

The Chef client enables you to run shell commands by using the execute resource. However, how can you pass arguments to such shell commands? Let's assume you want to calculate a value you need to pass to the shell command in your recipe. How can you do that? Let's find out...

Getting ready

Make sure you have a cookbook called my_cookbook, and the run_list of your node includes my_cookbook, as described in the Creating and using cookbooks recipe in Chapter 1, Chef Infrastructure.

How to do it...

Let's see how we can pass Ruby variables into shell commands:

  1. Edit your default recipe. You'll pass an argument to a shell command by using an execute resource:

    mma@laptop:~/chef-repo $ subl cookbooks/my_cookbook/recipes/default.rb
    max_mem = node['memory']['total'].to_i * 0.8
    execute 'echo max memory value into tmp file' do
      command "echo #{max_mem} > /tmp/max_mem"
  2. Upload the modified cookbook to the Chef server:

    mma@laptop:~/chef-repo $ knife cookbook upload...