Anyway to get the $name or $title of the resource being called?

69 views
Skip to first unread message

loki77

unread,
Jul 13, 2012, 10:38:37 PM7/13/12
to Puppet Users
Hi - is there way to get the name of the resource being called when
its inside a class?

For example, if I have something defined as 'my_file' and I call it
like this inside of a class:

my_file {
"/etc/bashrc":
;
}

Is there anyway to get the "/etc/bashrc" part in the definition logic
itself? It used to work (in like the .24 days maybe?) that I could
use $name, but I'm noticing in 2.7 that $name and $title now seem to
point to the class that the definition is called in, which breaks my
definition.

Thanks for your help!

Nick Fagerlund

unread,
Jul 13, 2012, 11:22:52 PM7/13/12
to puppet...@googlegroups.com
You may be doing something weird, because $name and $title inside a defined type definition should definitely still refer to the instance's title. In fact, I just tested it to be sure:

define my_file ($message) {
  notify {$title:
    message => "$message, and the title is still $title",
  }
}

class my_class {
  my_file {'the title of the resource, not the class':
    message => "This is the message",
  }
}

include my_class

...should get you:

notice: This is the message, and the title is still the title of the resource, not the class
notice: /Stage[main]/My_class/My_file[the title of the resource, not the class]/Notify[the title of the resource, not the class]/message: defined 'message' as 'This is the message, and the title is still the title of the resource, not the class'
notice: Finished catalog run in 0.05 seconds

Show us what you're doing, maybe?

Erik Dalén

unread,
Jul 14, 2012, 8:09:08 AM7/14/12
to puppet...@googlegroups.com
I think the original poster referred to being able to access the title
of a resource inside a resource definition (not a defined type). For
example:

class example {
file { '/tmp/testfile': content => "foo ${name} bar\n",
}

Here $name would refer to "example", but he wants to access the name
'/tmp/testfile'. But as the resource definition doesn't create a new
scope there's no new variables in it.

--
Erik Dalén

loki77

unread,
Jul 14, 2012, 9:04:59 PM7/14/12
to Puppet Users
Thanks Nick - it turned out that it was an issue on my end which was
exacerbated by a misunderstanding of the log message and then
misreading some online posts.  Everything is working fine now.
 Thanks!

On Jul 13, 4:22 pm, Nick Fagerlund <nick.fagerl...@puppetlabs.com>
wrote:

loki77

unread,
Jul 14, 2012, 9:06:26 PM7/14/12
to Puppet Users
Nick actually was right - but I then ran into exactly what you're
saying Erik :) I fixed it by using a defined resource in place of the
plain file statement, basically how Nick was saying, and it worked
great.

Thanks for your help guys!

On Jul 14, 1:09 am, Erik Dalén <erik.gustav.da...@gmail.com> wrote:
Reply all
Reply to author
Forward
0 new messages