diff --git a/changelogs/fragments/610_logstash_callback_add_ini_config.yml b/changelogs/fragments/610_logstash_callback_add_ini_config.yml new file mode 100644 index 0000000000..ad2bbad27b --- /dev/null +++ b/changelogs/fragments/610_logstash_callback_add_ini_config.yml @@ -0,0 +1,2 @@ +minor_changes: +- "logstash callback - add ini config (https://github.com/ansible-collections/community.general/pull/610)." diff --git a/plugins/callback/logstash.py b/plugins/callback/logstash.py index 1383fab6b9..f9a3d93dec 100644 --- a/plugins/callback/logstash.py +++ b/plugins/callback/logstash.py @@ -19,16 +19,28 @@ DOCUMENTATION = ''' description: Address of the Logstash server env: - name: LOGSTASH_SERVER + ini: + - section: callback_logstash + key: server + version_added: 1.0.0 default: localhost port: description: Port on which logstash is listening env: - name: LOGSTASH_PORT + ini: + - section: callback_logstash + key: port + version_added: 1.0.0 default: 5000 type: description: Message type env: - name: LOGSTASH_TYPE + ini: + - section: callback_logstash + key: type + version_added: 1.0.0 default: ansible ''' @@ -68,7 +80,7 @@ class CallbackModule(CallbackBase): Requires: python-logstash - This plugin makes use of the following environment variables: + This plugin makes use of the following environment variables or ini config: LOGSTASH_SERVER (optional): defaults to localhost LOGSTASH_PORT (optional): defaults to 5000 LOGSTASH_TYPE (optional): defaults to ansible @@ -79,30 +91,37 @@ class CallbackModule(CallbackBase): CALLBACK_NAME = 'community.general.logstash' CALLBACK_NEEDS_WHITELIST = True - def __init__(self): - super(CallbackModule, self).__init__() + def __init__(self, display=None): + super(CallbackModule, self).__init__(display=display) if not HAS_LOGSTASH: self.disabled = True self._display.warning("The required python-logstash is not installed. " "pip install python-logstash") - else: - self.logger = logging.getLogger('python-logstash-logger') - self.logger.setLevel(logging.DEBUG) - self.handler = logstash.TCPLogstashHandler( - os.getenv('LOGSTASH_SERVER', 'localhost'), - int(os.getenv('LOGSTASH_PORT', 5000)), - version=1, - message_type=os.getenv('LOGSTASH_TYPE', 'ansible') - ) - - self.logger.addHandler(self.handler) - self.hostname = socket.gethostname() - self.session = str(uuid.uuid1()) - self.errors = 0 self.start_time = datetime.utcnow() + def set_options(self, task_keys=None, var_options=None, direct=None): + + super(CallbackModule, self).set_options(task_keys=task_keys, var_options=var_options, direct=direct) + + self.logger = logging.getLogger('python-logstash-logger') + self.logger.setLevel(logging.DEBUG) + + self.logstash_server = self.get_option('server') + self.logstash_port = self.get_option('port') + self.logstash_type = self.get_option('type') + self.handler = logstash.TCPLogstashHandler( + self.logstash_server, + int(self.logstash_port), + version=1, + message_type=self.logstash_type + ) + self.logger.addHandler(self.handler) + self.hostname = socket.gethostname() + self.session = str(uuid.uuid1()) + self.errors = 0 + def v2_playbook_on_start(self, playbook): self.playbook = playbook._file_name data = {