Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

In my puppet script, I have 2 calls to exec that execute different batch files

exec {
   command => 'file1.bat'

exec {
   command => 'file2.bat'

If file1.bat sets an environment variable using SET

SET VAR1=someVal

it is not accessible from file2.bat, which I expected.

However, if I use SETX

SETX VAR1 someVal -m

it is still not available in file2.bat.

The only way I have been able to make the variable changes available to file2.bat is by setting the variable directly in the registry (using reg ADD) in file1.bat and then querying it (using reg QUERY) in file2.bat. Is there a better way to do this?

I cannot combine file1.bat and file2.bat into a single script because they are used independently elsewhere.

share|improve this question

Set the variable in your puppet manifest and source the batch scripts from template files with variables where you would otherwise be using an OS variable.

For example, a line from my postfix.erb file:

myhostname = <%= fqdn %>

For a great resource of actual code in practice, see Ever wondered how the Wikimedia servers are configured?. You can check out the repository with git.

Sourcing your script

    file { "file1 batch script":
            path => "/whatever/you/want/file1.bat",
            content => template("file1.erb");

    exec {
            command = file1.bat

    file { "file2 batch script":
            path => "/whatever/you/want/file2.bat",
            content => template("file2.erb");

    exec {
            command = file1.bat

Check out some ERB values as examples.

Any environment variables can be read with a script just as if you had run it directly. Also, puppet can set environment variables for a command to be run in an exec block. Check out the Puppet type refercence for more info.

As for importing variables into puppet, read up on custom facts.

share|improve this answer
thanks. I have 2 follow-ups to that. 1. Can I access system environment variables from a puppet script? and 2. Can you explain what you mean by "source the batch scripts from template files?" – Jeff Storey Mar 1 '12 at 15:33

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.