1

Trying to construct a string.

I can do:

"Blah blah ${::osfamily} blah blah"

"Blah blah ${$::osfamily} blah blah"

But whats the syntax to call a function with a variable as parameter and have string interpolation work?

None of the following worked:

"Blah blah ${downcase($::osfamily)} blah blah"

"Blah blah ${downcase($osfamily)} blah blah"

"Blah blah ${downcase(::osfamily)} blah blah"

"Blah blah ${$downcase(osfamily)} blah blah"

"Blah blah ${$downcase($osfamily)} blah blah" and so on.

All I get is: Error 400 on SERVER: Syntax error at '('; expected ')'

Is that even possible in Puppet language?

3 Answers 3

3

It isn't possible. Puppet's language is a little ideosyncratic, in that many things you'd think would work just... don't. You'll need to assign the return value from the function to a variable, then interpolate that variable into the string, like this:

$downcased_osfamily = downcase($::osfamily)
"Blah blah ${downcased_osfamily} blah blah"

Of course, a string by itself isn't any use, so presumably you're assigning that string to a variable of its own, or using it as the value for a resource attribute.

4
  • OMG. For reeeealllz?
    – Swartz
    Sep 29, 2015 at 23:38
  • For reeeeeeealllz. You'll bump into a bunch of other things like this with functions in Puppet -- like no nesting calls (you can't downcase(template("something")), for example). Puppet's language wasn't "designed", it was "spawned".
    – womble
    Sep 29, 2015 at 23:41
  • I would like to have a word with the "spawner(s)".... sigh.
    – Swartz
    Sep 30, 2015 at 0:05
  • That would be Luke Kanies. Nice guy, and a good demonstration of why us sysadmins shouldn't try to become application developers. <grin>
    – womble
    Sep 30, 2015 at 0:06
7

It wasn't possible, but it is now. The following works using your example:

notice("Blah blah ${downcase($::osfamily)} blah blah")

This was tested on Puppet 4.10.x, it's documented as far back as 4.6: https://puppet.com/docs/puppet/4.6/lang_data_string.html#interpolation

1
  • Worked for me with puppet-master 4.8.2 and puppet (client) 3.8.5 . Thanks!
    – lickdragon
    Nov 12, 2018 at 15:27
0

further to bogit's answer you are also able to use the following syntax

puppet apply -e 'notice("Blah blah ${::osfamily.downcase} blah blah")'

you may also want to use the newer facts hash as well

puppet apply -e 'notice("Blah blah ${facts['os']['family'].downcase} blah blah")'

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .