Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Access hiera scope from Puppet custom type

Tags:

puppet

hiera

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

like image 393
Raul Andres Avatar asked Feb 01 '16 11:02

Raul Andres


1 Answers

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.

like image 53
Robo Avatar answered Nov 01 '22 02:11

Robo