1 Puppet::Type.newtype(:file_line) do
4 Ensures that a given line is contained within a file.
6 The implementation matches the full line, including whitespace at the
7 beginning and end. If the line is not contained in the given file, Puppet
8 will append the line to the end of the file to ensure the desired state.
9 Multiple resources may be declared to manage multiple lines in the same file.
13 file_line { 'sudo_rule':
14 path => '/etc/sudoers',
15 line => '%sudo ALL=(ALL) ALL',
18 file_line { 'sudo_rule_nopw':
19 path => '/etc/sudoers',
20 line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
23 In this example, Puppet will ensure both of the specified lines are
24 contained in the file /etc/sudoers.
29 file_line { 'bashrc_proxy':
31 path => '/etc/bashrc',
32 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
33 match => '^export\ HTTP_PROXY\=',
37 In this code example match will look for a line beginning with export
38 followed by HTTP_PROXY and replace it with the value in line.
40 * Examples With `ensure => absent`:
42 This type has two behaviors when `ensure => absent` is set.
44 One possibility is to set `match => ...` and `match_for_absence => true`,
45 as in the following example:
48 file_line { 'bashrc_proxy':
50 path => '/etc/bashrc',
51 match => '^export\ HTTP_PROXY\=',
52 match_for_absence => true,
56 In this code example match will look for a line beginning with export
57 followed by HTTP_PROXY and delete it. If multiple lines match, an
58 error will be raised unless the `multiple => true` parameter is set.
60 Note that the `line => ...` parameter would be accepted BUT IGNORED in
63 The second way of using `ensure => absent` is to specify a `line => ...`,
67 file_line { 'bashrc_proxy':
69 path => '/etc/bashrc',
70 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
75 When ensuring lines are absent this way, the default behavior
76 this time is to always remove all lines matching, and this behavior
82 file_line { "XScreenSaver":
84 path => '/root/XScreenSaver',
85 line => "*lock: 10:00:00",
87 encoding => "iso-8859-1",
91 Files with special characters that are not valid UTF-8 will give the
92 error message "invalid byte sequence in UTF-8". In this case, determine
93 the correct file encoding and specify the correct encoding using the
94 encoding attribute, the value of which needs to be a valid Ruby character
97 **Autorequires:** If Puppet is managing the file that will contain the line
98 being managed, the file_line resource will autorequire that file.
102 desc 'Manage the state of this type.'
107 newparam(:name, :namevar => true) do
108 desc 'An arbitrary name used as the identity of the resource.'
112 desc 'An optional ruby regular expression to run against existing lines in the file.
113 If a match is found, we replace that line rather than adding a new line.
114 A regex comparison is performed against the line value and if it does not
115 match an exception will be raised.'
118 newparam(:match_for_absence) do
119 desc 'An optional value to determine if match should be applied when ensure => absent.
120 If set to true and match is set, the line that matches match will be deleted.
121 If set to false (the default), match is ignored when ensure => absent.
122 When `ensure => present`, match_for_absence is ignored.'
123 newvalues(true, false)
127 newparam(:multiple) do
128 desc 'An optional value to determine if match can change multiple lines.
129 If set to false, an exception will be raised if more than one line matches'
130 newvalues(true, false)
134 desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)
135 This is also takes a regex.'
138 # The line property never changes; the type only ever performs a create() or
139 # destroy(). line is a property in order to allow it to correctly handle
140 # Sensitive type values. Because it is a property which will never change,
141 # it should never be considered out of sync.
142 newproperty(:line) do
143 desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
151 desc 'The file Puppet will ensure contains the line specified by the line parameter.'
153 unless Puppet::Util.absolute_path?(value)
154 raise Puppet::Error, "File paths must be fully qualified, not '#{value}'"
159 newparam(:replace) do
160 desc 'If true, replace line that matches. If false, do not write line if a match is found'
161 newvalues(true, false)
165 newparam(:replace_all_matches_not_matching_line) do
166 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.'
168 newvalues(true, false)
172 newparam(:encoding) do
173 desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
177 newparam(:append_on_no_match) do
178 desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
179 newvalues(true, false)
183 # Autorequire the file resource if it's being managed
184 autorequire(:file) do
188 if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:multiple].to_s == 'false'
189 raise(Puppet::Error, 'multiple must be true when replace_all_matches_not_matching_line is true')
191 if self[:replace_all_matches_not_matching_line].to_s == 'true' && self[:replace].to_s == 'false'
192 raise(Puppet::Error, 'replace must be true when replace_all_matches_not_matching_line is true')
195 unless (self[:ensure].to_s == 'absent') && (self[:match_for_absence].to_s == 'true') && self[:match]
196 raise(Puppet::Error, 'line is a required attribute')
200 raise(Puppet::Error, 'path is a required attribute')