loadjson reads from the master

184 views
Skip to first unread message

buoyant_puppy

unread,
Dec 1, 2017, 10:53:16 AM12/1/17
to Puppet Users
I'm trying to pull data from my cmdb into a variable, by downloading my relevant data to a local json file and reading it in:
 
class get_cmdb {
  file { "/tmp/${::hostname}.json":
    source => "http://mycmdb.example.com/${::hostname}"
  }
  $mydata = loadjson("/tmp/${::hostname}.json")

The issue that the file in the first step is created on the agent, but the loadjson attempts to read the file on the master.

How can I either read the file on the agent instead, or vice versa, fetch the file on the master?

R.I.Pienaar

unread,
Dec 1, 2017, 11:59:51 AM12/1/17
to puppet...@googlegroups.com
All functions like foo() run on the master during compile. It's a multi
phase process, the only way a node can influence the compile time is
using facts.

You could integrate your CMDB with puppet using a hiera data function
that could exist as a tier in your hierarchy

buoyant_puppy

unread,
Dec 5, 2017, 5:13:33 AM12/5/17
to Puppet Users


On Friday, December 1, 2017 at 5:59:51 PM UTC+1, R.I. Pienaar wrote:

All functions like foo() run on the master during compile.  It's a multi
phase process, the only way a node can influence the compile time is
using facts.

You could integrate your CMDB with puppet using a hiera data function
that could exist as a tier in your hierarchy

Thanks, that makes sense now. I'm looking into hiera, but I think I have another alternative as well. If I put my json files in the facter/facts.d directory (on the agent), it will be automatically loaded.
The only somewhat minor downside is that facter runs first, so the data it fetches will only be available in the next puppet run.

I'm not sure if that's better or worse than the hiera option as I didn't get hiera working yet.

Rob Nelson

unread,
Dec 5, 2017, 7:39:38 AM12/5/17
to puppet...@googlegroups.com
Facts are loaded, and distributed from the master, before the catalog compilation step. See https://docs.puppet.com/facter/3.3/custom_facts.html#external-facts. Rather than distributing the json files as a File resource, you could add an executable fact to a module and the fact could communicate with the CMDB to gather the correct fact information.

However, I think what you really want is an External Node Classifier (https://puppet.com/docs/puppet/5.3/nodes_external.html). Similar to a function, it runs on the master, but unlike functions it happens before catalog compilation, so the hash of key/values it returns can be used during catalog compilation. That seems like the perfect fit for your use case, except ENCs need to return yaml, not json.

--
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+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/1bbf939a-75a8-49a7-9b11-8e073b945335%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

buoyant_puppy

unread,
Dec 5, 2017, 10:00:55 AM12/5/17
to Puppet Users


On Tuesday, December 5, 2017 at 1:39:38 PM UTC+1, Rob Nelson wrote:
Facts are loaded, and distributed from the master, before the catalog compilation step. See https://docs.puppet.com/facter/3.3/custom_facts.html#external-facts. Rather than distributing the json files as a File resource, you could add an executable fact to a module and the fact could communicate with the CMDB to gather the correct fact information.

I'm confused, because I created this on my client only:
/opt/puppetlabs/facter/facts.d/boo.json:
{ "boo": "boo222" }

And added this to me module:
   notify { "boo is ${boo}": }

And received:
Notice: boo is boo222

Doesn't that demonstrate that files in facts.d/boo.json are loaded read in on the agent side? There is no "boo" variable defined anywhere on the master side.


 
However, I think what you really want is an External Node Classifier (https://puppet.com/docs/puppet/5.3/nodes_external.html). Similar to a function, it runs on the master, but unlike functions it happens before catalog compilation, so the hash of key/values it returns can be used during catalog compilation. That seems like the perfect fit for your use case, except ENCs need to return yaml, not json.



Thanks, I'll investigate that option.

 
On Tue, Dec 5, 2017 at 5:13 AM, buoyant_puppy <fred.o...@gmail.com> wrote:


On Friday, December 1, 2017 at 5:59:51 PM UTC+1, R.I. Pienaar wrote:

All functions like foo() run on the master during compile.  It's a multi
phase process, the only way a node can influence the compile time is
using facts.

You could integrate your CMDB with puppet using a hiera data function
that could exist as a tier in your hierarchy

Thanks, that makes sense now. I'm looking into hiera, but I think I have another alternative as well. If I put my json files in the facter/facts.d directory (on the agent), it will be automatically loaded.
The only somewhat minor downside is that facter runs first, so the data it fetches will only be available in the next puppet run.

I'm not sure if that's better or worse than the hiera option as I didn't get hiera working yet.

--
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.

Rob Nelson

unread,
Dec 5, 2017, 1:19:29 PM12/5/17
to Puppet Users
I'm confused, because I created this on my client only:
/opt/puppetlabs/facter/facts.d/boo.json:
{ "boo": "boo222" }

And added this to me module:
   notify { "boo is ${boo}": }

And received:
Notice: boo is boo222

Doesn't that demonstrate that files in facts.d/boo.json are loaded read in on the agent side? There is no "boo" variable defined anywhere on the master side.

Facts are read on the agent side and, when used with `puppet agent`, submitted to the master. The master can also distribute facts from the master to the agents, and they are still processed before the agent's catalog is compiled. When you use `puppet apply`, the master of course cannot distribute it, but the facts are still parsed before the agent compilation, which is why `$boo` had a value.
Reply all
Reply to author
Forward
0 new messages