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:
Excellent, thanks for the tip on how to replace a string in a file.
LikeLike
Glad to know i could help. =)
LikeLike
when user file_line , get following error
Puppet::Parser::AST::Resource failed with error ArgumentError: Invalid resource type file_line at
LikeLike
Did u have the stdlib included?
GitHub – puppetlabs/puppetlabs-stdlib
LikeLike
Any idea how to add a string on the end of a existing string and be sure it’s present ?
LikeLike
replace the whole existing string?
LikeLike
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
LikeLike
hv you tried this?
Puppet: How to fix “Invalid resource type file_line”
LikeLike
nice, thanks.
LikeLike
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
LikeLike