Access puppet hash name in templates

203 views
Skip to first unread message

Dan

unread,
Feb 20, 2013, 12:10:50 PM2/20/13
to puppet...@googlegroups.com

Hi,

I've defined a hash like so in my nodes.pp:

net::addr { "eth5":
        rt => {
                rt1 => {
                        address => '192.168.10.0',
                        netmask => '255.255.255.0',
                        gateway => '192.5.28.19',
                        src     => '192.5.28.21'
                },
        }

What I can't get to access is the title of the hash in my templates, so I want to print out the title of the hash i.e. "eth5", how can I do that? Also I have a variable called $int in my define class in the file for my module (/etc/puppet/modules/net/manifests/addr.pp), how can I access that using the scope.lookupvar function from this template in the same module?

Thanks Dan

jcbollinger

unread,
Feb 21, 2013, 11:35:19 AM2/21/13
to puppet...@googlegroups.com
"eth5" is not the name/title of a hash, it is the title of a resource of defined type 'net::addr'.  That resource has a parameter 'rt' that is a hash of hashes.  Inside the resource, you can access the title as $title or $name (either one).  Inside a template evaluated by a template() or inline_template() call from within the definition body, you should be able to access it as @title or @name (and the hash as @rt).

Resources are not data objects for consumption by the catalog compiler (except classes, inasmuch as those are sometimes cast as resources).  You should not attempt to use them as such.  What you could do, however, would be something along these lines:

class net {
  $addresses = {
    'eth5' => {

      rt => {
        rt1 => {
          address => '192.168.10.0',
          netmask => '255.255.255.0',
          gateway => '192.5.28.19',
          src => '192.5.28.21'
        }
      }
    } 
  }
}

You can then access it as $net::addresses['eth5'] from Puppet DSL, and you can use scope.lookupvar('net::addresses') to retrieve the hash, and then access the value of its 'eth5' key.


John

Reply all
Reply to author
Forward
0 new messages