1 | Puppet::Type.newtype(:file_line) do
|
---|
2 |
|
---|
3 | desc <<-EOT
|
---|
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.
|
---|
9 |
|
---|
10 | Example:
|
---|
11 |
|
---|
12 | file_line { 'sudo_rule':
|
---|
13 | path => '/etc/sudoers',
|
---|
14 | line => '%sudo ALL=(ALL) ALL',
|
---|
15 | }
|
---|
16 |
|
---|
17 | file_line { 'sudo_rule_nopw':
|
---|
18 | path => '/etc/sudoers',
|
---|
19 | line => '%sudonopw ALL=(ALL) NOPASSWD: ALL',
|
---|
20 | }
|
---|
21 |
|
---|
22 | In this example, Puppet will ensure both of the specified lines are
|
---|
23 | contained in the file /etc/sudoers.
|
---|
24 |
|
---|
25 | Match Example:
|
---|
26 |
|
---|
27 | file_line { 'bashrc_proxy':
|
---|
28 | ensure => present,
|
---|
29 | path => '/etc/bashrc',
|
---|
30 | line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
|
---|
31 | match => '^export\ HTTP_PROXY\=',
|
---|
32 | }
|
---|
33 |
|
---|
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.
|
---|
36 |
|
---|
37 | Match Example With `ensure => absent`:
|
---|
38 |
|
---|
39 | file_line { 'bashrc_proxy':
|
---|
40 | ensure => absent,
|
---|
41 | path => '/etc/bashrc',
|
---|
42 | line => 'export HTTP_PROXY=http://squid.puppetlabs.vm:3128',
|
---|
43 | match => '^export\ HTTP_PROXY\=',
|
---|
44 | match_for_absence => true,
|
---|
45 | }
|
---|
46 |
|
---|
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.
|
---|
50 |
|
---|
51 | **Autorequires:** If Puppet is managing the file that will contain the line
|
---|
52 | being managed, the file_line resource will autorequire that file.
|
---|
53 | EOT
|
---|
54 |
|
---|
55 | ensurable do
|
---|
56 | defaultvalues
|
---|
57 | defaultto :present
|
---|
58 | end
|
---|
59 |
|
---|
60 | newparam(:name, :namevar => true) do
|
---|
61 | desc 'An arbitrary name used as the identity of the resource.'
|
---|
62 | end
|
---|
63 |
|
---|
64 | newparam(:match) do
|
---|
65 | desc 'An optional ruby regular expression to run against existing lines in the file.' +
|
---|
66 | ' If a match is found, we replace that line rather than adding a new line.' +
|
---|
67 | ' A regex comparison is performed against the line value and if it does not' +
|
---|
68 | ' match an exception will be raised.'
|
---|
69 | end
|
---|
70 |
|
---|
71 | newparam(:match_for_absence) do
|
---|
72 | desc 'An optional value to determine if match should be applied when ensure => absent.' +
|
---|
73 | ' If set to true and match is set, the line that matches match will be deleted.' +
|
---|
74 | ' If set to false (the default), match is ignored when ensure => absent.' +
|
---|
75 | ' When `ensure => present`, match_for_absence is ignored.'
|
---|
76 | newvalues(true, false)
|
---|
77 | defaultto false
|
---|
78 | end
|
---|
79 |
|
---|
80 | newparam(:multiple) do
|
---|
81 | desc 'An optional value to determine if match can change multiple lines.' +
|
---|
82 | ' If set to false, an exception will be raised if more than one line matches'
|
---|
83 | newvalues(true, false)
|
---|
84 | end
|
---|
85 |
|
---|
86 | newparam(:after) do
|
---|
87 | desc 'An optional value used to specify the line after which we will add any new lines. (Existing lines are added in place)' +
|
---|
88 | ' This is also takes a regex.'
|
---|
89 | end
|
---|
90 |
|
---|
91 | newparam(:line) do
|
---|
92 | desc 'The line to be appended to the file or used to replace matches found by the match attribute.'
|
---|
93 | end
|
---|
94 |
|
---|
95 | newparam(:path) do
|
---|
96 | desc 'The file Puppet will ensure contains the line specified by the line parameter.'
|
---|
97 | validate do |value|
|
---|
98 | unless Puppet::Util.absolute_path?(value)
|
---|
99 | raise Puppet::Error, "File paths must be fully qualified, not '#{value}'"
|
---|
100 | end
|
---|
101 | end
|
---|
102 | end
|
---|
103 |
|
---|
104 | newparam(:replace) do
|
---|
105 | desc 'If true, replace line that matches. If false, do not write line if a match is found'
|
---|
106 | newvalues(true, false)
|
---|
107 | defaultto true
|
---|
108 | end
|
---|
109 |
|
---|
110 | # Autorequire the file resource if it's being managed
|
---|
111 | autorequire(:file) do
|
---|
112 | self[:path]
|
---|
113 | end
|
---|
114 |
|
---|
115 | validate do
|
---|
116 | unless self[:line]
|
---|
117 | unless (self[:ensure].to_s == 'absent') and (self[:match_for_absence].to_s == 'true') and self[:match]
|
---|
118 | raise(Puppet::Error, "line is a required attribute")
|
---|
119 | end
|
---|
120 | end
|
---|
121 | unless self[:path]
|
---|
122 | raise(Puppet::Error, "path is a required attribute")
|
---|
123 | end
|
---|
124 | end
|
---|
125 | end
|
---|