Ask Your Question
0

How do I access the value of "puppetserver" from Ruby script?

asked 2016-01-05 12:15:22 -0500

rewurzb gravatar image

I need to set a custom fact, based on the IP of the Puppet Server that is visible from both the Puppet server and client. From within a manifest that is common to both sides I can use the value $puppetserver. However, I need to access this same variable from one of my Ruby scripts/functions. I understand that it is in base::params. I need to retrieve this IP address so I can pass it to another Ruby script/function that will set my custom fact.

I've tried accessing the value using Facter.value(:puppetserver) and Facter.value('puppetserver'), but both methods appear to return nil, so I'm guessing I have a scope problem and I don't know enough about either Ruby or Puppet to figure out the right syntax.

Thanks, Ron

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
0

answered 2016-01-05 17:01:00 -0500

GregLarkin gravatar image

Hi Ron,

I've never tried it, but according to this documentation, it appears that the lookupvar function will do what you want: https://docs.puppetlabs.com/guides/custom_functions.html#using-facts-and-variables

I suppose the other option would be to create a file resource that looks something like this to write an external fact on each agent:

file { '/etc/puppetlabs/facter/facts.d/puppetserver.txt':
  ensure  => file,
  owner   => 'root',
  group   => 'root',
  mode    => '0644',
  content => "puppetserver=${::puppetserver}\n"
}

It will take one Puppet run for the external fact to be created, and then on the next Puppet run, the agent will present the puppetserver=A.B.C.D fact back to the master.

Please post back here if you have further questions, if it doesn't work for some reason, or if I've misunderstood what you're trying to do.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2016-01-05 12:15:22 -0500

Seen: 95 times

Last updated: Jan 05 '16