passing hiera data to custom fact

648 views
Skip to first unread message

Luca Gioppo

unread,
Aug 28, 2013, 4:58:00 AM8/28/13
to puppet...@googlegroups.com
I need to create a custom fact based on a parametric information (I need to get the IP of another machine knowing the hostname, the machine name could be different for different nodes so I need it parameteric and I recover the machine hostname from a hiera, but for the purpose of the question it could also be a data in the module definition).

I'm installing a cluster and need to get the master IP and if I can set the hostname as a fixed known information the IP will be variable since I'm installing it in a cloud environment

The custom facter code is:

# master_ip.rb:
#

require 'facter'

if  FileTest.exists?("/usr/bin/dig")
    cmd = sprintf('/usr/bin/dig +short HERE_GOES_THE PARAMETRIC_HOSTNAME')
    result = `#{cmd}`
    Facter.add("master_ip") do
        setcode { result }
    end
end


Any Idea on how to refer to a data present in the module like $master_hostname or a hiera hash like $master_info['hostname']

Thanks

Dan White

unread,
Aug 28, 2013, 6:43:13 AM8/28/13
to puppet...@googlegroups.com
Do you know this information when you set up the server ?
If so, just create an appropriate "host" resource

http://docs.puppetlabs.com/references/latest/type.html#host

“Sometimes I think the surest sign that intelligent life exists elsewhere in the universe is that none of it has tried to contact us.”
Bill Waterson (Calvin & Hobbes)


From: "Luca Gioppo" <lucag...@gmail.com>
To: puppet...@googlegroups.com
Sent: Wednesday, August 28, 2013 4:58:00 AM
Subject: [Puppet Users] passing hiera data to custom fact
--
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 post to this group, send email to puppet...@googlegroups.com.
Visit this group at http://groups.google.com/group/puppet-users.
For more options, visit https://groups.google.com/groups/opt_out.

jcbollinger

unread,
Aug 28, 2013, 9:41:10 AM8/28/13
to puppet...@googlegroups.com


This is altogether the wrong approach.  The datum you wish to evaluate is not a property of the node, but rather a property of its environment, therefore it is not a node fact.

If you want the master IP to be a top-scope variable then it is possible to make it so without making it a node fact, though I think that would be a poor design choice.  Instead, you should make it a class variable of some suitable class.  Wherever you hang it, you can assign the value based on your looked-up hostname, using a custom function or perhaps the built-in generate() function.


John

Luca Gioppo

unread,
Aug 30, 2013, 4:42:26 AM8/30/13
to puppet...@googlegroups.com
You are right is not a node fact but an "env" datum, so I developed a function that get me the IP address of a Hostname and assigned to a variable of the node.
Thanks
Luca
Reply all
Reply to author
Forward
0 new messages