1 Puppet::Type.newtype(:file_line) do
3 Ensures that a given line is contained within a file. The implementation
4 matches the full line, including whitespace at the beginning and end. If
5 the line is not contained in the given file, Puppet will append the line to
6 the end of the file to ensure the desired state. Multiple resources may
7 be declared to manage multiple lines in the same file.
11 file_line { 'sudo_rule':
12 path => '/etc/sudoers',
13 line => '%sudo ALL=(ALL) ALL',
16 file_line { 'sudo_rule_nopw':
17 path => '/etc/sudoers',
18 line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
21 In this example, Puppet will ensure both of the specified lines are
22 contained in the file /etc/sudoers.
26 file_line { 'bashrc_proxy':
28 path => '/etc/bashrc',
29 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
30 match => '^export\ HTTP_PROXY\=',
33 In this code example match will look for a line beginning with export
34 followed by HTTP_PROXY and replace it with the value in line.
36 Examples With `ensure => absent`:
38 This type has two behaviors when `ensure => absent` is set.
40 One possibility is to set `match => ...` and `match_for_absence => true`,
41 as in the following example:
43 file_line { 'bashrc_proxy':
45 path => '/etc/bashrc',
46 match => '^export\ HTTP_PROXY\=',
47 match_for_absence => true,
50 In this code example match will look for a line beginning with export
51 followed by HTTP_PROXY and delete it. If multiple lines match, an
52 error will be raised unless the `multiple => true` parameter is set.
54 Note that the `line => ...` parameter would be accepted BUT IGNORED in
57 The second way of using `ensure => absent` is to specify a `line => ...`,
60 file_line { 'bashrc_proxy':
62 path => '/etc/bashrc',
63 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
66 Note that when ensuring lines are absent this way, the default behavior
67 this time is to always remove all lines matching, and this behavior
72 file_line { "XScreenSaver":
74 path => '/root/XScreenSaver',
75 line => "*lock: 10:00:00",
77 encoding => "iso-8859-1",
80 Files with special characters that are not valid UTF-8 will give the
81 error message "invalid byte sequence in UTF-8". In this case, determine
82 the correct file encoding and specify the correct encoding using the
83 encoding attribute, the value of which needs to be a valid Ruby character
86 **Autorequires:** If Puppet is managing the file that will contain the line
87 being managed, the file_line resource will autorequire that file.
95 newparam(:name, :namevar => true) do
96 desc 'An arbitrary name used as the identity of the resource.'
100 desc 'An optional ruby regular expression to run against existing lines in the file.' \
101 ' If a match is found, we replace that line rather than adding a new line.' \
102 ' A regex comparison is performed against the line value and if it does not' \
103 ' match an exception will be raised.'
106 newparam(:match_for_absence) do
107 desc 'An optional value to determine if match should be applied when ensure => absent.' \
108 ' If set to true and match is set, the line that matches match will be deleted.' \
109 ' If set to false (the default), match is ignored when ensure => absent.' \
110 ' When `ensure => present`, match_for_absence is ignored.'
111 newvalues(true, false)
115 newparam(:multiple) do
116 desc 'An optional value to determine if match can change multiple lines.' \
117 ' If set to false, an exception will be raised if more than one line matches'
118 newvalues(true, false)
122 desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' \
123 ' This is also takes a regex.'
126 # The line property never changes; the type only ever performs a create() or
127 # destroy(). line is a property in order to allow it to correctly handle
128 # Sensitive type values. Because it is a property which will never change,
129 # it should never be considered out of sync.
130 newproperty(:line) do
131 desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
139 desc 'The file Puppet will ensure contains the line specified by the line parameter.'
141 unless Puppet::Util.absolute_path?(value)
142 raise Puppet::Error, "File paths must be fully qualified, not '#{value}'"
147 newparam(:replace) do
148 desc 'If true, replace line that matches. If false, do not write line if a match is found'
149 newvalues(true, false)
153 newparam(:replace_all_matches_not_matching_line) do
154 desc 'Configures the behavior of replacing all lines in a file which match the `match` parameter regular expression, regardless of whether the specified line is already present in the file.'
156 newvalues(true, false)
160 newparam(:encoding) do
161 desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
165 newparam(:append_on_no_match) do
166 desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
167 newvalues(true, false)
171 # Autorequire the file resource if it's being managed
172 autorequire(:file) do
177 if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false'
178 raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true')
180 if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false'
181 raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true')
184 unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match]
185 raise(Puppet::Error, 'line is a required attribute')
189 raise(Puppet::Error, 'path is a required attribute')