1

I originally asked this question on StackOverflow, but did not receive any working answers: https://stackoverflow.com/questions/18648713/specify-chef-provider-for-windows-feature.

I'm trying to use Chef (chef-solo) to manage my Windows Server 2008 R2 installation. Chef provides windows_feature to add roles/features to a Windows server. By default, windows_feature uses DISM to install roles (if available). However, to my knowledge, not all roles (e.g., RDS-RD-Server) are able to be added via DISM.

I could presumably use Chef::Provider::WindowsFeature::ServerManagerCmd (identified in the Windows cookbook readme: https://github.com/opscode-cookbooks/windows), but it doesn't look like that is a real class (browsing the source code there). Also, servermanagercmd is deprecated (though it would work).

I wouldn't mind even using a powershell block to add the role, but I'm having a hard time ensuring idempotence. It seems like the not_if command shell is some weird mingwin shell rather than CMD.

Here's a sample of what I've tried using powershell (doesn't work):

powershell "install_rds_server" do
  code %Q{
    Import-Module Servermanager
    Add-WindowsFeature RDS-RD-Server
  }.strip
  not_if %Q{
    powershell "Import-Module Servermanager; $check = get-windowsfeature -name RDS-RD-Server; if ($check.Installed -ne \"True\") { exit 1 }"
  }.strip
end

I've also tried the following:

windows_feature 'RDS-RD-Server' do
  provider Chef::Provider::WindowsFeature::ServerManagerCmd
end

which returns the following error:

FATAL: NameError: uninitialized constant Chef::Provider::WindowsFeature::ServerManagerCmd

What would be the recommended Chef way of adding this role?

kardeiz
  • 185
  • 1
  • 1
  • 9

2 Answers2

3

Based on the Chef documentation for LWRPs, I think the actual class name for the LWRP in the windows cookbook is

Chef::Provider::WindowsFeatureServermanagercmd

As such, you should use something like

windows_feature 'RDS-RD-Server' do
  provider Chef::Provider::WindowsFeatureServermanagercmd
end
Holger Just
  • 3,315
  • 1
  • 16
  • 23
  • Thanks! Wow, what an ugly class name--`Chef::Provider::WindowsFeatureServermanagercmd`. You are right, though, and all of the Chef Windows docs (on their website and Github) are wrong (e.g., http://docs.opscode.com/lwrp_windows.html). – kardeiz Sep 11 '13 at 12:06
  • The first (kind of) works, the second returns an error: `undefined method 'windows_feature_servermanagercmd' for Chef::Resource::WindowsFeature`. I say "kind of" works because the deprecation warning for servermanagercmd is causing a fatal error in Chef: `Expected process to exit with [0, 42, 127], but received '1003'`. – kardeiz Sep 11 '13 at 12:06
  • You are right, the "short" name doesn't work. I misinterpreted some blog posts and have subsequently removed the wrong information now. Why the remote call from the provider still fails is beyond my knowledge as I have never used Chef on Windows (or even used Windows at all in the last 2 years :) You might want to debug [the process invocation](https://github.com/opscode-cookbooks/windows/blob/master/providers/feature_servermanagercmd.rb#L25-L27) yourself (and maybe provide a patch for the cookbook). – Holger Just Sep 11 '13 at 12:26
  • Thanks again for your help. I'm accepting your answer because it answered my primary question, but I also posted as an answer the solution I ended up using. – kardeiz Sep 15 '13 at 17:49
0

Holger Just's solution works, more or less, though the servermanagercmd.exe deprecation message causes some issues. Here is how I ended up resolving the issue:

ps_64 = 'C:\Windows\sysnative\WindowsPowershell\v1.0\powershell.exe'

powershell "install_rds_server" do
  code %Q{
    Import-Module Servermanager
    Add-WindowsFeature RDS-RD-Server
  }.strip
  not_if %Q{
    #{ps_64} "Import-Module Servermanager; $check = get-windowsfeature -name RDS-RD-Server; if ($check.Installed -ne 'True') { exit 1 }"
  }.strip
end

My initial Powershell based solution wasn't working because the generic powershell command was launching the 32-bit Powershell. This solution is still very hacky, but I prefer it over using the deprecated servermanagercmd.exe.

kardeiz
  • 185
  • 1
  • 1
  • 9