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:

Advertisement

11 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

  3. Hey, just one question. say you had to replace a line with a facter variable. For eg. $fqdn gives the complete hostname of the server. Now how would you write the line attribute to replace a work with the $fqdn variable

    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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.