How can Puppet query from Sybase - Facter or Hiera?

103 views
Skip to first unread message

Stella

unread,
Oct 20, 2014, 1:27:57 PM10/20/14
to puppet...@googlegroups.com
Hi,

I am using Puppet 3.62. Trying to figure out what is the best way to connect to my sybase database to query machine information.

Which is better:

1. Define facts that will connect to my sybase and do the query.

2. Use Hiera to connect to my sybase and do the query.

Anyone has any example to share?

Thanks!

Stella

Johan De Wit

unread,
Oct 20, 2014, 5:36:50 PM10/20/14
to puppet...@googlegroups.com
Hi Stella,

my first thought goes to implement a ENC script. Since you are already keeping inventory data in a database, you could keep everything in one place.  If it is possible to add puppet support, in practice, make it possible to assign puppet classes to your nodes in your sybase setup.

If that is not possible, I would look at a hiera backend getting the info from your sybase database.

Keep in mind that it is the puppet master that needs the info from your sybase database, so facts is not really an option. 

(technically you can, but then every node needs access to your database, but your master can get the needed info also, only keep in mind your master identifies the nodes with the 'clientcert' or 'certname'.

Personally, I would go for the ENC first place, hiera in second, but facts is a no-go.

hth a bit

jo  
--
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/69fa9702-708a-442c-803c-1b3bead7ab2f%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.


-- 
Johan De Wit

Open Source Consultant

Red Hat Certified Engineer              (805008667232363)
Puppet Certified Professional 2013/2014 (PCP0000006)
_________________________________________________________
 
Open-Future                 Phone     +32 (0)2/255 70 70
Zavelstraat 72              Fax       +32 (0)2/255 70 71
3071 KORTENBERG             Mobile    +32 (0)474/42 40 73
BELGIUM                     http://www.open-future.be
_________________________________________________________
 

Upcoming Events:

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-13th-october

Puppet Practitioner Training | http://www.open-future.be/puppet-practitioner-former-puppet-advanced-training-14-till-16th-october

Linux Training | http://www.open-future.be/linux-training-20-till-24th-october

Puppet Introduction Course | http://www.open-future.be/puppet-introduction-course-10th-november

Puppet Fundamentals Training | http://www.open-future.be/puppet-fundamentals-training-12-till-14th-november

Subscribe to our newsletter: http://eepurl.com/BUG8H


Stella

unread,
Oct 21, 2014, 10:20:34 AM10/21/14
to puppet...@googlegroups.com
Hi Johan,

Thank you very much for the  information.

I am not using the machine information from Sybase to do node classification in puppet.  My node classification is done by Puppet Dashboard.

My Sybase database doesn't contain any puppet class information. It contains the hostname and the workgroup the host belongs to.  What I want to return from my database query is: given a hostname, please tell me which workgroup it belongs to, then based on the workgroup value, my puppet class will decide which template to apply to this host.

That's why I think a custom fact might do this job: I will define a custom fact, which will get hostname and then connect to Sybase to determine this host's workgroup. I actually already have the perl script to do this job, but need to convert it to Ruby scirpt (puppet fact is defined in Ruby).

I am still doing research to find out what is the best approach. Will let you know.

Thanks,
Stella

Christopher Webber

unread,
Oct 21, 2014, 12:11:21 PM10/21/14
to puppet...@googlegroups.com
Assuming the libraries to connect to sybase from ruby exist… you could just write a custom function. https://docs.puppetlabs.com/guides/custom_functions.html

so basically you want to do something like:

````
$workgroup = sybase_workgroup($::hostname)

case $workgroup {

‘A’: {
$template = template(‘modulename/A/template.erb’)
}
‘B’: {
$template = template(‘modulename/B/template.erb’)
}
default: {
$template = template(‘modulename/default/template.erb’)
}
}

And then the custom function would do something like
```` 
require ‘an_sybase_library’

module Puppet::Parser::Functions

  newfunction(:sybase_workstation) do |args|
db = sybase_db_connection
result = db.query(“SELECT workgroup FROM workgroups WHERE hostname = ‘#{args[0]}’”)
result[‘workgroup’]
end
end
````
Totally full of pseudo code, but that hopefully makes sense.

— cwebber

Stella

unread,
Oct 21, 2014, 1:27:44 PM10/21/14
to puppet...@googlegroups.com
Hi cwebber,

Thank you for the detail example!

I finally figured out how to do this. Very simple.

https://docs.puppetlabs.com/facter/2.0/custom_facts.html#external-facts
With recent versions of facter, you can create external facts using any executable format, or even simple yaml or text files, placed in: <MODULEPATH>/<MODULE>/facts.d/. or in /etc/facter/facts.d/
External facts provide a way to use arbitrary executables or scripts as facts, or set facts statically with structured data. If you’ve ever wanted to write a custom fact in Perl, C, or a one-line text file, this is how.

So what I did is just to place my existing perl script which will connect to my sybase and query into the facts.d folder. Modify it a little bit to return key/value pairs:

key1=value1
key2=value2
key3=value3

Then I can use "key1", "key2" and "key3" as global facts (variables).

Thanks!
Reply all
Reply to author
Forward
0 new messages