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?

Swartz
  • 304
  • 5
  • 14

3 Answers3

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

bodgit
  • 4,751
  • 16
  • 27
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.

womble
  • 96,255
  • 29
  • 175
  • 230
  • OMG. For reeeealllz? – Swartz Sep 29 '15 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 '15 at 23:41
  • I would like to have a word with the "spawner(s)".... sigh. – Swartz Sep 30 '15 at 00:05
  • That would be Luke Kanies. Nice guy, and a good demonstration of why us sysadmins shouldn't try to become application developers. – womble Sep 30 '15 at 00:06
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")'
balder
  • 401
  • 4
  • 4