rubyscopepuppetfacter

Access facter variable from within ruby


Is it possible to access a facter variable from within ruby?

if %{virtual} == 'virtualbox'
  result = 'changeit'
else
  result = c[1]['passwd']
end

So $::virtual is a variable in facter.


Solution

  • The method depends on which Ruby function API you're using. If you're using the current 4.x API (your function is at lib/puppet/functions/*.rb) then:

    if closure_scope['facts']['virtual'] == 'virtualbox'
    

    Docs: Writing functions in Ruby: Accessing Puppet variables

    Or if you're using the legacy 3.x compatible API (your function is at lib/puppet/parser/functions/*.rb) then:

    if lookupvar('virtual') == 'virtualbox'
    

    Docs: The legacy Ruby functions API: Using Facts and Variables