summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--README.md12
-rw-r--r--lib/puppet/provider/file_line/ruby.rb4
-rw-r--r--lib/puppet/type/file_line.rb8
-rwxr-xr-xspec/unit/puppet/provider/file_line/ruby_spec.rb18
4 files changed, 40 insertions, 2 deletions
diff --git a/README.md b/README.md
index 61318ea..5e298d5 100644
--- a/README.md
+++ b/README.md
@@ -115,6 +115,18 @@ file_line { 'bashrc_proxy':
In the example above, `match` looks for a line beginning with 'export' followed by 'HTTP_PROXY' and replaces it with the value in line.
+Match Example:
+
+ file_line { 'bashrc_proxy':
+ ensure => present,
+ path => '/etc/bashrc',
+ line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
+ match => '^export\ HTTP_PROXY\=',
+ append_on_no_match => false,
+ }
+
+In this code example, `match` looks for a line beginning with export followed by HTTP_PROXY and replaces it with the value in line. If a match is not found, then no changes are made to the file.
+
Match Example with `ensure => absent`:
```puppet
diff --git a/lib/puppet/provider/file_line/ruby.rb b/lib/puppet/provider/file_line/ruby.rb
index 2f6c8ef..16f2709 100644
--- a/lib/puppet/provider/file_line/ruby.rb
+++ b/lib/puppet/provider/file_line/ruby.rb
@@ -12,7 +12,9 @@ Puppet::Type.type(:file_line).provide(:ruby) do
found = lines_count > 0
else
match_count = count_matches(new_match_regex)
- if resource[:replace].to_s == 'true'
+ if resource[:append_on_no_match].to_s == 'false'
+ found = true
+ elsif resource[:replace].to_s == 'true'
found = lines_count > 0 && lines_count == match_count
else
found = match_count > 0
diff --git a/lib/puppet/type/file_line.rb b/lib/puppet/type/file_line.rb
index 3d691bf..b2357b8 100644
--- a/lib/puppet/type/file_line.rb
+++ b/lib/puppet/type/file_line.rb
@@ -58,7 +58,7 @@ Puppet::Type.newtype(:file_line) do
encoding => "iso-8859-1",
}
- Files with special characters that are not valid UTF-8 will give the
+ Files with special characters that are not valid UTF-8 will give the
error message "invalid byte sequence in UTF-8". In this case, determine
the correct file encoding and specify the correct encoding using the
encoding attribute, the value of which needs to be a valid Ruby character
@@ -136,6 +136,12 @@ Puppet::Type.newtype(:file_line) do
defaultto 'UTF-8'
end
+ newparam(:append_on_no_match) do
+ desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
+ newvalues(true, false)
+ defaultto true
+ end
+
# Autorequire the file resource if it's being managed
autorequire(:file) do
self[:path]
diff --git a/spec/unit/puppet/provider/file_line/ruby_spec.rb b/spec/unit/puppet/provider/file_line/ruby_spec.rb
index ab6edf9..dcca4a4 100755
--- a/spec/unit/puppet/provider/file_line/ruby_spec.rb
+++ b/spec/unit/puppet/provider/file_line/ruby_spec.rb
@@ -194,6 +194,24 @@ describe provider_class, :unless => Puppet::Util::Platform.windows? do
@provider.create
expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = bar\nfoo2")
end
+
+ it 'should not add line after no matches found' do
+ @resource = Puppet::Type::File_line.new(
+ {
+ :name => 'foo',
+ :path => @tmpfile,
+ :line => 'inserted = line',
+ :match => '^foo3$',
+ :append_on_no_match => false,
+ }
+ )
+ @provider = provider_class.new(@resource)
+ File.open(@tmpfile, 'w') do |fh|
+ fh.write("foo1\nfoo = blah\nfoo2\nfoo = baz")
+ end
+ expect(@provider.exists?).to be true
+ expect(File.read(@tmpfile).chomp).to eql("foo1\nfoo = blah\nfoo2\nfoo = baz")
+ end
end
describe 'using after' do