Discussion:
[Puppet Users] compare two variables via regex
Lori Cho
2014-12-05 23:16:02 UTC
Permalink
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":}
}

***@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?
--
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 puppet-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/d5fe93ec-b5ab-4e05-89ca-f88d0c6e00d2%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Ramin K
2014-12-06 00:43:05 UTC
Permalink
Post by Lori Cho
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.
$variable1 = 'foo'
$variable2 = 'foobar'
if($variable2 =~ /$variable1/) {
notify {"it works":}
} else {
notify {"regex did not work":}
}
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
--
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 puppet-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/54825119.7020801%40badapple.net.
For more options, visit https://groups.google.com/d/optout.
Felix Frank
2014-12-10 12:46:13 UTC
Permalink
Post by Ramin K
Post by Lori Cho
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.
$variable1 = 'foo'
$variable2 = 'foobar'
if($variable2 =~ /$variable1/) {
notify {"it works":}
} else {
notify {"regex did not work":}
}
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 puppet-users+***@googlegroups.com.
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.
Henrik Lindberg
2014-12-13 23:23:44 UTC
Permalink
Post by Ramin K
Post by Lori Cho
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.
$variable1 = 'foo'
$variable2 = 'foobar'
if($variable2 =~ /$variable1/) {
notify {"it works":}
} else {
notify {"regex did not work":}
}
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
In Puppet 3.7 with future parser the right hand side can be any
expression that evaluates to a regular expression or a string (if string
it is
interpreted as a regexp and the string should not be enclosed in / /).
This will be standard in Puppet 4.0.

e.g

# The variable is a regexp string
if($variable2 =~ $variable) {
#...

# interpolated as part of a regext
if($variable2 =~ "xxxxx$variable") {
#...

Hope that helps
Regards
- henrik
--
Visit my Blog "Puppet on the Edge"
http://puppet-on-the-edge.blogspot.se/
--
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 puppet-users+***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/puppet-users/m6ihq0%24ohm%241%40ger.gmane.org.
For more options, visit https://groups.google.com/d/optout.
Loading...