Skip to content

Commit 7f465f5

Browse files
author
jbondpdx
committed
Edits to new Function: create_ini_settings in README
Also added language highlighting throughout.
1 parent f72730d commit 7f465f5

1 file changed

Lines changed: 133 additions & 136 deletions

File tree

README.markdown

Lines changed: 133 additions & 136 deletions
Original file line numberDiff line numberDiff line change
@@ -25,10 +25,9 @@ Many applications use INI-style configuration files to store their settings. Thi
2525

2626
###Beginning with inifile
2727

28-
2928
To manage a single setting in an INI file, add the `ini_setting` type to a class:
3029

31-
~~~
30+
~~~puppet
3231
ini_setting { "sample setting":
3332
ensure => present,
3433
path => '/tmp/foo.ini',
@@ -51,7 +50,7 @@ The inifile module tries hard not to manipulate your file any more than it needs
5150

5251
Use the `ini_subsetting` type:
5352

54-
~~~
53+
~~~puppet
5554
JAVA_ARGS="-Xmx192m -XX:+HeapDumpOnOutOfMemoryError -XX:HeapDumpPath=/var/log/pe-puppetdb/puppetdb-oom.hprof "
5655
5756
ini_subsetting {'sample subsetting':
@@ -67,7 +66,7 @@ ini_subsetting {'sample subsetting':
6766

6867
###Use a non-standard section header
6968

70-
~~~
69+
~~~puppet
7170
default:
7271
minage = 1
7372
maxage = 13
@@ -85,7 +84,6 @@ ini_setting { 'default minage':
8584

8685
###Implement child providers
8786

88-
8987
You might want to create child providers that inherit the `ini_setting` provider, for one or both of these purposes:
9088

9189
* Make a custom resource to manage an application that stores its settings in INI files, without recreating the code to manage the files themselves.
@@ -94,7 +92,7 @@ You might want to create child providers that inherit the `ini_setting` provider
9492

9593
To implement child providers, first specify a custom type. Have it implement a namevar called `name` and a property called `value`:
9694

97-
~~~
95+
~~~ruby
9896
#my_module/lib/puppet/type/glance_api_config.rb
9997
Puppet::Type.newtype(:glance_api_config) do
10098
ensurable
@@ -114,7 +112,7 @@ end
114112

115113
Your type also needs a provider that uses the `ini_setting` provider as its parent:
116114

117-
~~~
115+
~~~ruby
118116
# my_module/lib/puppet/provider/glance_api_config/ini_setting.rb
119117
Puppet::Type.type(:glance_api_config).provide(
120118
:ini_setting,
@@ -138,20 +136,128 @@ end
138136

139137
Now the settings in /etc/glance/glance-api.conf file can be managed as individual resources:
140138

141-
~~~
139+
~~~puppet
142140
glance_api_config { 'HEADER/important_config':
143141
value => 'secret_value',
144142
}
145143
~~~
146144

147145
If you've implemented self.file_path, you can have Puppet purge the file of all lines that aren't implemented as Puppet resources:
148146

149-
~~~
147+
~~~puppet
150148
resources { 'glance_api_config'
151149
purge => true,
152150
}
153151
~~~
154152

153+
### Manage multiple ini_settings
154+
155+
To manage multiple ini_settings, use the [`create_ini_settings`](#function-create_ini_settings) function.
156+
157+
~~~puppet
158+
$defaults = { 'path' => '/tmp/foo.ini' }
159+
$example = { 'section1' => { 'setting1' => 'value1' } }
160+
create_ini_settings($example, $defaults)
161+
~~~
162+
163+
results in:
164+
165+
~~~puppet
166+
ini_setting { '[section1] setting1':
167+
ensure => present,
168+
section => 'section1',
169+
setting => 'setting1',
170+
value => 'value1',
171+
path => '/tmp/foo.ini',
172+
}
173+
~~~
174+
175+
To include special parameters, the following code:
176+
177+
~~~puppet
178+
$defaults = { 'path' => '/tmp/foo.ini' }
179+
$example = {
180+
'section1' => {
181+
'setting1' => 'value1',
182+
'settings2' => {
183+
'ensure' => 'absent'
184+
}
185+
}
186+
}
187+
create_ini_settings($example, $defaults)
188+
~~~
189+
190+
results in:
191+
192+
~~~puppet
193+
ini_setting { '[section1] setting1':
194+
ensure => present,
195+
section => 'section1',
196+
setting => 'setting1',
197+
value => 'value1',
198+
path => '/tmp/foo.ini',
199+
}
200+
ini_setting { '[section1] setting2':
201+
ensure => absent,
202+
section => 'section1',
203+
setting => 'setting2',
204+
path => '/tmp/foo.ini',
205+
}
206+
~~~
207+
208+
#### Manage multiple ini_settings with Hiera
209+
210+
This example requires Puppet 3.x/4.x, as it uses automatic retrieval of Hiera data for class parameters and `puppetlabs/stdlib`.
211+
212+
For the profile `example`:
213+
214+
~~~puppet
215+
class profile::example (
216+
$settings,
217+
) {
218+
validate_hash($settings)
219+
$defaults = { 'path' => '/tmp/foo.ini' }
220+
create_ini_settings($settings, $defaults)
221+
}
222+
~~~
223+
224+
Provide this in your Hiera data:
225+
226+
~~~puppet
227+
profile::example::settings:
228+
section1:
229+
setting1: value1
230+
setting2: value2
231+
setting3:
232+
ensure: absent
233+
~~~
234+
235+
Results in:
236+
237+
~~~puppet
238+
ini_setting { '[section1] setting1':
239+
ensure => present,
240+
section => 'section1',
241+
setting => 'setting1',
242+
value => 'value1',
243+
path => '/tmp/foo.ini',
244+
}
245+
ini_setting { '[section1] setting2':
246+
ensure => present,
247+
section => 'section1',
248+
setting => 'setting2',
249+
value => 'value2',
250+
path => '/tmp/foo.ini',
251+
}
252+
ini_setting { '[section1] setting3':
253+
ensure => absent,
254+
section => 'section1',
255+
setting => 'setting3',
256+
path => '/tmp/foo.ini',
257+
}
258+
~~~
259+
260+
155261
##Reference
156262

157263
###Public Types
@@ -164,9 +270,6 @@ resources { 'glance_api_config'
164270

165271
* [`create_ini_settings`](#function-create_ini_settings)
166272

167-
168-
169-
170273
### Type: ini_setting
171274

172275
Manages a setting within an INI file.
@@ -207,12 +310,9 @@ Determines whether the specified setting should exist. Valid options: 'present'
207310

208311
##### `section_suffix`
209312

210-
*Optional.* Designates the string that will appear after the section's name. Default value: "]"
211-
212-
**NOTE:** The way this type finds all sections in the file is by looking for lines like `${section_prefix}${title}${section_suffix}`
213-
214-
313+
*Optional.* Designates the string that will appear after the section's name. Default value: "]".
215314

315+
**NOTE:** This type finds all sections in the file by looking for lines like `${section_prefix}${title}${section_suffix}`.
216316

217317
### Type: ini_subsetting
218318

@@ -248,7 +348,6 @@ Specifies whether the subsetting should be present. Valid options: 'present' and
248348

249349
*Required.* Designates a subsetting to manage within the specified setting. Valid options: a string.
250350

251-
252351
##### `subsetting_separator`
253352

254353
*Optional.* Specifies a string to use between subsettings. Valid options: a string. Default value: " ".
@@ -257,142 +356,40 @@ Specifies whether the subsetting should be present. Valid options: 'present' and
257356

258357
*Optional.* Supplies a value for the specified subsetting. Valid options: a string. Default value: undefined.
259358

260-
261-
262-
263359
### Function: create_ini_settings
264360

265-
`create_ini_settings($settings, $defaults)`
361+
Manages multiple `ini_setting` resources from a hash. Note that this cannot be used with ini_subsettings.
266362

267-
Manage multiple ini_setting resources from a hash with comfort. You can provide a hash in your manifest and feed it from Hiera. This can however not be used with ini_subsettings!
363+
`create_ini_settings($settings, $defaults)`
268364

269-
#### Parameters
365+
#### Arguments
270366

271-
##### `$settings`
367+
##### First argument: `settings`
272368

273-
*Required.* Specify a hash with the ini_setting resources.
369+
*Required.* Specify a hash representing the `ini_setting` resources you want to create.
274370

275-
###### Example
276-
~~~
277-
defaults = { 'path' => '/tmp/foo.ini' }
278-
$example = { 'section1' => { 'setting1' => 'value1' } }
279-
create_ini_settings($example, $defaults)
280-
~~~
281-
results in a resource
282-
~~~
283-
ini_setting { '[section1] setting1':
284-
ensure => present,
285-
section => 'section1',
286-
setting => 'setting1',
287-
value => 'value1',
288-
path => '/tmp/foo.ini'
289-
}
290-
~~~
371+
##### `defaults`
291372

292-
###### Example with special parameters
293-
~~~
294-
defaults = { 'path' => '/tmp/foo.ini' }
295-
$example = { 'section1' => {
296-
'setting1' => 'value1',
297-
'settings2' => {
298-
'ensure' => 'absent'
299-
}
300-
}
301-
}
302-
create_ini_settings($example, $defaults)
303-
~~~
304-
results in resources
305-
~~~
306-
ini_setting { '[section1] setting1':
307-
ensure => present,
308-
section => 'section1',
309-
setting => 'setting1',
310-
value => 'value1',
311-
path => '/tmp/foo.ini',
312-
}
313-
ini_setting { '[section1] setting2':
314-
ensure => absent,
315-
section => 'section1',
316-
setting => 'setting2',
317-
path => '/tmp/foo.ini',
318-
}
373+
*Optional.* Accepts a hash to be used as the values for any attributes not defined in the first argument.
374+
375+
~~~puppet
376+
`$example = {
377+
'section1' => {
378+
'setting1' => {
379+
'value' => 'value1', 'path' => '/tmp/foo.ini'
380+
}
381+
}
382+
}`
319383
~~~
320384

321-
##### `$defaults`
322-
323-
*Optional, but recommended.*
324-
325-
This works exactly like `create_resources` defaults parameter. Use it to not repeat yourself to often
326-
and write settings more densely. Example usage see parameter `$settings` above.
327-
328-
If you omit this parameter, you will need to add the `path` and `value` attribute to every single setting as a hash
329-
(`$example = { 'section1' => { 'setting1' => { 'value' => 'value1', 'path' => '/tmp/foo.ini' } } }`).
330-
This most certainly is not what you want, but if you need it it's there.
331-
332385
Default value: '{}'.
333386

334-
#### Example with Hiera
335-
This example will need Puppet 3.x/4.x as it uses automatic retrieval of Hiera data for class parameters and
336-
`puppetlabs/stdlib` (you use that one already, don't you?).
337-
338-
Of course you may use `hiera_hash` when on Puppet 2.x or other use cases. Remember this is only one example,
339-
feel free to live your creativity on writing manifests.
340-
341-
Imagine a profile `example`:
342-
~~~
343-
class profile::example (
344-
$settings,
345-
) {
346-
validate_hash($settings)
347-
$defaults = { 'path' => '/tmp/foo.ini' }
348-
create_ini_settings($settings, $defaults)
349-
}
350-
~~~
351-
352-
Now provide this in your Hiera data:
353-
~~~
354-
profile::example::settings:
355-
section1:
356-
setting1: value1
357-
setting2: value2
358-
setting3:
359-
ensure: absent
360-
~~~
361-
362-
This will result in resources:
363-
~~~
364-
ini_setting { '[section1] setting1':
365-
ensure => present,
366-
section => 'section1',
367-
setting => 'setting1',
368-
value => 'value1',
369-
path => '/tmp/foo.ini',
370-
}
371-
ini_setting { '[section1] setting2':
372-
ensure => present,
373-
section => 'section1',
374-
setting => 'setting2',
375-
value => 'value2',
376-
path => '/tmp/foo.ini',
377-
}
378-
ini_setting { '[section1] setting3':
379-
ensure => absent,
380-
section => 'section1',
381-
setting => 'setting3',
382-
path => '/tmp/foo.ini',
383-
}
384-
~~~
385-
386-
387-
388387
##Limitations
389388

390389
This module has been tested on [all PE-supported platforms](https://forge.puppetlabs.com/supported#compat-matrix), and no issues have been identified. Additionally, it is tested (but not supported) on Windows 7, Mac OS X 10.9, and Solaris 12.
391390

392391
##Development
393392

394-
#Development
395-
396393
Puppet Labs modules on the Puppet Forge are open projects, and community contributions are essential for keeping them great. We can't access the huge number of platforms and myriad of hardware, software, and deployment configurations that Puppet is intended to serve.
397394

398395
We want to keep it as easy as possible to contribute changes so that our modules work in your environment. There are a few guidelines that we need contributors to follow so that we can have a chance of keeping on top of things.

0 commit comments

Comments
 (0)