Ask Your Question

How to set and use env variables in the same windows agent run?

asked 2016-02-21 09:41:24 -0500

Raf gravatar image

I have a Puppet agent on a Windows machine run as a service under the LocalSystem account. I am wanting to install some software on this machine that requires a particular environment variable to be set before the software can be installed. I have created a manifest that sets the env variable and then uses the 'package' resource type to install the software. However, the software cannot see the variable, thus does not get installed properly.

I have tried creating the env variable using different approaches (setx/set using 'exec' and using the '/badgerious/windows_env' module), all to no avail.

Any help will be appreciated.



edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted

answered 2016-02-22 09:47:56 -0500

Areson gravatar image

Unfortunately, setting the environment variable isn't enough. Since the Puppet agent is launching your installer, the installer inherits the environment variables that Puppet knows about, which is determined when the agent started. Normally this means that you'll have to restart the agent in order for it to see new environment variables. Depending on how you initially set the environment variable, it may be that it is no longer visible to the agent when it restarts.

Since the agent is running under Ruby, the probably really is that you need Ruby to refresh its environment variables before starting your installer. You'll probably have to write a provider to refresh Ruby's environment variables and include it as a resource in your manifest that runs before your installer.

Unfortunately, last time I looked around for such a provider I couldn't find one (we have a similar situation with our installer). We wrote one, but we haven't been approved to release it, so I can't point you to a finished product. However, Ruby keeps its environment variables in a hash called ENV which is globally accessible. If you can scrape Windows for the current environment variables (we pulled them from the registry, but there may be other ways or doing it) you can update the ENV hash, at which point your installer should be able to see your environment variable when it runs. There are a few Stackoverflow articles out there for bits and pieces of this and hopefully that will get you on your way.

edit flag offensive delete link more


Thanks for your help. I will try and see what I can come up with. No doubt I will need further help as I am both a puppet and ruby noob :-) Cheers Raf

Raf gravatar imageRaf ( 2016-02-22 11:42:46 -0500 )edit

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower


Asked: 2016-02-21 09:41:24 -0500

Seen: 202 times

Last updated: Feb 22 '16