Ternary operator in puppet ?

2,196 views
Skip to first unread message

Albert Shih

unread,
Jan 16, 2019, 5:09:36 AM1/16/19
to puppet...@googlegroups.com
Hi,

Is they are something like the ruby ternary operator ?: in puppet ?

I would like to do something like

$value = $hash.has_key('key') ? $hash('key') : $default_value

instead of

if $hash.has_key('key') {
$value = $hash('key')
}
else
{
$value = $default_value
}

to not get a lot of if.

Regards

--
Albert SHIH
DIO bātiment 15
Heure local/Local time:
Wed Jan 16 12:06:15 CET 2019

Karsten Heymann

unread,
Jan 16, 2019, 5:19:08 AM1/16/19
to puppet...@googlegroups.com
Hi Albert,

Am Mi., 16. Jan. 2019 um 11:09 Uhr schrieb Albert Shih <Alber...@obspm.fr>:
> Is they are something like the ruby ternary operator ?: in puppet ?

Sort of, it's called selectors:
https://puppet.com/docs/puppet/5.3/lang_conditional.html#selectors

Unfortunately one of the most useful cases (inside class declarations)
of selectors is disregarded according to puppet-lint:
http://puppet-lint.com/checks/selector_inside_resource/

Best regards
Karsten

Henrik Lindberg

unread,
Jan 16, 2019, 5:47:52 AM1/16/19
to puppet...@googlegroups.com
On 2019-01-16 11:18, Karsten Heymann wrote:
> Hi Albert,
>
> Am Mi., 16. Jan. 2019 um 11:09 Uhr schrieb Albert Shih <Alber...@obspm.fr>:
>> Is they are something like the ruby ternary operator ?: in puppet ?
>
> Sort of, it's called selectors:
> https://puppet.com/docs/puppet/5.3/lang_conditional.html#selectors
>

You can also use an if-expression - for example:

$x = if $somevar == 'foo' { 'it is foo' } else { 'it is not foo' }

- henrik


--

Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/

Reply all
Reply to author
Forward
0 new messages