Custom facts question on windows 2008 R2

1,104 views
Skip to first unread message

Marco Parra D.

unread,
May 10, 2012, 11:40:15 AM5/10/12
to puppet...@googlegroups.com
Hi guys,
I'm trying to define a custom fact on windows puppet agent, I'm using
the version 2.7.14. I made a powershell that checks for the state of a
service and return true or false.
If I execute it on powershell I got this:

PS C:\programdata\PuppetLabs\facter\ext> .\udp_status.ps1
udp_status=true

But If I ran facter I didn't see the facter and value for this custom
fact....

I put the .ps1 file on C:\programdata\PuppetLabs\facter\ext but I can't
see the value for the fact...

What am I doing wrong?....

thank you in advance...

Regards

mparrad

unread,
May 10, 2012, 4:08:13 PM5/10/12
to puppet...@googlegroups.com
Hi guys, I realized that If I make the file udp_status.rb and I put on D:\Program Files (x86)\Puppet Labs\Puppet\facter\lib\facter, that's where I installed puppet from the MSI file, and I use the Powershell script that I made, I got the value that I expect when I run facter:

D:\>facter udp_status
true

Now, My question will be: How can I use thi fact on a resource exec, Is this possible?, I tried using with onlyif => "$udp_status" but I got several errors about convertions and another stuffs...

What would be the correct way to use a fact on a resource EXEC on ​​windows?

any help would be appreciated

best regards

Nan Liu

unread,
May 10, 2012, 8:22:59 PM5/10/12
to puppet...@googlegroups.com
On Thu, May 10, 2012 at 1:08 PM, mparrad <marco....@gmail.com> wrote:
> Hi guys, I realized that If I make the file udp_status.rb and I put on
> D:\Program Files (x86)\Puppet Labs\Puppet\facter\lib\facter, that's where I
> installed puppet from the MSI file, and I use the Powershell script that I
> made, I got the value that I expect when I run facter:
>
> D:\>facter udp_status
> true
>
> Now, My question will be: How can I use thi fact on a resource exec, Is this
> possible?, I tried using with onlyif => "$udp_status" but I got several
> errors about convertions and another stuffs...

Facts are provided to the master, so in this case simply:

if $udp_status {
exec { ... }
}

If the fact return false simply don't include the exec in the catalog.

HTH,

Nan

Jeff McCune

unread,
May 11, 2012, 12:08:28 AM5/11/12
to puppet...@googlegroups.com
On Thu, May 10, 2012 at 1:08 PM, mparrad <marco....@gmail.com> wrote:
Hi guys, I realized that If I make the file udp_status.rb and I put on D:\Program Files (x86)\Puppet Labs\Puppet\facter\lib\facter, that's where I installed puppet from the MSI file, and I use the Powershell script that I made, I got the value that I expect when I run facter:

That's great you were able to work this out.  Would you mind pasting the copy of udp_status.rb into gist.github.com to share?  This will help us understand what your goal is.
 
D:\>facter udp_status
true

Now, My question will be: How can I use thi fact on a resource exec, Is this possible?, I tried using with onlyif => "$udp_status" but I got several errors about convertions and another stuffs...

The onlyif parameter of the exec resource is meant to be set to a shell command.  If the exit status is OK (usually a return code of 0 on unix) then the exec resource on the system is considered out of sync with the catalog.  Puppet executes the command specified with the command parameter when the resource is out of sync.

It's definitely possible to use the fact with an exec resource, or any resource Puppet can manage.  Since it's a variable you can use it in any string in the manifest files.  You can also use it in a template with the template() function.
 
What would be the correct way to use a fact on a resource EXEC on ​​windows?

What are you trying to accomplish?  Since you were using onlyif, perhaps execute a command only if a udp port is open?
 
-Jeff

Marco Parra D.

unread,
May 11, 2012, 9:07:20 AM5/11/12
to puppet...@googlegroups.com
Hi Jeff,
On 11-05-2012 0:08, Jeff McCune wrote:
On Thu, May 10, 2012 at 1:08 PM, mparrad <marco....@gmail.com> wrote:
Hi guys, I realized that If I make the file udp_status.rb and I put on D:\Program Files (x86)\Puppet Labs\Puppet\facter\lib\facter, that's where I installed puppet from the MSI file, and I use the Powershell script that I made, I got the value that I expect when I run facter:

That's great you were able to work this out.  Would you mind pasting the copy of udp_status.rb into gist.github.com to share?  This will help us understand what your goal is.
 
D:\>facter udp_status
true
I try to verify is a service was installed, this is my rb file for the fact udp_status.rb:

Facter.add("udp_status") do
  setcode do
    Facter::Util::Resolution.exec('C:\Windows\Sysnative\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy remotesigned -File D:\installs\Scripts\check_udp.ps1')
  end
end

And my check_udp.ps1 file is:

Function Check-UDP()
{
$Service=Get-Service cscudpprocessor -ComputerName localhost
if ($Service.status -eq "Running")
{
    write-host "true"
}
else
    {       
    Write-host "false"
    }
}

Check-UDP



Now, My question will be: How can I use thi fact on a resource exec, Is this possible?, I tried using with onlyif => "$udp_status" but I got several errors about convertions and another stuffs...

The onlyif parameter of the exec resource is meant to be set to a shell command.  If the exit status is OK (usually a return code of 0 on unix) then the exec resource on the system is considered out of sync with the catalog.  Puppet executes the command specified with the command parameter when the resource is out of sync.

It's definitely possible to use the fact with an exec resource, or any resource Puppet can manage.  Since it's a variable you can use it in any string in the manifest files.  You can also use it in a template with the template() function.
 
What would be the correct way to use a fact on a resource EXEC on ​​windows?

What are you trying to accomplish?  Since you were using onlyif, perhaps execute a command only if a udp port is open?

I try to make that puppet install the service UDP, if is already installed do nothing.... I think I acomplish that  making the next thing, when the service is installed  the next time that puppet agent run, I got a error from puppet because the execution form installutil.exe  returns a 255 code; My idea was originally use fact to acomplish that, but I couldn't... this is my resource definition now:

exec { 'Install-UDP':
        command => 'C:\\Windows\\Microsoft.NET\\Framework\\v4.0.30319\\InstallUtil.exe /username=DOMAIN\username /password=somepassword /unattended d:\\services\\CommonUdpService\\bin\\MyApplication.Common.UDP.exe',
        timeout => 0,
        returns => ['0','255'],
        provider => windows,
        path => ['C:\ProgramData\PuppetLabs\facter\ext'],
        onlyif => 'C:\\Windows\\Sysnative\\WindowsPowerShell\\v1.0\\powershell.exe -ExecutionPolicy remotesigned -File D:\\installs\\Scripts\\check_udp.ps1',
    }

My service isn't the Protocol UDP, its only the name of the service....


Thank you

 
-Jeff

--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.


Best Regards..!

Marco Parra D.

unread,
May 14, 2012, 3:29:55 PM5/14/12
to puppet...@googlegroups.com
Hi Nan:
I tested as you said, I put the If statement previous to the resource
exec using the udp_status fact , and It works perfect..


Thank you a lot...

Best Regards

Reply all
Reply to author
Forward
0 new messages