Title on file resources

15 views
Skip to first unread message

Justin Lambert

unread,
Jan 14, 2014, 5:12:56 PM1/14/14
to puppet-users@googlegroups.com Users
I’m attempting to create a bunch of symlinks based on an array of filenames but I can’t figure out how to use the title of the resource within the resource itself.

class app::config {
  file { [ 'a.conf', 'b.conf' ]:
    ensure => 'link',
    path   => "/etc/app/${name}",
    target => "/usr/app/${name}
  }
}
$name and $title both equal app::config in this example.  Is what I’m trying to do possible?

I’m running puppet 3.4.0.

Thanks,
Justin

Joseph Swick

unread,
Jan 14, 2014, 5:33:54 PM1/14/14
to puppet...@googlegroups.com
It is, but you have to use a define (or at least how I'm doing it uses a
define), here's an example based on something thing I'm doing:

common.pp:
class my_module::common () {
...

my_module::common::my_symlinks { ['file1', 'file2', 'file3']: }

...
}

common/my_symlinks.pp:
define my_module::common::my_symlinks: {
file { "symlink_${name}":
ensure => link,
path => "/some/path/${name}",
target => "/some/other/path/${name}",
}
}

I wasn't the first to figure that out, I think I remember who I got it
from, but I'm not sure if it's something they came up with or if they
got it from somewhere else. Hope that helps.

--
Joseph Swick <joseph...@meltwater.com>
Operations Engineer
Meltwater Group

signature.asc

jcbollinger

unread,
Jan 14, 2014, 5:39:49 PM1/14/14
to puppet...@googlegroups.com


Resource declarations do not establish new scopes, so variable references inside mean the same thing they do outside.  Type definitions do establish scopes, however, so if you have enough of these to make it worthwhile then you can do it like so:

define app::link () {
  file { $title:
   
ensure => 'link',
    path => "/etc/app/${title}",
    target => "/usr/app/${title}"
    # $name would work in place of $title, too
}

}

class app::config {
  app::link { [ 'a.conf', 'b.conf' ]: }
}


John

Justin Lambert

unread,
Jan 14, 2014, 5:40:47 PM1/14/14
to puppet-users@googlegroups.com Users
Thanks for the response Joseph - that does help that you had the same situation. That was the only solution I could think of, I was just hoping there was some other way. I’m going to guess it is something to do with scoping of how the catalog is compiled, but I don’t know enough about the puppet internals to say that with authority.
Reply all
Reply to author
Forward
0 new messages