Export puppetdb result as custom fact

asked 2018-02-08 07:29:20 -0600

updated 2018-02-15 07:50:11 -0600

I'd like to export a PuppetDB query result as custom fact.

Currently I query the PupppetDB inside the puppet manifest, write the results to a facter .yaml and read the results afterwards withhin the next puppet run with facter. But that feels somehow wrong to me :-)

is there maybe a more elegant way to do it?

edit: more details added

I have a lxc hostserver with e.g. 5 lxc containers running on it. On the lxc host, I've the facts exported:

  • DC
  • rack
  • rackunit

now I want to show this information in /etc/motd of the lxc containers. (but I can't facter this information inside of the containers, because the information is missing. The facter script is only working on the lxc host)

So I can query the puppetdb during the puppet run on a container

# collect lxc host server
$query_host = ['from', 'resources',['and', ['=','type', 'Lxc'],
['=', 'title',$::networking['hostname']]]]
$result3 = puppetdb_query($query_host)
$host = join($result3.map |$index3, $value3| { $value3['certname'] }, ', ')

I have now the $certname of the host. Next step is to query the puppetdb for the facts of this host and get my relevant info:

if $host != '' {
  $query_dc = "facts{name = \"dc\" and certname = \"${host}\"}"
  $dc = puppetdb_query($query_dc)[0]['value']

next step is to write this information inside of a structured facter .yaml

# create facter needed directories
$facterdir = ['/etc/facter/', '/etc/facter/facts.d']
file {$facterdir:
  ensure => directory,
# create executable facts yaml file
file { '/etc/facter/facts.d/datacenter.yaml':
  ensure  => present,
  content => template('profile/base/datacenter.yaml.erb'),
  mode    => 'ugo+x',

and with the next puppet run, we have the needed things set.

meanwhile I'm ok with the solution. But still wondering if there is a better way.

edit retag flag offensive close merge delete


It might help if you explained what you're trying to do at a higher level - maybe there's a better way to do it?

Daniel Scott gravatar imageDaniel Scott ( 2018-02-13 06:49:29 -0600 )edit

sure, I've improved the question ;)

khaefeli gravatar imagekhaefeli ( 2018-02-15 07:51:00 -0600 )edit

perhapse there is some way you could use exported facts : https://puppet.com/docs/puppet/5.4/lang_exported.html. Export the facts on the host server, and collect them on the containers? just a random idea.

jason gravatar imagejason ( 2018-02-23 13:31:57 -0600 )edit

that's what I'm doing right now - see above: "I have a lxc hostserver with e.g. 5 lxc containers running on it. On the lxc host, I've the facts exported:" :-)

khaefeli gravatar imagekhaefeli ( 2018-02-27 07:33:13 -0600 )edit

i meant specifically collecting exported resources. This would be an alternative to using puppetdb queries.


jason gravatar imagejason ( 2018-03-08 09:36:16 -0600 )edit