Puppet – Add or Replace a string in a file

We can use the file_line type reference to append or substitute a string inside a file with regular expression.

Add a string

file { '/tmp/eureka.txt':
  ensure => present,
}->
file_line { 'Append a line to /tmp/eureka.txt':
  path => '/tmp/eureka.txt',  
  line => 'Hello World',
}

 

Replace a string

file { '/tmp/eureka.txt':
  ensure => present,
}->
file_line { 'Append a line to /tmp/eureka.txt':
  path => '/tmp/eureka.txt',  
  line => 'Hello Eureka',
  match   => "^Hello.*$",
}

 

When you to replace the string, make sure the line attribute is also an valid entry in the match regular expression.

Done =)

Reference:

Advertisements

10 thoughts on “Puppet – Add or Replace a string in a file”

  1. when user file_line , get following error

    Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type file_line at

    Like

  2. Hi

    if you get the solution how to replace string could you please tell me? I am also getting same error “Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type file_line at

    thanks
    –girija

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s