On 12/06/2014 01:43 AM, Ramin K wrote:
> On 12/5/14 3:16 PM, Lori Cho wrote:
>> I have two variables and I want to compare them to each other. However,
>> the regex doesn't return true, because it seems to treat the variable in
>> the // as a literal.
>>
>> Something like this:
>>
>> $variable1 = 'foo'
>> $variable2 = 'foobar'
>>
>> if($variable2 =~ /$variable1/) {
>> notify {"it works":}
>> } else {
>> notify {"regex did not work":}
>> }
>>
>> root@test-slincsplunk1101r(~)# puppet apply /srv/tmp/test.pp
>> notice: regex did not work
>> notice: /Stage[main]//Notify[regex did not work]/message: defined
>> 'message' as 'regex did not work'
>>
>>
>> How can I do this?
>
> The docs say it's not available.
> https://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#regular-expressions
>
>
> "Alternate forms of regex quoting are not allowed and Ruby-style
> variable interpolation is not available."
>
> Ramin
>
>
As a workaround, you can resort to a custom function, or even
inline_template.
Please note that this is horrible and should likely not be used in real
life. Consider it a last ditch.
if inline_template('<%= @variable2 =~ /#{@variable1}/ %>') == 'true' { }
Be aware that inline_template carries a compiler performance penalty.
And don't use this particular example at all ;-)
Felix
--
You received this message because you are subscribed to the Google Groups
"Puppet Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/puppet-users/54884095.20908%40alumni.tu-berlin.de.
For more options, visit https://groups.google.com/d/optout.