Search code examples
rubylinuxvariablespuppet

Puppet getvar() function not working


I am looking at https://github.com/puppetlabs/puppetlabs-stdlib#getvar

My manifests

class test::var inherits stdlib {

$datalocation = 'site::data'
$bar = getvar("${datalocation}::bar")
# Equivalent to $bar = $site::data::bar

notify{"This is getvar() testing variable, Now bar equal to ${bar}":}

}

when i run puppet on client i get blank

[root@401 ~]# puppet agent --test --noop
...
...
Notice: /Stage[main]/test::Var/Notify[This is getvar() testing variable, Now bar equal to '']/message: current_value absent, should be This is getvar() testing variable, Now bar equal to '' (noop)

Am i missing something?


Solution

  • When I run the above code, part of my puppet output is the following warning:

    Warning: Scope(Class[Test::Var]): Could not look up qualified variable 'site::data::bar'; class site::data could not be found
    

    What this is saying is that site::data::bar doesn't exist in the current scope. If you define it first, then your assignment and following notification will work as expected.

    class site::data {
      $bar = 'foo'
    }
    
    class test::var inherits stdlib {
      require site::data
    
      $datalocation = 'site::data'
      $bar = getvar("${datalocation}::bar")
      # Equivalent to $bar = $site::data::bar                                                                                                                                    
    
      notify{"This is getvar() testing variable, Now bar equal to ${bar}":}
    
    }