From 85f90162fddf0ada11017483e6a295f205d21ff5 Mon Sep 17 00:00:00 2001 From: Lucas Crownover Date: Wed, 23 Nov 2022 17:29:01 -0800 Subject: [PATCH] Add support for not sending callback data for certain hosts. --- plugins/callback/foreman.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/plugins/callback/foreman.py b/plugins/callback/foreman.py index 674d77ac63..1c217af2db 100644 --- a/plugins/callback/foreman.py +++ b/plugins/callback/foreman.py @@ -96,6 +96,16 @@ ini: - section: callback_foreman key: dir_store + ignore_hosts: + description: + - Any hosts matching the set regex pattern will not be reported to Foreman. + - This can be useful for not reporting "localhost" facts. + env: + - name: FOREMAN_IGNORE_HOSTS + default: '' + ini: + - section: callback_foreman + key: ignore_hosts disable_callback: description: - Toggle to make the callback plugin disable itself even if it is loaded. @@ -109,6 +119,7 @@ from datetime import datetime from collections import defaultdict import json +import re import time try: @@ -197,6 +208,8 @@ def set_options(self, task_keys=None, var_options=None, direct=None): ssl_cert = self.get_option('client_cert') ssl_key = self.get_option('client_key') self.dir_store = self.get_option('dir_store') + self.ignore_hosts = self.get_option('ignore_hosts') + self._ignored_hosts_notified = [] if not HAS_REQUESTS: self._disable_plugin(u'The `requests` python module is not installed') @@ -234,6 +247,12 @@ def _ssl_verify(self, option): return verify def _send_data(self, data_type, report_type, host, data): + if self.ignore_hosts and re.match(self.ignore_hosts, host): + if not host in self._ignored_hosts_notified: + self._display.warning(u'Not sending callback data for ignored host: {host}'.format(host=to_text(host))) + self._ignored_hosts_notified.append(host) + return + if data_type == 'facts': url = self.foreman_url + '/api/v2/hosts/facts' elif data_type == 'report' and report_type == 'foreman':