file_line doesn't replace the match pattern

asked 2016-07-18 04:36:42 -0500

Chutki gravatar image

Hi,

I have below manifests to comment lines from /etc/services file

class servicename_services {

  $count = 5

  $count.each|Integer $index| {

    $incr_index = $index + 1


    file_line { "servicename$incr_index":
      path     => '/etc/services',
      line     => "#servicename$incr_index",
       match    => "^servicename$incr_index .*",
       multiple => 'true'
    }
  }
}

But the above code remove the existing line and replace the matching line.

Actual line /etc/services

servicename1     5000/tcp    #servicename1
servicename1     5000/udp    #servicename1
servicename2     5001/tcp    #servicename2
servicename2     5001/udp    #servicename2
servicename3     5002/tcp    #servicename3
servicename3     5002/udp    #servicename3
servicename4     5003/tcp    #servicename4
servicename4     5003/udp    #servicename4

I want the file to be

#servicename1     5000/tcp    #servicename1
#servicename1     5000/udp    #servicename1
#servicename2    5001/tcp    #servicename2
#servicename2    5001/udp    #servicename2
#servicename3     5002/tcp    #servicename3
#servicename3     5002/udp    #servicename3
#servicename4     5003/tcp    #servicename4
#servicename4     5003/udp    #servicename4

But I got below o/p

#servicename1
#servicename1
#servicename2
#servicename2
#servicename3
#servicename3
#servicename4
#servicename4

It's removing existing port number and hash out line.

edit retag flag offensive close merge delete

Comments

file_line isn't a regex replace function; you've defined you want the line #servicename$incr_index to be the line and that it should replace (rather than add) anything that matches your regex.

Rob Ogilvie gravatar imageRob Ogilvie ( 2016-07-18 12:28:23 -0500 )edit