John Simpson
2013-Sep-25 12:12 UTC
[Puppet Users] Noob question about the ${name} variable
Greetings. When defining a "file" resource, why does the ${name} variable sometimes expand to the filename being maintained, and sometimes to the module name? Examples (in a module which, for simplicity, we will call "apache") ... file { ''/etc/httpd/conf/httpd.conf'' : ensure => file , source => "puppet:///modules/${module_name}${name}" , } Here $name expands to "/etc/httpd/conf/httpd.conf", so "source" expands to "puppet:///modules/apache/etc/httpd/conf/httpd.conf". file { ''/etc/httpd/conf.d/ssl.conf'' : ensure => file , content => template ( "${module_name}{$name}.erb" ) , } Here $name expands to "apache", so the filename passed to template() is "apacheapache.erb", rather than the expected and desired value "apache/etc/httpd/conf.d/ssl.conf.erb". Why does $name expand differently in these cases, and where did I miss this in the documentation? -- John Simpson <jms1@voalte.com> Unix System/VM Developer and Engineering Operations, Voalte +1 (941) 312-2830 x148 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Brian Lalor
2013-Sep-25 12:19 UTC
Re: [Puppet Users] Noob question about the ${name} variable
On Sep 25, 2013, at 8:12 AM, John Simpson <jms1@voalte.com> wrote:> file { ''/etc/httpd/conf.d/ssl.conf'' : > ensure => file , > content => template ( "${module_name}{$name}.erb" ) , > } > > Here $name expands to "apache", so the filename passed to template() is "apacheapache.erb", rather than the expected and desired value "apache/etc/httpd/conf.d/ssl.conf.erb".Is this the literal syntax? Because you have "{$name}" instead of "${name}". I can''t explain why the former would expand the way it does, unless the braces around the variable name have special meaning. I would expect you''d get the literal braces in the expanded value. Not a real answer, but it does look unintentional. -- Brian Lalor blalor@bravo5.org http://github.com/blalor -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Rahul Khengare
2013-Sep-25 12:30 UTC
[Puppet Users] Re: Noob question about the ${name} variable
> file { ''/etc/httpd/conf.d/ssl.conf'' : > ensure => file , > content => template ( "${module_name}{$name}.erb" ) , > } > > Here $name expands to "apache", so the filename passed to template() is > "apacheapache.erb", rather than the expected and desired value > "apache/etc/httpd/conf.d/ssl.conf.erb". > > Here you have syntax problem try,content => template ( "${module_name}/${name}.erb" ) , Thanks and Regards, Rahul Khengare, NTT DATA OSS Center, Pune, India. -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
John Simpson
2013-Sep-25 13:06 UTC
Re: [Puppet Users] Noob question about the ${name} variable
On 2013-09-25, at 0819, Brian Lalor <blalor@bravo5.org> wrote: On Sep 25, 2013, at 8:12 AM, John Simpson <jms1@voalte.com> wrote:> >> file { ''/etc/httpd/conf.d/ssl.conf'' : >> ensure => file , >> content => template ( "${module_name}{$name}.erb" ) , >> } >> >> Here $name expands to "apache", so the filename passed to template() is "apacheapache.erb", rather than the expected and desired value "apache/etc/httpd/conf.d/ssl.conf.erb". > > Is this the literal syntax? Because you have "{$name}" instead of "${name}". I can''t explain why the former would expand the way it does, unless the braces around the variable name have special meaning. I would expect you''d get the literal braces in the expanded value. Not a real answer, but it does look unintentional.Sorry, I tried to remove a bunch of extraneous detail from my first message and fat-fingered the variable. The actual syntax, copied and pasted from the module, is: file { ''/etc/httpd/conf.d/ssl.conf'' : ensure => file , mode => 0644 , owner => 0 , group => 0 , content => template ( "${module_name}${name}.erb" ) , notify => Service[''httpd''] , require => Package[''httpd''] , } When I try to apply this on my test machine, I get... # puppet agent -t Info: Retrieving plugin Info: Loading facts in /var/lib/puppet/lib/facter/os_maj_version.rb Error: Could not retrieve catalog from remote server: Error 400 on SERVER: Could not find template ''apacheapache.erb'' at /home/jms1/puppet/apache/manifests/init.pp:21 on node xxx Warning: Not using cache on failed catalog Error: Could not retrieve catalog; skipping run -- John Simpson <jms1@voalte.com> Unix System/VM Developer and Engineering Operations, Voalte +1 (941) 312-2830 x148 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
Rahul Khengare
2013-Sep-26 07:03 UTC
Re: [Puppet Users] Noob question about the ${name} variable
Hi John, Read my previous reply,> > file { ''/etc/httpd/conf.d/ssl.conf'' : > ensure => file , > mode => 0644 , > owner => 0 , > group => 0 , > content => template ( "${module_name}${name}.erb" ) , > notify => Service[''httpd''] , > require => Package[''httpd''] , > } > > The *''/''* is missing in between ${module_name} and ${name}Thanks and Regards, Rahul Khengare -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
jcbollinger
2013-Sep-26 13:26 UTC
[Puppet Users] Re: Noob question about the ${name} variable
On Wednesday, September 25, 2013 7:12:29 AM UTC-5, John Simpson wrote:> > Greetings. > > When defining a "file" resource, why does the ${name} variable sometimes > expand to the filename being maintained, and sometimes to the module name?It doesn''t. The ${name} variable always resolves to the name of the class or definition instance in which it appears, including when (as is often the case) it is used therein inside a resource declaration, such as of a File. Examples: # Class definition: class demo_class { # resource declaration: notify { ''demonstration resource1'': message => "The name of my class is ${name}" } } # Type definition define demo_resource() { # resource declaration: notify { ''demonstration resource2'': message => "The name of my instance is ${name}" } } # Class declaration class { ''demo_class'': } # Will emit a notify message "The name of my class is demo_class" # Class declaration include ''demo_class'' # Equivalent to the previous, so it will emit a notify message # "The name of my class is demo_class" (but only one such if both # declarations appear). # Resource declaration demo_resource { ''instance1'': } # Will emit a notify message "The name of my instance is instance1" Do not be confused by the curly braces of a resource declaration: they do not establish a new scope, so variable references appearing inside them mean the same thing that they do immediately outside. John -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.
John Simpson
2013-Sep-26 22:26 UTC
Re: [Puppet Users] Noob question about the ${name} variable
On 2013-09-26, at 0303, Rahul Khengare <rahulk1306@gmail.com> wrote:> Hi John, > Read my previous reply, > > file { ''/etc/httpd/conf.d/ssl.conf'' : > ensure => file , > mode => 0644 , > owner => 0 , > group => 0 , > content => template ( "${module_name}${name}.erb" ) , > notify => Service[''httpd''] , > require => Package[''httpd''] , > } > > The ''/'' is missing in between ${module_name} and ${name}Sorry, but I wrote the line this way specifically because the first character of the value of $name is "/". Adding an extra "/" between "${module_name}" and "${name}" would result in "apache//xxx.erb". Presumably the "//" wouldn''t hurt anything (the Linux kernel ignores the empty directory element between the two slashes when resolving the filename), but it wouldn''t have helped either. I did, however, figure out what the problem was. In this case, it was confusion caused by either less-than-perfect documentation, or my own less-than-perfect understanding of that documentation. The confusion has to do with what the $name variable actually is. Essentially, it does not refer to the item you''re declaring (in this case, a file.) It refers to the object WITHIN WHICH you are declaring it. So for a file declared within a class... class apache { file { ''blah'' : ensure => file , content => template ( "${module_name}${name}.erb" ) , } } the value of $name is "apache" (the name of the class.) And for a file declared within a define... class apache { define configfile { file { $name : ensure => file , content => template ( "${module_name}${name}.erb" ) , } } configfile { ''/etc/httpd/conf.d/01_aaa.conf'' : } configfile { ''/etc/httpd/conf.d/02_bbb.conf'' : } } the value of $name, in both locations, is the value with which that "define" is later instantiated. So in the second example, $name would be either "/etc/httpd/conf.d/01_aaa.conf" or "/etc/httpd/conf.d/02_bbb.conf", depending on which "configfile" object is being instantiated at the time. The real problem I was trying to solve is that I didn''t want to have to type each filename twice, primarily because it opens up a potential source of errors if the names aren''t typed exactly the same. Creating a "wrapper" around the file object allows the same name to be used multiple times, without having to worry about the name potentially not being identical. It also allowed me to include several other elements (such as "owner", "mode", and "notify") which would otherwise have had to be duplicated. My thanks to everybody who offered advice. -- John Simpson <jms1@voalte.com> Unix System/VM Developer and Engineering Operations, Voalte +1 (941) 312-2830 x148 -- You received this message because you are subscribed to the Google Groups "Puppet Users" group. To unsubscribe from this group and stop receiving emails from it, send an email to puppet-users+unsubscribe@googlegroups.com. To post to this group, send email to puppet-users@googlegroups.com. Visit this group at http://groups.google.com/group/puppet-users. For more options, visit https://groups.google.com/groups/opt_out.