Skip to content
This repository was archived by the owner on Apr 16, 2018. It is now read-only.

Commit c467ad9

Browse files
buzzdeeearioch
authored andcommitted
Add custom_prepend and custom_append parameter to object/host.pp and
the accompanying template. Idea taken from the apply_service class/template.
1 parent e8ad384 commit c467ad9

File tree

4 files changed

+87
-0
lines changed

4 files changed

+87
-0
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -787,6 +787,16 @@ Notes on specific parameters:
787787

788788
* `groups`: must be specified as a [Puppet array](https://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#arrays), even if there's only one element
789789
* `vars`: must be specified as a [Puppet hash](https://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#hashes), with the Icinga 2 variable as the **key** and the variable's value as the **value**
790+
* `custom_prepend` and `custom_append`: must be specified as [Puppet arrays](https://docs.puppetlabs.com/puppet/latest/reference/lang_datatypes.html#arrays), even if there's only one element. This allows to define free form text which will be inserted either in the beginning, or end of the hosts definition, i.e.
791+
792+
<pre>
793+
custom_append => [
794+
'vars += { disks["disk"] = {} }',
795+
]
796+
</pre>
797+
798+
will add the literal line 'vars += { disks["disk"] = {} }' to the host definition. This helps overcoming limitations about quoting especially in the `vars` hash.
799+
This also allows to add comments to the hosts file.
790800

791801
**Note:** The `ipv6_address` parameter is set to **undef** by default. This is because `facter` can return either IPv4 or IPv6 addresses for the `ipaddress_ethX` facts. The default value for the `ipv6_address` parameter is set to **undef** and not `ipaddress_eth0` so that an IPv4 address isn't unintentionally set as the value for `address6` in the rendered host object definition.
792802

manifests/object/host.pp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,8 @@
4747
$refresh_icinga2_service = true,
4848
$zone = undef,
4949
$command_endpoint = undef,
50+
$custom_prepend = [],
51+
$custom_append = [],
5052
) {
5153

5254
validate_string($object_hostname)
@@ -64,6 +66,8 @@
6466
validate_bool($refresh_icinga2_service)
6567
validate_string($zone)
6668
validate_string($command_endpoint)
69+
validate_array($custom_prepend)
70+
validate_array($custom_append)
6771

6872
#If the refresh_icinga2_service parameter is set to true...
6973
if $refresh_icinga2_service == true {

spec/defines/icinga2__object/host_spec.rb

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -165,4 +165,67 @@
165165

166166
end
167167

168+
context "on #{default} with custom_prepend parameter" do
169+
let :facts do
170+
IcingaPuppet.variants[default]
171+
end
172+
let :params do
173+
{
174+
:display_name => 'testhost',
175+
:custom_prepend => [
176+
'vars += { disks["disk"] = {} }',
177+
'# this is just a comment',
178+
]
179+
}
180+
end
181+
let :pre_condition do
182+
"include 'icinga2'"
183+
end
184+
185+
let(:title) { 'testhost' }
186+
187+
object_file = '/etc/icinga2/objects/hosts/testhost.conf'
188+
it { should contain_icinga2__object__host('testhost') }
189+
it { should contain_file(object_file).with({
190+
:ensure => 'file',
191+
:path => '/etc/icinga2/objects/hosts/testhost.conf',
192+
:content => /object Host "testhost"/,
193+
}) }
194+
it { should contain_file(object_file).with_content(/^\s*import "generic-host"$/) }
195+
it { should contain_file(object_file).with_content(/^\s*display_name = "testhost"$/) }
196+
it { should contain_file(object_file).with_content(/^\s*vars \+= { disks\["disk"\] = {} }/) }
197+
it { should contain_file(object_file).with_content(/^\s*# this is just a comment/) }
198+
end
199+
context "on #{default} with custom_append parameter" do
200+
let :facts do
201+
IcingaPuppet.variants[default]
202+
end
203+
let :params do
204+
{
205+
:display_name => 'testhost',
206+
:custom_append => [
207+
'vars += { disks["disk"] = {} }',
208+
'# this is just a comment',
209+
]
210+
}
211+
end
212+
let :pre_condition do
213+
"include 'icinga2'"
214+
end
215+
216+
let(:title) { 'testhost' }
217+
218+
object_file = '/etc/icinga2/objects/hosts/testhost.conf'
219+
it { should contain_icinga2__object__host('testhost') }
220+
it { should contain_file(object_file).with({
221+
:ensure => 'file',
222+
:path => '/etc/icinga2/objects/hosts/testhost.conf',
223+
:content => /object Host "testhost"/,
224+
}) }
225+
it { should contain_file(object_file).with_content(/^\s*import "generic-host"$/) }
226+
it { should contain_file(object_file).with_content(/^\s*display_name = "testhost"$/) }
227+
it { should contain_file(object_file).with_content(/^\s*vars \+= { disks\["disk"\] = {} }/) }
228+
it { should contain_file(object_file).with_content(/^\s*# this is just a comment/) }
229+
end
230+
168231
end

templates/object/host.conf.erb

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,11 @@
1616
import "<%= t -%>"
1717
<%- end -%>
1818
<%- end -%>
19+
<%- if @custom_prepend && Array(@custom_prepend).size > 0 -%>
20+
<%- Array(@custom_prepend).each do |line| -%>
21+
<%= line %>
22+
<%- end -%>
23+
<%- end -%>
1924
<%- if @display_name -%>
2025
display_name = "<%= @display_name -%>"
2126
<%- end -%>
@@ -96,4 +101,9 @@
96101
<%- if @command_endpoint -%>
97102
command_endpoint = "<%= @command_endpoint -%>"
98103
<%- end -%>
104+
<%- if @custom_append && Array(@custom_append).size > 0 -%>
105+
<%- Array(@custom_append).each do |line| -%>
106+
<%= line %>
107+
<%- end -%>
108+
<%- end -%>
99109
}

0 commit comments

Comments
 (0)