Ask Your Question
0

regsubst - replace backslashes for fowardslashes

asked 2017-02-21 10:19:48 -0500

Ricardo Gaspar gravatar image

Hey, I'm trying to change some string by replacing the \ (back-slash) for / (forward-slash). The code:

define sslcertificate (
  $password,
  $location,
  $thumbprint,
  $root_store       = 'LocalMachine',
  $store_dir        = 'My',
  $default_temp_dir = 'C:/Windows/Temp') {
  validate_re($name, '^(.)+$', "Must pass name to ${module_name}[${title}]")
  validate_re($location, '^(.)+$', "Must pass location to ${module_name}[${title}]")
  validate_re($thumbprint, '^(.)+$', "Must pass a certificate thumbprint to ${module_name}[${title}]")

  $temp_dir = regsubst("${default_temp_dir}", '\\', '/', 'G')

  ensure_resource('file', $temp_dir, {
    ensure => directory
  }
  )

The original code is from: https://github.com/voxpupuli/puppet-s...

I just want to replace the backslashes in the var $defaulttempdir and store the changed path (with forward-slashes) in $temp_dir.

I also tried using double quotes and using directly the variable.

 $temp_dir = regsubst($default_temp_dir, '\\', '/', 'G')

I'm getting this error:

[1;31mError: Could not retrieve catalog from remote server: Error 500 on SERVER: {"message":"Server Error: Evaluation Error: Error while evaluating a Resource Statement, Evaluation Error: Error while evaluating a Function Call, too short escape sequence: /\/ at /mnt/puppetnfsdir/environments/rchavesgteigi/modules/sslcertificate/manifests/init.pp:63:15 at /mnt/puppetnfsdir/environments/rchavesgteigi/hostgroups/hgwindowsdev/manifests/tsteigicerts.pp:14 on node windev-test-01.cern.ch","issuekind":"RUNTIMEERROR"}

edit retag flag offensive close merge delete

Comments

What version of puppet are you using? Your above call works fine for my (ancient) puppet 3.7.4 configuration at work, I know that 'some string that is supposed to end in a forward slash \' would result in parsing errors as it would sometimes escape the end single quote.

DarylW gravatar imageDarylW ( 2017-02-22 07:20:51 -0500 )edit

You may need to double escape it in that case, or try your string as "\\" instead of '\\'

DarylW gravatar imageDarylW ( 2017-02-22 07:22:08 -0500 )edit

I'm not sure, whether PP complains about that regsubst line but something else? $temp_dir = regsubst($default_temp_dir, '\134', '/', 'G') https://docs.puppet.com/puppet/latest/function.html#regsubsthttps://ruby-doc.org/core/Regexp.html

Kai Burghardt gravatar imageKai Burghardt ( 2017-02-22 08:15:19 -0500 )edit

1 Answer

Sort by » oldest newest most voted
1

answered 2017-02-23 09:27:33 -0500

Ricardo Gaspar gravatar image

My agent is running 3.8.6, but my puppet Master is running version 4. That's the problem because of how puppet 4 deals with escape characters (). I tested with the double quotes and it doesn't work. The solution is to use four backslashes $temp_dir =

$temp_dir = regsubst("${default_temp_dir}", '\\\\', '/', 'G')

In regex we have to use four backslashes to escape only one \

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

1 follower

Stats

Asked: 2017-02-21 10:19:48 -0500

Seen: 151 times

Last updated: Feb 23