Can Windows environment variables be referenced by 'exec'

317 views
Skip to first unread message

Leroy Tennison

unread,
Oct 9, 2015, 3:53:47 PM10/9/15
to Puppet Users
Puppet v3.4.3

What I was specifically hoping to do was:

exec { 'test' :
command => "%comspec% /c (a Windows command-line program with parameters)",
}

to avoid having to determine where Windows was installed.  Although 'puppet parser vaildate init.pp' doesn't return an error the client agent reports "...not qualified and no path was specified... " (I realize the parser doesn't catch everything)

I tried variants %%comspec%% and \%comspec\% (puppet parser complained about this) but nothing works.  If this is possible please provide an example of the correct syntax, thanks.

Now for the more general question, are environment variables defined on Windows able to be used at all on the puppet server when specifying an agent action?

jcbollinger

unread,
Oct 12, 2015, 8:54:10 AM10/12/15
to Puppet Users


Yes and no.  The docs for Exec's 'windows' provider say this:

Execute external binaries on Windows systems. As with the posix provider, this provider directly calls the command with the arguments given, without passing it through a shell or performing any interpolation. To use shell built-ins — that is, to emulate the shell provider on Windows — a command must explicitly invoke the shell:

exec {'echo foo':
  command
=> 'cmd.exe /c echo "foo"',
}

The explanation of how to use shell built-ins should apply also to using the shell's syntax for referencing environment variables.  Note also that you can use powershell for such purposes instead of cmd.exe, and many do so.


John

Rob Reynolds

unread,
Oct 12, 2015, 10:53:20 AM10/12/15
to puppet...@googlegroups.com
On Mon, Oct 12, 2015 at 5:54 AM, jcbollinger <John.Bo...@stjude.org> wrote:

On Friday, October 9, 2015 at 2:53:47 PM UTC-5, Leroy Tennison wrote:
Puppet v3.4.3

At 3.4.3 your options are a bit more limited. For instance you are on 32 bit Puppet/Ruby b/c we didn't offer x64 Puppet/Ruby until 3.7.0. Or the $::system32 fact[1]. We'll get into why this is important below.


 

What I was specifically hoping to do was:

exec { 'test' :
command => "%comspec% /c (a Windows command-line program with parameters)",
}

to avoid having to determine where Windows was installed.  Although 'puppet parser vaildate init.pp' doesn't return an error the client agent reports "...not qualified and no path was specified... " (I realize the parser doesn't catch everything)

I tried variants %%comspec%% and \%comspec\% (puppet parser complained about this) but nothing works.  If this is possible please provide an example of the correct syntax, thanks.

Now for the more general question, are environment variables defined on Windows able to be used at all on the puppet server when specifying an agent action?

If you offer those environment variables up as facts, they will be available. Environment variables are usually available to whatever you are running, but they may or may not be available for Puppet. So having a custom fact that finds ComSpec and returns it as a custom fact will allow you to 

exec { 'test' :
command => "${comspec} /c (a Windows command-line program with parameters)",
}
 
But you are likely to run into file system redirection if you don't provide some compensation for it. This may adjust your approach. Read below.



Yes and no.  The docs for Exec's 'windows' provider say this:

Execute external binaries on Windows systems. As with the posix provider, this provider directly calls the command with the arguments given, without passing it through a shell or performing any interpolation. To use shell built-ins — that is, to emulate the shell provider on Windows — a command must explicitly invoke the shell:

exec {'echo foo':
  command
=> 'cmd.exe /c echo "foo"',
}

The explanation of how to use shell built-ins should apply also to using the shell's syntax for referencing environment variables.  Note also that you can use powershell for such purposes instead of cmd.exe, and many do so.

Let's say for a second that your Windows install is in C:\Windows. Strictly using cmd.exe or %comspec% is going to end up using C:\Windows\SysWOW64\cmd.exe even though it appears it is using C:\Windows\System32\cmd.exe. This is due to Puppet 3.4.3 being subject to Windows' File System Redirection[2]. I wrote a post on this awhile ago[3] that would be worth a read. The PowerShell provider already compensates for this to use the native paths. We've written up in the docs how you can compensate for redirection even if you don't have the $system32 fact[4].

So while you can provide comspec as a fact, you will want to do the compensation in the custom fact to ensure that it uses a 64 bit paths and sysnative on 64 bit systems.

 


John

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/faa328e8-7382-4f65-8a87-d3361b076890%40googlegroups.com.

For more options, visit https://groups.google.com/d/optout.



--
Rob Reynolds
Developer, Puppet Labs

PuppetConf 2015 is this week! Join us via the live stream

Leroy Tennison

unread,
Oct 12, 2015, 11:09:33 AM10/12/15
to Puppet Users
Thanks for the replies, learned more than I imagined.


On Friday, October 9, 2015 at 2:53:47 PM UTC-5, Leroy Tennison wrote:

Josh Cooper

unread,
Jan 6, 2016, 12:17:42 AM1/6/16
to puppet...@googlegroups.com
On Mon, Oct 12, 2015 at 8:09 AM, Leroy Tennison <leroy.t...@gmail.com> wrote:
Thanks for the replies, learned more than I imagined.

On Friday, October 9, 2015 at 2:53:47 PM UTC-5, Leroy Tennison wrote:
Puppet v3.4.3

What I was specifically hoping to do was:

exec { 'test' :
command => "%comspec% /c (a Windows command-line program with parameters)",
}

to avoid having to determine where Windows was installed.

One useful trick on Windows is to set the `path` parameter of the exec resource to the value of the `path` fact so that you don't have to know where Windows was installed:

exec { 'test':
  command => "cmd.exe /c echo hello",
  path => $path
}

It does assume you trust the `path` fact. For example, a user less privileged than puppet shouldn't be able to manipulate the system in a way that causes the `path` fact to contain user specified directories.

 Although 'puppet parser vaildate init.pp' doesn't return an error the client agent reports "...not qualified and no path was specified... " (I realize the parser doesn't catch everything)

I tried variants %%comspec%% and \%comspec\% (puppet parser complained about this) but nothing works.  If this is possible please provide an example of the correct syntax, thanks.

Now for the more general question, are environment variables defined on Windows able to be used at all on the puppet server when specifying an agent action?

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Josh

--
Josh Cooper
Developer, Puppet Labs
Reply all
Reply to author
Forward
0 new messages