Skip to content

How to access hiera data from ERB template? #9534

Closed
@akomakom

Description

@akomakom

Describe the Bug

Puppet Documentation suggests that it is possible to access variables from module ERB templates via the scope['variable_name'] syntax. It is unclear whether that only works for module variables, eg:

class module_name {
    $var1 = 3 # This works
}

or also for hiera-defined variables, eg in module_name/data/common.yaml:

module_name::var1: 3 # This does not work

Expected Behavior

This ERB template should work:

This is a value: <%= scope['module_name::var1']%>

Steps to Reproduce

Steps to reproduce the behavior:

Create a puppet module with the following files:

  • module_name/templates/test.erb
This is a value: <%= scope['module_name::var1']%>
  • module_name/hiera.yaml
---
version: 5

defaults:
  datadir: data
  data_hash: yaml_data

hierarchy:
  - name: "common"
    path: "common.yaml"
  • module_name/data/common.yaml:
This is a value: <%= scope['module_name::var1']%>
  • module_name/manifests/init.pp:
   file { '/some/test':
     content => template("$module_name/test.erb")
   }

Environment

  • Version Puppetserver 8.7.0
  • Platform Redhat 9

Additional Context

I have tried scope['var1'], scope.lookupvar( <full and short name>0, etc.
I can only resolve the value in a template if it is defined as an explicit variable in the manifest, eg:
$var1 = $module_name::var1

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions