From f44364bc3ff062cf135dfe4106472bada0507a5a Mon Sep 17 00:00:00 2001 From: root Date: Tue, 21 Mar 2023 12:43:04 +0000 Subject: [PATCH] fix #392, add parameter eport to Icinga objects to collect them with new class query_objects --- README.md | 5 ++- manifests/object/apiuser.pp | 17 +++++++- manifests/object/checkcommand.pp | 16 ++++++- manifests/object/dependency.pp | 57 ++++++++++++++++--------- manifests/object/endpoint.pp | 17 +++++++- manifests/object/eventcommand.pp | 17 +++++++- manifests/object/host.pp | 17 +++++++- manifests/object/hostgroup.pp | 33 ++++++++++---- manifests/object/icingaapplication.pp | 17 +++++++- manifests/object/notification.pp | 17 +++++++- manifests/object/notificationcommand.pp | 17 +++++++- manifests/object/scheduleddowntime.pp | 17 +++++++- manifests/object/service.pp | 17 +++++++- manifests/object/servicegroup.pp | 37 +++++++++++----- manifests/object/timeperiod.pp | 39 +++++++++++------ manifests/object/user.pp | 17 +++++++- manifests/object/usergroup.pp | 37 +++++++++++----- manifests/object/zone.pp | 17 +++++++- manifests/query_objects.pp | 56 ++++++++++++++++++++++++ metadata.json | 1 - spec/defines/objects_spec.rb | 11 +++++ templates/object.conf.epp | 17 +++++--- templates/objects.epp | 5 +++ 23 files changed, 416 insertions(+), 85 deletions(-) create mode 100644 manifests/query_objects.pp create mode 100644 templates/objects.epp diff --git a/README.md b/README.md index c713b2cd..7ea71874 100644 --- a/README.md +++ b/README.md @@ -25,6 +25,9 @@ Icinga 2 is a widely used open source monitoring software. This Puppet module helps with installing and managing configuration of Icinga 2 on multiple operating systems. +### What's new in version 3.6.0 +Each Icinga object has been given the new parameter `export` that specifies one (ordinary objects for the config server) or more nodes (e.g. zones and endpoints for HA servers from workers aka satellites) as targets where the objects are then created using the class `query_objects`. This has been implemented to avoid collecting export resources in large environments. + ### What's new in version 3.5.0 There are some new function for internal use. Function `icinga2::cert` handels files and/or content for TLS client auth for bot IDO features and for influxdb, infuxdb2, elasticsearch, gelf and icingadb. The function `icinga2::db::connect` provides the client connection string to mysql, mariadb or pgsql databses for both IDO features. @@ -97,7 +100,7 @@ The use of Icinga's own CA is recommended. If you still want to use the Puppet c This module has been tested on: * Debian 10, 11 -* Ubuntu 18.04, 20.04, 22.04 +* Ubuntu 20.04, 22.04 * CentOS/RHEL 7, 8, 9 * AlmaLinux/Rocky 8, 9 * Fedora 32 diff --git a/manifests/object/apiuser.pp b/manifests/object/apiuser.pp index faafc2af..2a07b60b 100644 --- a/manifests/object/apiuser.pp +++ b/manifests/object/apiuser.pp @@ -57,6 +57,9 @@ # @param [Variant[String, Integer]] order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::apiuser ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -65,6 +68,7 @@ Optional[Variant[String, Sensitive[String]]] $password = undef, Optional[String] $client_cn = undef, Variant[String, Integer] $order = 30, + Variant[Array[String], String] $export = [], ) { $_password = if $password =~ String { Sensitive($password) @@ -82,7 +86,7 @@ } # create object - icinga2::object { "icinga2::object::ApiUser::${title}": + $config = { ensure => $ensure, object_name => $apiuser_name, object_type => 'ApiUser', @@ -91,4 +95,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::ApiUser::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::ApiUser::${title}": + * => $config, + } + } } diff --git a/manifests/object/checkcommand.pp b/manifests/object/checkcommand.pp index 676571f5..4b0593ae 100644 --- a/manifests/object/checkcommand.pp +++ b/manifests/object/checkcommand.pp @@ -39,6 +39,8 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. # define icinga2::object::checkcommand ( Stdlib::Absolutepath $target, @@ -52,6 +54,7 @@ Optional[Variant[Hash, String]] $arguments = undef, Boolean $template = false, Variant[String, Integer] $order = 15, + Variant[Array[String], String] $export = [], ) { # compose the attributes $attrs = { @@ -63,7 +66,7 @@ } # create object - icinga2::object { "icinga2::object::CheckCommand::${title}": + $config = { ensure => $ensure, object_name => $checkcommand_name, object_type => 'CheckCommand', @@ -74,4 +77,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::CheckCommand::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::CheckCommand::${title}": + * => $config, + } + } } diff --git a/manifests/object/dependency.pp b/manifests/object/dependency.pp index 7928651c..44e12ac8 100644 --- a/manifests/object/dependency.pp +++ b/manifests/object/dependency.pp @@ -68,27 +68,31 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::dependency ( - Stdlib::Absolutepath $target, - Enum['absent', 'present'] $ensure = present, - String $dependency_name = $title, - Optional[String] $parent_host_name = undef, - Optional[String] $parent_service_name = undef, - Optional[String] $child_host_name = undef, - Optional[String] $child_service_name = undef, - Optional[Boolean] $disable_checks = undef, - Optional[Boolean] $disable_notifications = undef, - Optional[Boolean] $ignore_soft_states = undef, - Optional[String] $period = undef, - Optional[Array] $states = undef, - Variant[Boolean, String] $apply = false, - Variant[Boolean, String] $prefix = false, - Enum['Host', 'Service'] $apply_target = 'Host', - Array $assign = [], - Array $ignore = [], - Array $import = [], - Boolean $template = false, - Variant[String, Integer] $order = 70, + Stdlib::Absolutepath $target, + Enum['absent', 'present'] $ensure = present, + String $dependency_name = $title, + Optional[String] $parent_host_name = undef, + Optional[String] $parent_service_name = undef, + Optional[String] $child_host_name = undef, + Optional[String] $child_service_name = undef, + Optional[Boolean] $disable_checks = undef, + Optional[Boolean] $disable_notifications = undef, + Optional[Boolean] $ignore_soft_states = undef, + Optional[String] $period = undef, + Optional[Array] $states = undef, + Variant[Boolean, String] $apply = false, + Variant[Boolean, String] $prefix = false, + Enum['Host', 'Service'] $apply_target = 'Host', + Array $assign = [], + Array $ignore = [], + Array $import = [], + Boolean $template = false, + Variant[String, Integer] $order = 70, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -104,7 +108,7 @@ } # create object - icinga2::object { "icinga2::object::Dependency::${title}": + $config = { ensure => $ensure, object_name => $dependency_name, object_type => 'Dependency', @@ -120,4 +124,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Dependency::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Dependency::${title}": + * => $config, + } + } } diff --git a/manifests/object/endpoint.pp b/manifests/object/endpoint.pp index 5061795c..06634867 100644 --- a/manifests/object/endpoint.pp +++ b/manifests/object/endpoint.pp @@ -26,6 +26,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::endpoint ( Enum['absent', 'present'] $ensure = present, String $endpoint_name = $title, @@ -34,6 +37,7 @@ Optional[Icinga2::Interval] $log_duration = undef, Optional[Stdlib::Absolutepath] $target = undef, Variant[String, Integer] $order = 40, + Variant[Array[String], String] $export = [], ) { $conf_dir = $icinga2::globals::conf_dir @@ -51,7 +55,7 @@ } # create object - icinga2::object { "icinga2::object::Endpoint::${title}": + $config = { ensure => $ensure, object_name => $endpoint_name, object_type => 'Endpoint', @@ -60,4 +64,15 @@ target => $_target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Endpoint::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Endpoint::${title}": + * => $config, + } + } } diff --git a/manifests/object/eventcommand.pp b/manifests/object/eventcommand.pp index 91356b76..349d6414 100644 --- a/manifests/object/eventcommand.pp +++ b/manifests/object/eventcommand.pp @@ -36,6 +36,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::eventcommand ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -47,6 +50,7 @@ Optional[Hash] $arguments = undef, Array $import = [], Variant[String, Integer] $order = 20, + Variant[Array[String], String] $export = [], ) { # compose the attributes $attrs = { @@ -58,7 +62,7 @@ } # create object - icinga2::object { "icinga2::object::EventCommand::${title}": + $config = { ensure => $ensure, object_name => $eventcommand_name, object_type => 'EventCommand', @@ -68,4 +72,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::EventCommand::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::EventCommand::${title}": + * => $config, + } + } } diff --git a/manifests/object/host.pp b/manifests/object/host.pp index 9e33fb1e..334e7fb8 100644 --- a/manifests/object/host.pp +++ b/manifests/object/host.pp @@ -107,6 +107,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::host ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -142,6 +145,7 @@ Optional[String] $icon_image_alt = undef, Boolean $template = false, Variant[String, Integer] $order = 50, + Variant[Array[String], String] $export = [], ) { # compose the attributes $attrs = { @@ -176,7 +180,7 @@ } # create object - icinga2::object { "icinga2::object::Host::${title}": + $config = { ensure => $ensure, object_name => $host_name, object_type => 'Host', @@ -187,4 +191,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Host::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Host::${title}": + * => $config, + } + } } diff --git a/manifests/object/hostgroup.pp b/manifests/object/hostgroup.pp index dea3bb53..3fbc7886 100644 --- a/manifests/object/hostgroup.pp +++ b/manifests/object/hostgroup.pp @@ -34,15 +34,19 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::hostgroup ( - Stdlib::Absolutepath $target, - Enum['absent', 'present'] $ensure = present, - String $hostgroup_name = $title, - Optional[String] $display_name = undef, - Optional[Array] $groups = undef, - Array $assign = [], - Array $ignore = [], - Variant[String, Integer] $order = 55, + Stdlib::Absolutepath $target, + Enum['absent', 'present'] $ensure = present, + String $hostgroup_name = $title, + Optional[String] $display_name = undef, + Optional[Array] $groups = undef, + Array $assign = [], + Array $ignore = [], + Variant[String, Integer] $order = 55, + Variant[Array[String], String] $export = [], ) { if $ignore != [] and $assign == [] { fail('When attribute ignore is used, assign must be set.') @@ -55,7 +59,7 @@ } # create object - icinga2::object { "icinga2::object::HostGroup::${title}": + $config = { ensure => $ensure, object_name => $hostgroup_name, object_type => 'HostGroup', @@ -66,4 +70,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::HostGroup::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::HostGroup::${title}": + * => $config, + } + } } diff --git a/manifests/object/icingaapplication.pp b/manifests/object/icingaapplication.pp index 198ce5ad..f40a271c 100644 --- a/manifests/object/icingaapplication.pp +++ b/manifests/object/icingaapplication.pp @@ -41,6 +41,9 @@ # @param order # String or integer to control the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::icingaapplication ( Enum['absent', 'present'] $ensure = present, String $app_name = $title, @@ -54,6 +57,7 @@ Optional[String] $environment = undef, Optional[Stdlib::Absolutepath] $target = undef, Variant[String, Integer] $order = 5, + Variant[Array[String], String] $export = [], ) { $conf_dir = $icinga2::globals::conf_dir @@ -77,7 +81,7 @@ } # create object - icinga2::object { "icinga2::object::IcingaApplication::${title}": + $config = { ensure => $ensure, object_name => $app_name, object_type => 'IcingaApplication', @@ -86,4 +90,15 @@ target => $_target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::IcingaApplication::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::IcingaApplication::${title}": + * => $config, + } + } } diff --git a/manifests/object/notification.pp b/manifests/object/notification.pp index 81f677f1..846e5591 100644 --- a/manifests/object/notification.pp +++ b/manifests/object/notification.pp @@ -78,6 +78,9 @@ # @param ignore # Exclude notification using the ignore rules. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::notification ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -102,6 +105,7 @@ Array $import = [], Boolean $template = false, Variant[String, Integer] $order = 85, + Variant[Array[String], String] $export = [], ) { if $ignore != [] and $assign == [] { fail('When attribute ignore is used, assign must be set.') @@ -124,7 +128,7 @@ } # create object - icinga2::object { "icinga2::object::Notification::${title}": + $config = { ensure => $ensure, object_name => $notification_name, object_type => 'Notification', @@ -140,4 +144,15 @@ assign => $assign, ignore => $ignore, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Notification::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Notification::${title}": + * => $config, + } + } } diff --git a/manifests/object/notificationcommand.pp b/manifests/object/notificationcommand.pp index f1b7462c..5eed8662 100644 --- a/manifests/object/notificationcommand.pp +++ b/manifests/object/notificationcommand.pp @@ -40,6 +40,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::notificationcommand ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -52,6 +55,7 @@ Boolean $template = false, Array $import = [], Variant[String, Integer] $order = 25, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -63,7 +67,7 @@ } # create object - icinga2::object { "icinga2::object::NotificationCommand::${title}": + $config = { ensure => $ensure, object_name => $notificationcommand_name, object_type => 'NotificationCommand', @@ -74,4 +78,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::NotificationCommand::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::NotificationCommand::${title}": + * => $config, + } + } } diff --git a/manifests/object/scheduleddowntime.pp b/manifests/object/scheduleddowntime.pp index c470f84f..1ba78ae1 100644 --- a/manifests/object/scheduleddowntime.pp +++ b/manifests/object/scheduleddowntime.pp @@ -51,6 +51,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::scheduleddowntime ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -68,6 +71,7 @@ Array $assign = [], Array $ignore = [], Variant[String, Integer] $order = 90, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -81,7 +85,7 @@ } # create object - icinga2::object { "icinga2::object::ScheduledDowntime::${title}": + $config = { ensure => $ensure, object_name => $scheduleddowntime_name, object_type => 'ScheduledDowntime', @@ -95,4 +99,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::ScheduledDowntime::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::ScheduledDowntime::${title}": + * => $config, + } + } } diff --git a/manifests/object/service.pp b/manifests/object/service.pp index cc1cf102..f0b9d45c 100644 --- a/manifests/object/service.pp +++ b/manifests/object/service.pp @@ -145,6 +145,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::service ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -183,6 +186,7 @@ Array $import = [], Boolean $template = false, Variant[String, Integer] $order = 60, + Variant[Array[String], String] $export = [], ) { # compose the attributes $attrs = { @@ -216,7 +220,7 @@ } # create object - icinga2::object { "icinga2::object::Service::${title}": + $config = { ensure => $ensure, object_name => $service_name, object_type => 'Service', @@ -231,4 +235,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Service::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Service::${title}": + * => $config, + } + } } diff --git a/manifests/object/servicegroup.pp b/manifests/object/servicegroup.pp index ab7578bd..25da27a9 100644 --- a/manifests/object/servicegroup.pp +++ b/manifests/object/servicegroup.pp @@ -32,17 +32,21 @@ # @param [Variant[String, Integer]] order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::servicegroup ( - Stdlib::Absolutepath $target, - Enum['absent', 'present'] $ensure = present, - String $servicegroup_name = $title, - Optional[String] $display_name = undef, - Optional[Array] $groups = undef, - Array $assign = [], - Array $ignore = [], - Boolean $template = false, - Array $import = [], - Variant[String, Integer] $order = 65, + Stdlib::Absolutepath $target, + Enum['absent', 'present'] $ensure = present, + String $servicegroup_name = $title, + Optional[String] $display_name = undef, + Optional[Array] $groups = undef, + Array $assign = [], + Array $ignore = [], + Boolean $template = false, + Array $import = [], + Variant[String, Integer] $order = 65, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -51,7 +55,7 @@ } # create object - icinga2::object { "icinga2::object::ServiceGroup::${title}": + $config = { ensure => $ensure, object_name => $servicegroup_name, object_type => 'ServiceGroup', @@ -64,4 +68,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::ServiceGroup::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::ServiceGroup::${title}": + * => $config, + } + } } diff --git a/manifests/object/timeperiod.pp b/manifests/object/timeperiod.pp index 75cdd4cd..903e93e9 100644 --- a/manifests/object/timeperiod.pp +++ b/manifests/object/timeperiod.pp @@ -35,18 +35,22 @@ # @param order # String or integer to control the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::timeperiod ( - Stdlib::Absolutepath $target, - Enum['absent', 'present'] $ensure = present, - String $timeperiod_name = $title, - Optional[String] $display_name = undef, - Optional[Hash] $ranges = undef, - Optional[Boolean] $prefer_includes = undef, - Optional[Array] $excludes = undef, - Optional[Array] $includes = undef, - Boolean $template = false, - Array $import = ['legacy-timeperiod'], - Variant[String, Integer] $order = 35, + Stdlib::Absolutepath $target, + Enum['absent', 'present'] $ensure = present, + String $timeperiod_name = $title, + Optional[String] $display_name = undef, + Optional[Hash] $ranges = undef, + Optional[Boolean] $prefer_includes = undef, + Optional[Array] $excludes = undef, + Optional[Array] $includes = undef, + Boolean $template = false, + Array $import = ['legacy-timeperiod'], + Variant[String, Integer] $order = 35, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -58,7 +62,7 @@ } # create object - icinga2::object { "icinga2::object::TimePeriod::${title}": + $config = { ensure => $ensure, object_name => $timeperiod_name, object_type => 'TimePeriod', @@ -69,4 +73,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::TimePeriod::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::TimePeriod::${title}": + * => $config, + } + } } diff --git a/manifests/object/user.pp b/manifests/object/user.pp index df3edca5..616684c5 100644 --- a/manifests/object/user.pp +++ b/manifests/object/user.pp @@ -51,6 +51,9 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::user ( Stdlib::Absolutepath $target, Enum['absent', 'present'] $ensure = present, @@ -67,6 +70,7 @@ Array $import = [], Boolean $template = false, Variant[String, Integer] $order = 75, + Variant[Array[String], String] $export = [], ) { # compose attributes $attrs = { @@ -82,7 +86,7 @@ } # create object - icinga2::object { "icinga2::object::User::${title}": + $config = { ensure => $ensure, object_name => $user_name, object_type => 'User', @@ -93,4 +97,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::User::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::User::${title}": + * => $config, + } + } } diff --git a/manifests/object/usergroup.pp b/manifests/object/usergroup.pp index f5afb5ea..781efcc8 100644 --- a/manifests/object/usergroup.pp +++ b/manifests/object/usergroup.pp @@ -32,17 +32,21 @@ # @param order # String or integer to set the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::usergroup ( - Stdlib::Absolutepath $target, - Enum['absent', 'present'] $ensure = present, - String $usergroup_name = $title, - Optional[String] $display_name = undef, - Array $groups = [], - Array $assign = [], - Array $ignore = [], - Array $import = [], - Boolean $template = false, - Variant[String, Integer] $order = 80, + Stdlib::Absolutepath $target, + Enum['absent', 'present'] $ensure = present, + String $usergroup_name = $title, + Optional[String] $display_name = undef, + Array $groups = [], + Array $assign = [], + Array $ignore = [], + Array $import = [], + Boolean $template = false, + Variant[String, Integer] $order = 80, + Variant[Array[String], String] $export = [], ) { if $ignore != [] and $assign == [] { fail('When attribute ignore is used, assign must be set.') @@ -55,7 +59,7 @@ } # create object - icinga2::object { "icinga2::object::UserGroup::${title}": + $config = { ensure => $ensure, object_name => $usergroup_name, object_type => 'UserGroup', @@ -68,4 +72,15 @@ target => $target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::UserGroup::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::UserGroup::${title}": + * => $config, + } + } } diff --git a/manifests/object/zone.pp b/manifests/object/zone.pp index 797eab31..62d42ce6 100644 --- a/manifests/object/zone.pp +++ b/manifests/object/zone.pp @@ -24,6 +24,9 @@ # @param order # String or integer to control the position in the target file, sorted alpha numeric. # +# @param export +# Export object to destination, collected by class `icinga2::query_objects`. +# define icinga2::object::zone ( Enum['absent', 'present'] $ensure = present, String $zone_name = $title, @@ -32,6 +35,7 @@ Boolean $global = false, Optional[Stdlib::Absolutepath] $target = undef, Variant[String, Integer] $order = 45, + Variant[Array[String], String] $export = [], ) { $conf_dir = $icinga2::globals::conf_dir @@ -55,7 +59,7 @@ } # create object - icinga2::object { "icinga2::object::Zone::${title}": + $config = { ensure => $ensure, object_name => $zone_name, object_type => 'Zone', @@ -64,4 +68,15 @@ target => $_target, order => $order, } + + unless empty($export) { + @@icinga2::object { "icinga2::object::Zone::${title}": + tag => prefix(any2array($export), 'icinga2::instance::'), + * => $config, + } + } else { + icinga2::object { "icinga2::object::Zone::${title}": + * => $config, + } + } } diff --git a/manifests/query_objects.pp b/manifests/query_objects.pp new file mode 100644 index 00000000..7ca61f26 --- /dev/null +++ b/manifests/query_objects.pp @@ -0,0 +1,56 @@ +# @summary +# Class to query `icinga2::objects` from puppetdb. +# +# @param destination +# Destination equal to what was set in parameter `export` for objects. +# +# @param environments +# limits the response to objects of these environments +# +class icinga2::query_objects ( + String $destination = $facts['networking']['fqdn'], + Array[String] $environments = [$environment], +) { + $_environments = join($environments, "'") + + case $facts['os']['family'] { + 'windows': { + } # windows + default: { + Concat { + owner => $icinga2::globals::user, + group => $icinga2::globals::group, + mode => '0640', + } + } # default + } + + $pql_query = puppetdb_query("resources[parameters] { environment in ['${_environments}'] and type = 'Icinga2::Object' and exported = true and tag = 'icinga2::instance::${destination}' and nodes { deactivated is null and expired is null } order by certname }") + + $file_list = $pql_query.map |$object| { + $object['parameters']['target'] + } + + unique($file_list).each |$target| { + $objects = $pql_query.filter |$object| { $target == $object['parameters']['target'] } + + $_content = $facts['os']['family'] ? { + 'windows' => regsubst(epp('icinga2/objects.epp', { 'objects' => $objects }), '\n', "\r\n", 'EMG'), + default => epp('icinga2/objects.epp', { 'objects' => $objects }), + } + + if !defined(Concat[$target]) { + concat { $target: + ensure => present, + tag => 'icinga2::config::file', + warn => true, + } + } + + concat::fragment { "custom-${target}": + target => $target, + content => $_content, + order => 99, + } + } +} diff --git a/metadata.json b/metadata.json index 7fc6bb1d..63053199 100644 --- a/metadata.json +++ b/metadata.json @@ -70,7 +70,6 @@ { "operatingsystem": "Ubuntu", "operatingsystemrelease": [ - "18.04", "20.04", "22.04" ] diff --git a/spec/defines/objects_spec.rb b/spec/defines/objects_spec.rb index dad5f8c8..11b40bd3 100644 --- a/spec/defines/objects_spec.rb +++ b/spec/defines/objects_spec.rb @@ -191,5 +191,16 @@ it { is_expected.to compile } end + + context 'with export => foobar' do + let(:params) do + { + target: '/bar/baz', + export: 'foobar' + } + end + + it { expect(exported_resources).to contain_icinga2__object("icinga2::object::#{otype}::foo").with_object_type(otype) } + end end end diff --git a/templates/object.conf.epp b/templates/object.conf.epp index ffd44e0b..44963324 100644 --- a/templates/object.conf.epp +++ b/templates/object.conf.epp @@ -1,14 +1,17 @@ <%- | Hash $attrs, Array $attrs_list, - Variant[String, Boolean] $apply, + Variant[String, Boolean] $apply = false, Optional[String] $apply_target = undef, - Variant[String, Boolean] $prefix, + Variant[String, Boolean] $prefix = false, String $object_type, String $object_name, - Boolean $template, - Array $import, + Boolean $template = false, + Array $import = [], + Array $assign = [], + Array $ignore = [], | -%> +<% $_attrs = merge($attrs, { 'assign where' => $assign, 'ignore where' => $ignore, }) -%> <% if $apply =~ String { %>apply <%= $object_type -%> <% if $prefix { -%> <% if $prefix =~ String { %> "<%= $prefix %>"<% } else { -%> @@ -34,10 +37,10 @@ <% } -%> <% unless $import =~ Array[Data,0,0] { %><%= "\n" %><% } -%> <% if $apply =~String and $apply =~ /^([A-Za-z_]+)\s+in\s+.+$/ { -%> -<%= icinga2::parse($attrs, 2, $attrs_list, {$1=>{}}) -%> +<%= icinga2::parse($_attrs, 2, $attrs_list, {$1=>{}}) -%> <% } elsif $apply =~ String and $apply =~ /^([A-Za-z_]+)\s+=>\s+([A-Za-z_]+)\s+in\s+.+$/ { -%> -<%= icinga2::parse($attrs, 2, $attrs_list+[$1], {$2=>{}}) -%> +<%= icinga2::parse($_attrs, 2, $attrs_list+[$1], {$2=>{}}) -%> <% } else { -%> -<%= icinga2::parse($attrs, 2, $attrs_list) -%> +<%= icinga2::parse($_attrs, 2, $attrs_list) -%> <% } -%> } diff --git a/templates/objects.epp b/templates/objects.epp new file mode 100644 index 00000000..d41e18dc --- /dev/null +++ b/templates/objects.epp @@ -0,0 +1,5 @@ +<%- | Array $objects | -%> + +<% $objects.each |$object| { -%> +<%= epp('icinga2/object.conf.epp', delete($object['parameters'], ['order', 'target', 'ensure', 'tag'])) -%> +<% } -%>