1

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:

  • Write the script block with proper escaping to get it to work (not a very tidy option)
  • Write the script block in file.sh and execute it in command

Are there any other options?

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

1 Comment

It looks like you have mismatching " and ' in there

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.