|
1 Puppet types for concatenating data via a template |
|
2 ================================================== |
|
3 |
|
4 The `datacat` and `datacat_fragment` types allow you to build up a data |
|
5 structure which is rendered using a template. This is similar to some of the |
|
6 common concatenation patterns though the intent should be clearer as it pushes |
|
7 the boilerplate down into the type. |
|
8 |
|
9 [](https://travis-ci.org/richardc/puppet-datacat) |
|
10 |
|
11 Sample Usage |
|
12 ------------ |
|
13 |
|
14 ```puppet |
|
15 datacat { '/etc/nagios/objects/hostgroups.cfg': |
|
16 template => "${module_name}/hostgroups.cfg.erb", |
|
17 } |
|
18 |
|
19 datacat_fragment { "${::fqdn} in device hostgroup": |
|
20 target => '/etc/nagios/objects/hostgroups.cfg', |
|
21 data => { |
|
22 device => [ $::fqdn ], |
|
23 }, |
|
24 } |
|
25 |
|
26 # fred.dc1.notreal has an ilo fred-ilo.dc1.notreal |
|
27 $ilo_fqdn = regsubst($::fqdn, '\.', '-ilo.') |
|
28 datacat_fragment { "${ilo_fqdn} in device hostgroup": |
|
29 target => '/etc/nagios/objects/hostgroups.cfg', |
|
30 data => { |
|
31 device => [ $ilo_fqdn ], |
|
32 }, |
|
33 } |
|
34 ``` |
|
35 |
|
36 And then in your `hostgroups.cfg.erb` |
|
37 |
|
38 ```erb |
|
39 # hostgroups.cfg.erb |
|
40 <% @data.keys.sort.each do |hostgroup| %> |
|
41 define hostgroup { |
|
42 name <%= hostgroup %> |
|
43 members <%= @data[hostgroup].sort.join(',') %> |
|
44 } |
|
45 <% end %> |
|
46 ``` |
|
47 |
|
48 Will produce something like: |
|
49 |
|
50 ``` |
|
51 # /etc/nagios/objects/hostgroups.cfg |
|
52 define hostgroup { |
|
53 name device |
|
54 members fred.dc1.notreal,fred-ilo.dc1.notreal |
|
55 } |
|
56 ``` |
|
57 |
|
58 There are additional samples in a blog post I wrote to describe the approach, |
|
59 http://richardc.unixbeard.net/2013/02/puppet-concat-patterns/ |
|
60 |
|
61 Types and Definitions |
|
62 --------------------- |
|
63 |
|
64 ## Defined Type: `datacat` |
|
65 |
|
66 Wraps the `datacat_collector` and `file` types to cover the most common |
|
67 use-case, collecting data for and templating an entire file. |
|
68 |
|
69 The `ensure` parameter defaults to `file` (an alias for `present`). `ensure` |
|
70 can be set to `absent`. In that case `datacat` will make sure the file *does |
|
71 not exist* and will not collect anything with `datacat_collector`. |
|
72 |
|
73 ## Type: `datacat_collector` |
|
74 |
|
75 The `datacat_collector` type deeply merges a data hash from |
|
76 the `datacat_fragment` resources that target it. |
|
77 |
|
78 These fragments are then rendered via an erb template specified by the |
|
79 `template_body` parameter and used to update the `target_field` property |
|
80 of the related `target_resource`. |
|
81 |
|
82 Sample usage: |
|
83 |
|
84 ```puppet |
|
85 datacat_collector { 'open_ports': |
|
86 template_body => '<%= @data["ports"].sort.join(",") %>', |
|
87 target_resource => File_line['open_ports'], |
|
88 target_field => 'line', |
|
89 } |
|
90 |
|
91 datacat_fragment { 'open webserver': |
|
92 target => 'open_ports', |
|
93 data => { ports => [ 80, 443 ] }, |
|
94 } |
|
95 |
|
96 datacat_fragment { 'open ssh': |
|
97 target => 'open_ports', |
|
98 data => { ports => [ 22 ] }, |
|
99 } |
|
100 ``` |
|
101 |
|
102 Caveats |
|
103 ------- |
|
104 |
|
105 The template is evaluated by the agent at the point of catalog evaluation, |
|
106 this means you cannot call out to puppet parser functions as you would when |
|
107 using the usual `template()` function. |
|
108 |
|
109 |
|
110 Copyright and License |
|
111 --------------------- |
|
112 |
|
113 Copyright (C) 2013 Richard Clamp |
|
114 |
|
115 Licensed under the Apache License, Version 2.0 (the "License"); |
|
116 you may not use this file except in compliance with the License. |
|
117 You may obtain a copy of the License at |
|
118 |
|
119 http://www.apache.org/licenses/LICENSE-2.0 |
|
120 |
|
121 Unless required by applicable law or agreed to in writing, software |
|
122 distributed under the License is distributed on an "AS IS" BASIS, |
|
123 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
|
124 See the License for the specific language governing permissions and |
|
125 limitations under the License. |