I have a html file and I want to use Puppet to replace that file with an empty file with the same filename. I am wondering if it is possible for Puppet to delete the whole contents of a file?
E.g. Is this a correct way to delete a line in Puppet?
file_line { 'delete a line':
ensure => absent,
path => /tmp/test,
line => '\ '
match => '^(?:.*)'
}
If you know there is a file /path/to/file.html
and you want to ensure that that file exists and is empty, it's easy:
file { '/path/to/file.html':
ensure => file,
content => '',
}
If you want to ensure that a line in that file is removed using file_line
, you have two ways of doing this when ensure => absent
is set.
One is to set match => ...
and match_for_absence => true
,
as in the following example:
file_line { 'bashrc_proxy':
ensure => absent,
path => '/etc/bashrc',
match => '^export\ HTTP_PROXY\=',
match_for_absence => true,
}
In this code example match will look for a line beginning with export
followed by HTTP_PROXY and delete it. If multiple lines match, an
error will be raised unless the multiple => true
parameter is set.
Note that the line => ...
parameter would be accepted but ignored in
the above example.
The second way of using ensure => absent
is to specify a line => ...
,
and no match:
file_line { 'bashrc_proxy':
ensure => absent,
path => '/etc/bashrc',
line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
}
Note that when ensuring lines are absent this way, the default behavior this time is to always remove all lines matching, and this behavior can't be disabled.
See also in the code here and here and in the unit tests.
I have also raised a pull request to add these examples of file_line
into the official docs here, as this was not adequately documented before.
Thanks for the question.