Ask Your Question
1

how to read hiera value in custom factor

asked 2015-11-20 02:04:33 -0500

updated 2015-11-20 03:55:21 -0500

Hi ,

I would like to read value from the hiera for my custom factor. Is it possible ?

I am going to populate a factor based on my hiera value. Please look at my following code for your reference.

require 'facter'

# Default for non-Linux nodes
Facter.add(:jboss_base_algorithm) do
    setcode do
        nil
    end
end

# Linux
#
Facter.add(:jboss_base_algorithm) do
    confine :kernel  => :linux
    setcode do
        Facter::Util::Resolution.exec("/usr/bin/echo '{hiera_value}' | /usr/bin/base64")
    end
end

Any help is much appreciated.

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2015-11-20 20:18:58 -0500

GregLarkin gravatar image

updated 2015-11-24 18:57:35 -0500

That's not possible because Hiera lookups occur on the Puppet master, and custom facts are executed on the Puppet agent node. You can do something like this though:

# The Hiera lookup is done on the Puppet master
$jboss_base_algorithm = hiera('jboss_base_algorithm')

# These commands are executed on the Puppet master
$encoded_algorithm = generate("/usr/bin/echo ${jboss_base_algorithm} | /usr/bin/base64")

# The encoded value is written to a file on the Puppet agent node
file { '/tmp/algorithm.txt':
  ensure  => file,
  content => $encoded_algorithm,
}

Technically, I suppose you could use that file resource to write a custom fact text file in the proper location, and then your agent node would have a fact with the value that you want.

I'm not sure what you're trying to do, but that seems like a lot of unnecessary shuffling around of data, though.

UPDATE:

# The Hiera lookup is done on the Puppet master
$jboss_base_algorithm = hiera('jboss_base_algorithm')

# These commands are executed on the Puppet master
$encoded_algorithm = generate("/bin/sh", "-c", "/bin/echo ${jboss_base_algorithm} | /usr/bin/base64")

# Use the value of $encoded_algorithm as needed
edit flag offensive delete link more

Comments

@GregLarkin: I have pasted the same code which you have given above. $jboss_base_algorithm returning value which was given by hiera. But $encoded_algorithm returning empty value.

ArunRaj Rajavel gravatar imageArunRaj Rajavel ( 2015-11-21 02:53:00 -0500 )edit
1

The generate function is pretty strict but its character-set checking is worked around pretty easily. You also have to make sure that the path names to the executables it calls are correct, e.g. /bin vs. /usr/bin. I have updated my answer above with a possible working example for my CentOS VM.

GregLarkin gravatar imageGregLarkin ( 2015-11-24 18:56:30 -0500 )edit

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: 2015-11-20 02:04:33 -0500

Seen: 433 times

Last updated: Nov 24 '15