1

In my puppet code I use regex inside case statement to identify the nodes I need to apply a certain code

Below is a sample code in my Puppet class

case $os_family {
    'RedHat': {
     case $node_certname {
        /(xyz|pqr\d+)\.(com|au)?/: {

Since the above regex is repeated in may of my classes, I wish to declare it centrally so that I do not have to change all classes and can just change at a central place

I tried to pass variable but it did not work. Can someone please help if this is possible

I should be able to pass parameter or replace by some variable for the regex

/(xyz|pqr\d+)\.(com|au)?/
jd.tech20
  • 171
  • 1
  • 10
  • It is unclear what you are asking. Are you asking if Regex is a data type? Are you asking if a regex can be a variable? Are you asking if a regex can be used in a type alias? Are you asking if variables can be used in conjunction with a regex? Please provide an example of what you are trying to do and the result. – Matthew Schuchard Jun 18 '19 at 15:33
  • Has the answer below resolved your issue? – Alex Harvey Jul 13 '19 at 07:27

1 Answers1

0

I assume you have a version of Puppet < 5.4, and are affected by a bug in Puppet.

In Puppet 5.4, release notes note that:

Previous versions of Puppet converted “rich” data types to String too early when used with defined resource types, classes, or functions. For example, a regular expression would be converted to a String. Puppet 5.4.0 drops the conversion, and defined types, classes, and functions become instances of Regexp, Version, VersionRange, Binary, Timespan, and Timestamp instead of a String representation of the value.

Given this example class here:

class test (
  $regex = /(xyz|pqr\d+)\.(com|au)?/
  ) {
  $data = 'xyz.com'
  case $data {
    $regex: {
      notice("matches")
    }
  }
}

include test

This works fine on Puppet >= 5.4.0:

▶ bundle exec puppet -V                      
5.5.0
▶ bundle exec puppet apply manifests/init.pp                    
Notice: Scope(Class[Test]): matches
Notice: Compiled catalog for alexs-macbook-pro.local in environment production in 0.02 seconds
Notice: Applied catalog in 0.01 seconds

It doesn't work however on earlier versions.

If you upgrade to the latest release of Puppet 5 (or 6) you can avoid this bug.

Alex Harvey
  • 14,494
  • 5
  • 61
  • 97