Puppet Windows Automation

92 views
Skip to first unread message

Thomas Bartlett

unread,
Aug 27, 2015, 9:21:52 AM8/27/15
to Puppet Users
Hi Guys,

So I've been working with puppet on windows and I think my approach is all wrong. I've been using the puppetlabs/powershell module to run commands, however I'm having difficulty with exit codes. Primarily puppet expects exit codes to denote success/failure, whereas powershell is returning objects (and giving a 0 exit code regardless of result).

I'm automating the install of old bits of software, so getting meaningful answers out of the installers is pretty difficult, this means that the scripts are a bit ugly and not very idempotent. Typically I have to check a log file to find out if the install actually worked.

Are there any examples out there of windows puppet automation that makes heavy use of the powershell module?

Cheers,

Tom

Peter Kristolaitis

unread,
Aug 27, 2015, 10:27:04 AM8/27/15
to puppet...@googlegroups.com
We solve this issue by doing (kind of ugly) stuff like this in our modules:

unless  =>  'if ( ! ( Get-Service mcollectived ) ) { exit 1 }',

If you don't like that syntax, you may be able to use the $? or $LastExitCode variables that get set by PowerShell (I haven't tested this, however).  Both of those have non-obvious gotchas.  A good writeup on error handling in PS (not Puppet-specific) is here:  http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/12/powershell-error-handling-and-why-you-should-care.aspx

- Peter
--
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/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Thomas Bartlett

unread,
Aug 27, 2015, 12:06:13 PM8/27/15
to Puppet Users
Nice one, I'll give that a go. You don't happen to know how I can set a variable to equal the result of a powershell command do you? I need to use the hostname of the machine as a parameter for another command. At the minute I'm using hard-coding which is obviously a cardinal sin.

Peter Kristolaitis

unread,
Aug 27, 2015, 12:20:29 PM8/27/15
to puppet...@googlegroups.com
PowerShell is largely based on the syntax of the ksh shell, so most constructs that work in ksh will work in PS as well.

In this case:

$FOO=(hostname)

That will interpolate anywhere, not just during variable assignment, so you can do stuff like:

Some-CmdLet -Host (hostname)

If you're familiar with the use of backticks in bash the mechanism is exactly the same, just different syntax.

Fraser Goffin

unread,
Aug 28, 2015, 2:22:04 PM8/28/15
to Puppet Users
Whilst there are definitely some uses cases where executing scripts in either Powershell or via an Exec resource is the way to go, in my opinion you should really be thinking of using the higher level resource abstractions. That way you don't need to deal with the implementation details and you are leveraging the true power of a declarative language. Otherwise it's just glorified scripting and to some extent you may as well just run Powershell natively. Using the RAL, you will also undoubtedly find that it's not too difficult to abstract away the OS specifics as well, especially if you plug in Hiera. So go on, ... start thinking in terms of resources rather than code, you know you want to ;-)

Regards

Fraser.

Rob Reynolds

unread,
Aug 29, 2015, 10:17:25 AM8/29/15
to puppet...@googlegroups.com
On Thu, Aug 27, 2015 at 9:26 AM, Peter Kristolaitis <alt...@alter3d.ca> wrote:
We solve this issue by doing (kind of ugly) stuff like this in our modules:

unless  =>  'if ( ! ( Get-Service mcollectived ) ) { exit 1 }',

If you don't like that syntax, you may be able to use the $? or $LastExitCode variables that get set by PowerShell (I haven't tested this, however).  Both of those have non-obvious gotchas.  A good writeup on error handling in PS (not Puppet-specific) is here:  http://blogs.technet.com/b/heyscriptingguy/archive/2011/05/12/powershell-error-handling-and-why-you-should-care.aspx

Another great writeup for PowerShell confusion is http://joshua.poehls.me/2012/powershell-script-module-boilerplate/

The tl;dr of PowerShell scripts is that parser errors (incorrect PowerShell) will always exit with 0, so test your scripts with PowerShell and look at the last exit code.

Another is that even setting $ErrorActionPreference = 'Stop' (to catch non-terminating errors) doesn't appear to catch external command errors (versus powershell function calls):

Inline image 1

PowerShell.exe -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -Command "$ErrorActionPreference = 'Stop';reg /query bob; Write-Host 'yo'"
 


- Peter



On 08/27/2015 08:38 AM, Thomas Bartlett wrote:
Hi Guys,

So I've been working with puppet on windows and I think my approach is all wrong. I've been using the puppetlabs/powershell module to run commands, however I'm having difficulty with exit codes. Primarily puppet expects exit codes to denote success/failure, whereas powershell is returning objects (and giving a 0 exit code regardless of result).

I'm automating the install of old bits of software, so getting meaningful answers out of the installers is pretty difficult, this means that the scripts are a bit ugly and not very idempotent. Typically I have to check a log file to find out if the install actually worked.

Are there any examples out there of windows puppet automation that makes heavy use of the powershell module?

Cheers,

Tom
--
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/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

--
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.



--
Rob Reynolds
Developer, Puppet Labs

PuppetConf 2015 is coming to Portland, Oregon! Join us October 5-9.
Register now to take advantage of the Final Countdown discount save $149!

Rob Reynolds

unread,
Aug 29, 2015, 10:22:52 AM8/29/15
to puppet...@googlegroups.com
On Thu, Aug 27, 2015 at 7:38 AM, Thomas Bartlett <thomas.bar...@gmail.com> wrote:
Hi Guys,

So I've been working with puppet on windows and I think my approach is all wrong. I've been using the puppetlabs/powershell module to run commands, however I'm having difficulty with exit codes. Primarily puppet expects exit codes to denote success/failure, whereas powershell is returning objects (and giving a 0 exit code regardless of result).

I'm automating the install of old bits of software, so getting meaningful answers out of the installers is pretty difficult, this means that the scripts are a bit ugly and not very idempotent. Typically I have to check a log file to find out if the install actually worked.

If you are automating the install of software, you should look at using either the built-in package resource or one of the Windows package providers, e.g. Chocolatey[1]. It also has a pre-built, ready to go internal package server[2] so you can put your packages somewhere in your network.

Lastly, if you are looking at the Chocolatey route, note that the provider is almost finished with the install of choco on the machine[3] and using it as a provider for packages in the same run.[4] 



 

Are there any examples out there of windows puppet automation that makes heavy use of the powershell module?

Cheers,

Tom

--
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/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--

Rob Reynolds

unread,
Aug 29, 2015, 10:24:17 AM8/29/15
to puppet...@googlegroups.com
On Sat, Aug 29, 2015 at 9:22 AM, Rob Reynolds <r...@puppetlabs.com> wrote:


On Thu, Aug 27, 2015 at 7:38 AM, Thomas Bartlett <thomas.bar...@gmail.com> wrote:
Hi Guys,

So I've been working with puppet on windows and I think my approach is all wrong. I've been using the puppetlabs/powershell module to run commands, however I'm having difficulty with exit codes. Primarily puppet expects exit codes to denote success/failure, whereas powershell is returning objects (and giving a 0 exit code regardless of result).

I'm automating the install of old bits of software, so getting meaningful answers out of the installers is pretty difficult, this means that the scripts are a bit ugly and not very idempotent. Typically I have to check a log file to find out if the install actually worked.

If you are automating the install of software, you should look at using either the built-in package resource

I forgot to include a link with tips for using the built-in package resource - http://docs.puppetlabs.com/puppet/latest/reference/resources_package_windows.html
 
or one of the Windows package providers, e.g. Chocolatey[1]. It also has a pre-built, ready to go internal package server[2] so you can put your packages somewhere in your network.

Lastly, if you are looking at the Chocolatey route, note that the provider is almost finished with the install of choco on the machine[3] and using it as a provider for packages in the same run.[4] 



 

Are there any examples out there of windows puppet automation that makes heavy use of the powershell module?

Cheers,

Tom

--
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/7b4f140b-05fd-4477-b8bc-62e14f54da70%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.



--
Rob Reynolds
Developer, Puppet Labs

PuppetConf 2015 is coming to Portland, Oregon! Join us October 5-9.
Register now to take advantage of the Final Countdown discount save $149!

Matthew Stone

unread,
Aug 31, 2015, 3:38:19 PM8/31/15
to Puppet Users
For instances like this, I've usually smashed everything into a one-liner using the powershell provider. So...something along the lines of:

command => " \$thing = <result of your query>; Write-Host \"Puppet ${thingamajig} is a $\thing\""

As can be seen above, it can start looking pretty ugly, especially if the commands you are executing are a bit longwinded.  You can get a lot more power out of writing custom types/providers and it make for a lot cleaner looking manifest, but there aren't a lot of publicly available windows-specific ones in the wild right now.  I think most people tend to steer towards DSC for that and hope for the best.

Rob Reynolds

unread,
Aug 31, 2015, 10:15:32 PM8/31/15
to puppet...@googlegroups.com


On Monday, August 31, 2015, Matthew Stone <ma...@souldo.net> wrote:
For instances like this, I've usually smashed everything into a one-liner using the powershell provider. So...something along the lines of:

command => " \$thing = <result of your query>; Write-Host \"Puppet ${thingamajig} is a $\thing\""

As can be seen above, it can start looking pretty ugly, especially if the commands you are executing are a bit longwinded.  You can get a lot more power out of writing custom types/providers and it make for a lot cleaner looking manifest, but there aren't a lot of publicly available windows-specific ones in the wild right now. 

I thought about a blog post series specifically about types and providers in Windows.
 
 I think most people tend to steer towards DSC for that and hope for the best.


On Thursday, August 27, 2015 at 9:06:13 AM UTC-7, Thomas Bartlett wrote:
Nice one, I'll give that a go. You don't happen to know how I can set a variable to equal the result of a powershell command do you? I need to use the hostname of the machine as a parameter for another command. At the minute I'm using hard-coding which is obviously a cardinal sin.

--
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.

Matthew Stone

unread,
Sep 1, 2015, 4:00:17 PM9/1/15
to Puppet Users
do it do it do it do it!

I put one together for windowsfeature and wanted to submit a PR, but I'm unseasoned at rspec and haven't been able to put together a solid block of tests.  Any help or resources in that department is more than appreciated. :)
Reply all
Reply to author
Forward
0 new messages