case insensitive regex (or if statement)

2,653 views
Skip to first unread message

Jeffrey

unread,
Apr 5, 2011, 6:24:12 PM4/5/11
to Puppet Users
Hi folks,

Does anyone know how to do case-insensitive regex in a puppet
manifest? I tried obvious things, like adding an 'i' at the end of my
regex (ala perl) but that didn't work.

if ($operatingsystem =~ /(centos|redhat|oel)/) {
...
}

The following works (for centos), but it's a bit clunky,

if ($operatingsystem =~ /([Cc][Ee][Nn][Tt][Oo][Ss]|redhat|oel)/) {
...
}

For completion, I found the 'downcasefacts' directive in puppet.conf,
but that's not really what I want. (Plus, 'downcasefacts' is
deprecated anyway.)

Jacob Helwig

unread,
Apr 5, 2011, 6:48:25 PM4/5/11
to puppet...@googlegroups.com

After talking with Nick Lewis, we suspect that the following will work:

if ($operatingsystem =~ /((?i:centos)|redhat|oel)/) {
...
}

Tested with the version of Puppet in the next branch, but should work in
earlier versions.

--
Jacob Helwig

signature.asc

Nan Liu

unread,
Apr 5, 2011, 7:31:09 PM4/5/11
to puppet...@googlegroups.com, Jacob Helwig
On Tue, Apr 5, 2011 at 3:48 PM, Jacob Helwig <ja...@puppetlabs.com> wrote:
> On Tue, 05 Apr 2011 15:24:12 -0700, Jeffrey wrote:
>>
>> Hi folks,
>>
>> Does anyone know how to do case-insensitive regex in a puppet
>> manifest?  I tried obvious things, like adding an 'i' at the end of my
>> regex (ala perl) but that didn't work.
>>
>> if ($operatingsystem =~ /(centos|redhat|oel)/) {
>> ...
>> }
>>
>> The following works (for centos), but it's a bit clunky,
>>
>> if ($operatingsystem =~ /([Cc][Ee][Nn][Tt][Oo][Ss]|redhat|oel)/) {
>> ...
>> }
>>
>> For completion, I found the 'downcasefacts' directive in puppet.conf,
>> but that's not really what I want.  (Plus, 'downcasefacts' is
>> deprecated anyway.)
>>
>
> After talking with Nick Lewis, we suspect that the following will work:
>
>  if ($operatingsystem =~ /((?i:centos)|redhat|oel)/) {
>  ...
>  }

You can obtain the regex string that will work in Puppet via irb to_s:

/(centos|redhat|oel)/i.to_s
=> "(?i-mx:(centos|redhat|oel))"

Thanks,

Nan

Jeffrey

unread,
Apr 5, 2011, 10:35:16 PM4/5/11
to Puppet Users
Thanks Jacob. I was able to get a case insensitive regex using this ?
i: syntax.

if ($operatingsystem =~ /(?i:centos|redhat|oel)/) {
...
}

For the record, I'm using puppet 2.6.6 on Centos 5.5.

- Jeffrey
Reply all
Reply to author
Forward
0 new messages