$name is not the file resource title 'a'.
> include foo
>
> root@ops-puppet02:/tmp# puppet apply test.pp
> notice: /Stage[main]/Foo/File[a]/ensure: created
> notice: Finished catalog run in 0.02 seconds
>
> root@ops-puppet02:/tmp# ls -l link*
> lrwxrwxrwx 1 root root 9 2012-02-13 17:12 link.foo -> /etc/motd
>
> If I change the array from [ 'a' ] to [ 'a', 'b' ], then given the above
> logic it will attempt to redefine, leading to this output:
>
> Cannot alias File[b] to ["/tmp/link.foo"] at /tmp/test.pp:6; resource
> ["File", "/tmp/link.foo"] already defined at /tmp/test.pp:6
>
> Seems like a bug, but maybe I'm missing something. Suggestions?
It's not a bug per se, but rather you need to create a define type.
define my_symlink {
file { $name:
ensure => symlink,
path => "/tmp/link.${name}",
target => '/etc/motd',
}
}
and then
my_symlink { ['a', 'b']: }
HTH,
Nan
The field before the colon is the resource’s title, which must be unique and can be used to refer to the resource in other parts of the Puppet configuration.
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
You can refer to the title of the resource in a defined type as $name,
you can't use $name when you declare a resource. This page explains in
further detail about the unique constraint:
http://docs.puppetlabs.com/learning/definedtypes.html
HTH,
Nan
HTH,
Nan
--
You received this message because you are subscribed to the Google Groups "Puppet Users" group.
To post to this group, send email to puppet...@googlegroups.com.
To unsubscribe from this group, send email to puppet-users...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.
I'm not convinced that Justin wasn't correct about this being a bug. I'm having a similar issue
class myclass (...) {
# some extra stuff here
$file_list = [ 'a', 'b' ..., 'n' ]
file { $file_list :
ensure => 'file',
source => "puppet://modules/myclass/$title",
path => "/tmp/$title"
}
results in the "cannot alias file [a] to [/tmp/myclass]". For some reason the resource title in my file is resolving to the class name rather than the file name.
The solution provided above seems to change the semantics. myclass becomes a defined type instead and the list of files is moved outside and passed in as multiple instances are created. That's a work around.
Another would be to not use a list and simply define each file separately in the class, that avoids having to use the $title to abstract the definition into a single resource definition. I can do that quite easily but in my case I have about 100 files so it makes this much less readable. Found someone had already explained the issue here: http://www.nico.schottelius.org/blog/puppet-name-is-not-as-expected-but-classname/.
Does anyone have an answer as to why $title in this case is resolving to the class title rather than the resource title? Is that expected and is there a variable I could use instead that would resolve to the name of the resource I'm declaring rather than the class or defined type I'm declaring it in?
I also tested this and it's not to do with the list, if I use put in each file individually and use $title or $name it still resolves to the class name
On Monday, November 5, 2012 12:37:45 AM UTC-6, pdurkin wrote:I'm not convinced that Justin wasn't correct about this being a bug. I'm having a similar issue
How many people do you need to tell you that the behavior you observe is intended? File a ticket against the documentation if you like, but the behavior is as designed, well-entrenched, broadly relied-upon, and useful.
name
) whose value will default to the title if you don’t specify it. (Internally, this is called the “namevar.”) For the file
type, the path
will default to the title". It uses "name", "title" and "namevar" but these are not referring to variables but to attributes and parts of the resource. They are referred to later as variables on the same page but only in respect to defined resources and classes, so I guess that is why I assumed they were variables everywhere.
class myclass (...) {
# some extra stuff here
$file_list = [ 'a', 'b' ..., 'n' ]
file { $file_list :
ensure => 'file',
source => "puppet://modules/myclass/$title",
path => "/tmp/$title"
}
results in the "cannot alias file [a] to [/tmp/myclass]". For some reason the resource title in my file is resolving to the class name rather than the file name.
Yes. That's as it should be. Variables are always resolved in the innermost scope in which they appear. Other than the top-level scope, scopes are established only by node bocks, definition bodies, and class bodies. In particular, they are not established by resource declarations such as your example file declaration. Nor is there a functionality gap -- Nan already explained how to get the behavior you want.
The solution provided above seems to change the semantics. myclass becomes a defined type instead and the list of files is moved outside and passed in as multiple instances are created. That's a work around.
You are hypothesizing an implementation to criticize, instead of considering how to model your problem in a way that harmonizes with Puppet. Consider this:
define mymodule::modulefile() {
file { $name :
ensure => 'file',
source => "puppet:///mymodule/$name",path => "/tmp/$name"
}
}
class mymodule::myclass {
$file_list = [ 'a', 'b' ..., 'n' ]
mymodule::modulefile { $file_list : }
}
Your class remains a class, and continues to define the file list itself. The definition 'mymodule::modulefile' represents a type of file that has the particular relationship you want between its 'path' and 'source'.