Book Image

Puppet Cookbook - Third Edition

Book Image

Puppet Cookbook - Third Edition

Overview of this book

Table of Contents (17 chapters)
Puppet Cookbook Third Edition
Credits
About the Authors
About the Reviewers
www.PacktPub.com
Preface
Index

Passing arguments to shell commands


If you want to insert values into a command line (to be run by an exec resource, for example), they often need to be quoted, especially if they contain spaces. The shellquote function will take any number of arguments, including arrays, and quote each of the arguments and return them all as a space-separated string that you can pass to commands.

In this example, we would like to set up an exec resource that will rename a file; but both the source and the target name contain spaces, so they need to be correctly quoted in the command line.

How to do it…

Here's an example of using the shellquote function:

  1. Create a shellquote.pp manifest with the following command:

    $source = 'Hello Jerry'
    $target = 'Hello... Newman'
    $argstring = shellquote($source, $target)
    $command = "/bin/mv ${argstring}"
    notify { $command: }
    
  2. Run Puppet:

    $ puppet apply shellquote.pp 
    ...
    Notice: /bin/mv "Hello Jerry" "Hello... Newman"
    Notice: /Stage[main]/Main/Notify[/bin/mv "Hello Jerry" "Hello...