Hash Interpolation inside double quotes?

2,403 views
Skip to first unread message

Douglas Garstang

unread,
Aug 8, 2011, 2:19:36 PM8/8/11
to Puppet Users
I've got this:

    file {
        '/opt/sugarsync/tomcat/tomcat-home/current':
            ensure => "tomcat-$config['tomcat_version_server']";

where $config['tomcat_version_server'] was set with extlookup (the yaml one), by loading:

---
tomcat_config:
  tomcat_version_server: 6.0.20-1
  tomcat_version_libs: 1.0-1

Inside those double quotes, where variable interpolation is supposed to occur, I'm actually getting:

tomcat-tomcat_version_libs1.0-1tomcat_version_server6.0.20-1['tomcat_version_server']

Not '6.0.20-1'. How can I interpolate a hash inside a string? Is this a bug?

Doug.

Brian Gallew

unread,
Aug 8, 2011, 2:22:24 PM8/8/11
to puppet...@googlegroups.com
String interpolation in the Puppet DSL is strictly variable->string, and does not handle arrays. For what you want, use
inline_template("tomcat-<%= $config['tomcat_version_server'] %>")

> --
> 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.

Douglas Garstang

unread,
Aug 8, 2011, 2:52:10 PM8/8/11
to puppet...@googlegroups.com
Hmmm.... I tried that, with:

    file {
        '/opt/sugarsync/tomcat/tomcat-home/current':
            ensure => inline_template("tomcat-<%= $config['tomcat_version_server'] %>");
    }

and that results in:

Aug  8 11:51:03 hpma01p1 puppet-master[18712]: compile error (erb):1: no .<digit> floating literal anymore; put 0 before dot _erbout = ''; _erbout.concat "tomcat-"; _erbout.concat(( tomcat_version_libs1.0-1tomcat_version_server6.0.20-1['tomcat_version_server'] ).to_s); _erbout                                                                               ^ (erb):1: syntax error _erbout = ''; _erbout.concat "tomcat-"; _erbout.concat(( tomcat_version_libs1.0-1tomcat_version_server6.0.20-1['tomcat_version_server'] ).to_s); _erbout                                                                                ^ (erb):1: syntax error _erbout = ''; _erbout.concat "tomcat-"; _erbout.concat(( tomcat_version_libs1.0-1tomcat_version_server6.0.20-1['tomcat_version_server'] ).to_s); _erbout                                                                                                        ^ (erb):1: no .<digit> floating literal anymore; put 0 before dot _erbout = ''; _erbout.concat "tomcat-"; _erbout.concat(( tomcat_versi

vagn scott

unread,
Aug 8, 2011, 3:47:27 PM8/8/11
to puppet...@googlegroups.com
On 08/08/2011 02:52 PM, Douglas Garstang wrote:
>
> file {
> '/opt/sugarsync/tomcat/tomcat-home/current':
> ensure => inline_template("tomcat-<%=
> $config['tomcat_version_server'] %>");
> }
>

Try without the dollar sign:

file {
'/opt/sugarsync/tomcat/tomcat-home/current':
ensure => inline_template("tomcat-<%=

config['tomcat_version_server'] %>");
}

Douglas Garstang

unread,
Aug 8, 2011, 3:58:49 PM8/8/11
to puppet...@googlegroups.com
Thanks. That did it. Ugly...

--
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+unsubscribe@googlegroups.com.

For more options, visit this group at http://groups.google.com/group/puppet-users?hl=en.

Scott Smith

unread,
Aug 8, 2011, 4:01:15 PM8/8/11
to puppet...@googlegroups.com
Works the same as it does in Bourne shell. If you want to use double quotes inside a double-quoted string, you have to escape them. Or use single quotes.

To unsubscribe from this group, send email to puppet-users...@googlegroups.com.

Douglas Garstang

unread,
Aug 8, 2011, 4:09:50 PM8/8/11
to puppet...@googlegroups.com
I wasn't trying to use double quotes inside a double quoted string.

Scott Smith

unread,
Aug 8, 2011, 4:11:14 PM8/8/11
to puppet...@googlegroups.com
D'oh, looks like I need to refresh my vision prescription :(

Douglas Garstang

unread,
Aug 8, 2011, 4:26:28 PM8/8/11
to puppet...@googlegroups.com
But, I would like to replace:

    file {
        "${tomcat_home_dir}/current":
            ensure => inline_template("tomcat-<%= config['tomcat_version_server'] %>");
    }

with:

    file {
        "$config['tomcat_home_dir']/current":
            ensure => inline_template("tomcat-<%= config['tomcat_version_server'] %>");
    }

... and I don't think that will work. Obviously, I can't use an inline template here...

Doug.

vagn scott

unread,
Aug 8, 2011, 4:37:25 PM8/8/11
to puppet...@googlegroups.com
On 08/08/2011 04:26 PM, Douglas Garstang wrote:
> But, I would like to replace:
>
> file {
> "${tomcat_home_dir}/current":
> ensure => inline_template("tomcat-<%=
> config['tomcat_version_server'] %>");
> }
>
> with:
>
> file {
> "$config['tomcat_home_dir']/current":
> ensure => inline_template("tomcat-<%=
> config['tomcat_version_server'] %>");
> }
>

$f = inline_template("<%= config['tomcat_home_dir'] -%>/current")
$v = inline_template("tomcat-<%= config['tomcat_version_server'] %>")

file {
"${f}":
ensure => "${v}",
}

Or similar.

--
vagn

Thomas Bellman

unread,
Aug 8, 2011, 5:18:28 PM8/8/11
to puppet...@googlegroups.com
Douglas Garstang wrote:

> How can I interpolate a hash inside a string?

Like this:

"tomcat-${config['tomcat_version_server']}"

The curly braces groups things so the parser knows that the
variable expression to expand doesn't end after the "g" in
"config".

I'm not sure where this is documented, though. I can't find
it after a quick look in the language guide, where I had
expected it to be.


/Bellman

Stefan Schulte

unread,
Aug 8, 2011, 5:25:52 PM8/8/11
to Puppet Users
IIRC it should read "tomcat-${config['tomcat_version_server']}" (notice
the curling braces). Otherwise puppet will print the hash first (and
concats all keys and values) and then just put ['tomcat_version_server']
after it.

Does the above suggestion work?

-Stefan

Douglas Garstang

unread,
Aug 8, 2011, 5:57:05 PM8/8/11
to puppet...@googlegroups.com
Seems to work...

Thanks.

I got this:

    file {
        "${config['tomcat_home_dir']}/current":
            ensure => "tomcat-${config['tomcat_version_server']}",
            require => Package['ss-tomcat-server'];
    }

Slightly cleaner than the inline template approach (which didn't work for the name anyway).

Doug.
Reply all
Reply to author
Forward
0 new messages