- From: Andrea Campi <
>
- To:
- Subject: [chef] Re: Consuming a resource/provider from within another provider
- Date: Fri, 13 Apr 2012 11:17:15 +0200
On Thu, Apr 12, 2012 at 1:49 AM, James Dawson
<
>
wrote:
>
Hi,
>
>
I'm trying to extend the 'Feature' resource in the Windows cookbook to also
>
support using the Powershell module that is available in Windows 2008 R2.
>
>
I was intending on using the Powershell resource/provider to actually
>
execute the necessary commands, but am struggling to get this working - I've
>
tried various combinations without much success but am new to Ruby so feel
>
like I'm stumbling in the dark really.
>
>
Here is how I'm trying to use it in my implementation of the 'installed?'
>
method:
>
>
def installed?
>
poshOutFile = Tempfile.new('feature_smm')
>
@installed ||= begin
>
Chef::Resource::Powershell.new("query feature") do
>
code <<-EOH
>
import-module ServerManager
>
$feature = Get-WindowsFeature
>
"
"
>
Set-Content -Path "#{poshOutFile.path}" -Value $feature.Installed
>
EOH
>
end
>
>
!(poshOutFile.nil?) && ((poshOutFile.read).casecmp 'true')
>
end
>
end
>
>
This particular attempt doesn't throw any errors but nothing actually
>
happens, presumably because I'm only only instantiating the resource and not
>
actually executing it. Other attempts typically resulted in 'method_missing'
Correct. You need:
resource.run_action(:create)
Note that this is in principle, I haven't taken the time do understand
the details—but you seem to be on right way to figure it out anyway ;)
>
As a side question, I didn't see any way to pass information back from the
>
invoked Powershell command (standard out doesn't seem to be accessible in
>
the same way as with the 'shell_out' provider - or have I missed something
>
here too?) so have resorted to using a file to store the result - is this
>
the best approach?
You could probably take the guts out of that LWRP, create a helper in
libraries, and use that instead of the resource.
That's the approach I use when I need a return value; YMMV.
Andrea
Archive powered by MHonArc 2.6.16.