0

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.

1 Answer 1

3

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.

1
  • 1
    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?" Mar 1, 2012 at 15:33

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .