Noob question about the ${name} variable

924 views
Skip to first unread message

John Simpson

unread,
Sep 25, 2013, 8:12:29 AM9/25/13
to puppet...@googlegroups.com
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 <jm...@voalte.com>
Unix System/VM Developer and Engineering Operations, Voalte
+1 (941) 312-2830 x148

Brian Lalor

unread,
Sep 25, 2013, 8:19:56 AM9/25/13
to puppet...@googlegroups.com
On Sep 25, 2013, at 8:12 AM, John Simpson <jm...@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
bla...@bravo5.org
http://github.com/blalor

Rahul Khengare

unread,
Sep 25, 2013, 8:30:22 AM9/25/13
to puppet...@googlegroups.com

  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.

John Simpson

unread,
Sep 25, 2013, 9:06:46 AM9/25/13
to puppet...@googlegroups.com
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

Rahul Khengare

unread,
Sep 26, 2013, 3:03:32 AM9/26/13
to puppet...@googlegroups.com
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   

jcbollinger

unread,
Sep 26, 2013, 9:26:46 AM9/26/13
to puppet...@googlegroups.com


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

John Simpson

unread,
Sep 26, 2013, 6:26:48 PM9/26/13
to puppet...@googlegroups.com
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.
Reply all
Reply to author
Forward
0 new messages