Deepak
Deepak

Reputation: 575

What is the best way to run a script block in puppet exec?

Executing simple commands is pretty straight forward. But what is the best way to write, if the command is a script block instead of a one-liner, e.g.:

exec {
    command => 'for i in vars
                do
                    echo $i
                    more-statements
                done'; 
}

A couple of ways that I can think of are:

Are there any other options?

Upvotes: 1

Views: 2856

Answers (1)

Peter Souter
Peter Souter

Reputation: 5190

If you do want to write it as a script block, I generally do it in the way you described:

exec { 'Multi line exec':
    command => "source foo.sh
echo 'bar'
touch /var/tmp/baz
",
    path => '/usr/bin:/usr/sbin:/bin:/usr/local/bin',
    provider => 'shell',
}

Generally, I advise against doing this, because it becomes a little complicated with escaping things (and the difference between a puppet usage of $ variables and actual shell $ variables...

Depending on the complexity of the script, and if you want to run it with tools other than Puppet, I'd generally create it as a file-on-disk with a file type, then have an exec run the scripts.

Or even better, split the shells parts into separate exec resources with dependencies and unless parameters.

Upvotes: 4

Related Questions