Coverage for lib/ansible/plugins/callback/logstash.py : 38%

Hot-keys on this page
r m x p toggle line displays
j k next/prev highlighted chunk
0 (zero) top of page
1 (one) first highlighted chunk
# (C) 2016, Ievgen Khmelenko <ujenmr@gmail.com> # (C) 2017 Ansible Project # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
callback: logstash type: notification short_description: Sends events to Logstash description: - This callback will report facts and task events to Logstash https://www.elastic.co/products/logstash version_added: "2.3" requirements: - whitelisting in configuration - logstash (python library) options: server: description: Address of the Logstash server env: - name: LOGSTASH_SERVER default: localhost port: description: Port on which logstash is listening env: - name: LOGSTASH_PORT default: 5000 type: description: Message type env: - name: LOGSTASH_TYPE default: ansible '''
HAS_LOGSTASH = True
""" ansible logstash callback plugin ansible.cfg: callback_plugins = <path_to_callback_plugins_folder> callback_whitelist = logstash and put the plugin in <path_to_callback_plugins_folder>
logstash config: input { tcp { port => 5000 codec => json } }
Requires: python-logstash
This plugin makes use of the following environment variables: LOGSTASH_SERVER (optional): defaults to localhost LOGSTASH_PORT (optional): defaults to 5000 LOGSTASH_TYPE (optional): defaults to ansible """
super(CallbackModule, self).__init__()
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()
self.playbook = playbook._file_name data = { 'status': "OK", 'host': self.hostname, 'session': self.session, 'ansible_type': "start", 'ansible_playbook': self.playbook, } self.logger.info("ansible start", extra=data)
end_time = datetime.utcnow() runtime = end_time - self.start_time summarize_stat = {} for host in stats.processed.keys(): summarize_stat[host] = stats.summarize(host)
if self.errors == 0: status = "OK" else: status = "FAILED"
data = { 'status': status, 'host': self.hostname, 'session': self.session, 'ansible_type': "finish", 'ansible_playbook': self.playbook, 'ansible_playbook_duration': runtime.total_seconds(), 'ansible_result': json.dumps(summarize_stat), } self.logger.info("ansible stats", extra=data)
data = { 'status': "OK", 'host': self.hostname, 'session': self.session, 'ansible_type': "task", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'ansible_task': result._task, 'ansible_result': self._dump_results(result._result) } self.logger.info("ansible ok", extra=data)
data = { 'status': "SKIPPED", 'host': self.hostname, 'session': self.session, 'ansible_type': "task", 'ansible_playbook': self.playbook, 'ansible_task': result._task, 'ansible_host': result._host.name } self.logger.info("ansible skipped", extra=data)
data = { 'status': "IMPORTED", 'host': self.hostname, 'session': self.session, 'ansible_type': "import", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'imported_file': imported_file } self.logger.info("ansible import", extra=data)
data = { 'status': "NOT IMPORTED", 'host': self.hostname, 'session': self.session, 'ansible_type': "import", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'missing_file': missing_file } self.logger.info("ansible import", extra=data)
data = { 'status': "FAILED", 'host': self.hostname, 'session': self.session, 'ansible_type': "task", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'ansible_task': result._task, 'ansible_result': self._dump_results(result._result) } self.errors += 1 self.logger.error("ansible failed", extra=data)
data = { 'status': "UNREACHABLE", 'host': self.hostname, 'session': self.session, 'ansible_type': "task", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'ansible_task': result._task, 'ansible_result': self._dump_results(result._result) } self.logger.error("ansible unreachable", extra=data)
data = { 'status': "FAILED", 'host': self.hostname, 'session': self.session, 'ansible_type': "task", 'ansible_playbook': self.playbook, 'ansible_host': result._host.name, 'ansible_task': result._task, 'ansible_result': self._dump_results(result._result) } self.errors += 1 self.logger.error("ansible async", extra=data) |