7

I'm building a custom type and I'm unable to get access to the hiera scope from a defaultto block

module Puppet
  require 'puppet/parser/functions/hiera' 
  newtype(:my_type) do

    ensurable

    newparam(:myparam) do
      defaultto { Puppet::Parser::Functions.hiera('myparam') }
    end
    newproperty(:value) do
        desc "Value of the item."
    end
  end
end

But I get

Error: undefined method `hiera' for Puppet::Parser::Functions:Module

I'm actually using Puppet 3.8 and future parser

As a workaround, we use

  $my_vals = hiera_hash('mytype_vals')
  create_resource(my_type, $myvals, {myparam => hiera('myparam')})

That works fine, but my_type objects are expected to be instantiated anywhere in the catalog, an myparam is expected to be the same across all instances. So multiple default value declaration should not be necessary.

Another approach would be to place

My_type{
  myparam => hiera('myparam')
}

In the node manifest. That would do the trick, too, but we are developing a module, and main manifest is out of our scope

Raul Andres
  • 3,766
  • 15
  • 24
  • I can't offhand explain the "undefined method" problem, but I don't think this approach either (1) makes sense or (2) could work for you even if you were able to make the Hiera call. Speaking mainly to the latter, your type needs to be evaluated both on the master *and on the agent*; in the latter case, you (probably) don't have Hiera available to draw upon. – John Bollinger Feb 03 '16 at 16:48
  • For the second point, we use a masterless architecture, and use puppet apply, so nodes have access to their hiera info. For the first, I'll try to explain our use case in the question – Raul Andres Feb 04 '16 at 04:52

1 Answers1

2

You can't access hiera data from a provider, because a provider runs agent side and hiera runs master side.

You've mentioned you run masterless in your comments, this is irrelevant as there is still a "master" run which compiles the Puppet catalog and evaluates hiera values and an "agent" run that applies the catalog using providers.

Imagine the Puppet run as a series of steps:

  • Agent sends master a list of facts
  • Master compiles the site manifest to find the list of classes to include for the node
  • Master evaluates all parameters and hiera variables
  • Master compiles modules into a catalog
  • Master sends catalog to agent
  • Agent applies catalog by feeding parameters to providers

So your best bet is to wrap the provider in a define type. Use the define class to get hiera defaults and pass them to the provider, while allowing the default to be overwritten.

Robo
  • 992
  • 4
  • 10