1 Puppet::Type.newtype(:file_line) do
4 Ensures that a given line is contained within a file. The implementation
5 matches the full line, including whitespace at the beginning and end. If
6 the line is not contained in the given file, Puppet will append the line to
7 the end of the file to ensure the desired state. Multiple resources may
8 be declared to manage multiple lines in the same file.
12 file_line { 'sudo_rule':
13 path => '/etc/sudoers',
14 line => '%sudo ALL=(ALL) ALL',
17 file_line { 'sudo_rule_nopw':
18 path => '/etc/sudoers',
19 line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
22 In this example, Puppet will ensure both of the specified lines are
23 contained in the file /etc/sudoers.
27 file_line { 'bashrc_proxy':
29 path => '/etc/bashrc',
30 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
31 match => '^export\ HTTP_PROXY\=',
34 In this code example match will look for a line beginning with export
35 followed by HTTP_PROXY and replace it with the value in line.
37 Match Example With `ensure => absent`:
39 file_line { 'bashrc_proxy':
41 path => '/etc/bashrc',
42 line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
43 match => '^export\ HTTP_PROXY\=',
44 match_for_absence => true,
47 In this code example match will look for a line beginning with export
48 followed by HTTP_PROXY and delete it. If multiple lines match, an
49 error will be raised unless the `multiple => true` parameter is set.
53 file_line { "XScreenSaver":
55 path => '/root/XScreenSaver'
56 line => "*lock: 10:00:00",
58 encoding => "iso-8859-1",
61 Files with special characters that are not valid UTF-8 will give the
62 error message "invalid byte sequence in UTF-8". In this case, determine
63 the correct file encoding and specify the correct encoding using the
64 encoding attribute, the value of which needs to be a valid Ruby character
67 **Autorequires:** If Puppet is managing the file that will contain the line
68 being managed, the file_line resource will autorequire that file.
76 newparam(:name, :namevar => true) do
77 desc 'An arbitrary name used as the identity of the resource.'
81 desc 'An optional ruby regular expression to run against existing lines in the file.' +
82 ' If a match is found, we replace that line rather than adding a new line.' +
83 ' A regex comparison is performed against the line value and if it does not' +
84 ' match an exception will be raised.'
87 newparam(:match_for_absence) do
88 desc 'An optional value to determine if match should be applied when ensure => absent.' +
89 ' If set to true and match is set, the line that matches match will be deleted.' +
90 ' If set to false (the default), match is ignored when ensure => absent.' +
91 ' When `ensure => present`, match_for_absence is ignored.'
92 newvalues(true, false)
96 newparam(:multiple) do
97 desc 'An optional value to determine if match can change multiple lines.' +
98 ' If set to false, an exception will be raised if more than one line matches'
99 newvalues(true, false)
103 desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' +
104 ' This is also takes a regex.'
107 # The line property never changes; the type only ever performs a create() or
108 # destroy(). line is a property in order to allow it to correctly handle
109 # Sensitive type values. Because it is a property which will never change,
110 # it should never be considered out of sync.
111 newproperty(:line) do
112 desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
120 desc 'The file Puppet will ensure contains the line specified by the line parameter.'
122 unless Puppet::Util.absolute_path?(value)
123 raise Puppet::Error, "File paths must be fully qualified, not '#{value}'"
128 newparam(:replace) do
129 desc 'If true, replace line that matches. If false, do not write line if a match is found'
130 newvalues(true, false)
134 newparam(:encoding) do
135 desc 'For files that are not UTF-8 encoded, specify encoding such as iso-8859-1'
139 newparam(:append_on_no_match) do
140 desc 'If true, append line if match is not found. If false, do not append line if a match is not found'
141 newvalues(true, false)
145 # Autorequire the file resource if it's being managed
146 autorequire(:file) do
152 unless (self[:ensure].to_s == 'absent') and (self[:match_for_absence].to_s == 'true') and self[:match]
153 raise(Puppet::Error, "line is a required attribute")
157 raise(Puppet::Error, "path is a required attribute")