ansible: try and clean up callback_plugins
Signed-off-by: Kevin Fenzi <kevin@scrye.com>
This commit is contained in:
parent
4dbcb94d27
commit
4957a811f1
7 changed files with 47 additions and 671 deletions
|
@ -1,98 +0,0 @@
|
||||||
# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
|
|
||||||
# based on the log_plays example
|
|
||||||
# skvidal@fedoraproject.org
|
|
||||||
# rbean@redhat.com
|
|
||||||
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
import os
|
|
||||||
import pwd
|
|
||||||
|
|
||||||
import fedmsg
|
|
||||||
import fedmsg.config
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.plugins.callback import CallbackBase
|
|
||||||
except ImportError:
|
|
||||||
# Ansible v1 compat
|
|
||||||
CallbackBase = object
|
|
||||||
|
|
||||||
def getlogin():
|
|
||||||
try:
|
|
||||||
user = os.getlogin()
|
|
||||||
except OSError, e:
|
|
||||||
user = pwd.getpwuid(os.geteuid())[0]
|
|
||||||
return user
|
|
||||||
|
|
||||||
|
|
||||||
class CallbackModule(CallbackBase):
|
|
||||||
""" Publish playbook starts and stops to fedmsg. """
|
|
||||||
|
|
||||||
playbook_path = None
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
config = fedmsg.config.load_config()
|
|
||||||
config.update(dict(
|
|
||||||
name='relay_inbound',
|
|
||||||
cert_prefix='shell',
|
|
||||||
active=True,
|
|
||||||
))
|
|
||||||
# It seems like recursive playbooks call this over and over again and
|
|
||||||
# fedmsg doesn't like to be initialized more than once. So, here, just
|
|
||||||
# catch that and ignore it.
|
|
||||||
try:
|
|
||||||
fedmsg.init(**config)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
|
|
||||||
|
|
||||||
def playbook_on_play_start(self, pattern):
|
|
||||||
# This gets called once for each play.. but we just issue a message once
|
|
||||||
# for the first one. One per "playbook"
|
|
||||||
play = getattr(self, 'play', None)
|
|
||||||
if play:
|
|
||||||
# figure out where the playbook FILE is
|
|
||||||
path = os.path.abspath(play.playbook.filename)
|
|
||||||
|
|
||||||
# Bail out early without publishing if we're in --check mode
|
|
||||||
if play.playbook.check:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self.playbook_path:
|
|
||||||
fedmsg.publish(
|
|
||||||
modname="ansible", topic="playbook.start",
|
|
||||||
msg=dict(
|
|
||||||
playbook=path,
|
|
||||||
userid=getlogin(),
|
|
||||||
extra_vars=play.playbook.extra_vars,
|
|
||||||
inventory=play.playbook.inventory.host_list,
|
|
||||||
playbook_checksum=play.playbook.check,
|
|
||||||
check=play.playbook.check,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
self.playbook_path = path
|
|
||||||
|
|
||||||
def playbook_on_stats(self, stats):
|
|
||||||
if not self.playbook_path:
|
|
||||||
return
|
|
||||||
|
|
||||||
results = dict([(h, stats.summarize(h)) for h in stats.processed])
|
|
||||||
fedmsg.publish(
|
|
||||||
modname="ansible", topic="playbook.complete",
|
|
||||||
msg=dict(
|
|
||||||
playbook=self.playbook_path,
|
|
||||||
userid=getlogin(),
|
|
||||||
results=results,
|
|
||||||
),
|
|
||||||
)
|
|
|
@ -1,116 +0,0 @@
|
||||||
# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
|
|
||||||
# based on the log_plays example
|
|
||||||
# skvidal@fedoraproject.org
|
|
||||||
# rbean@redhat.com
|
|
||||||
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
import os
|
|
||||||
import pwd
|
|
||||||
|
|
||||||
import fedmsg
|
|
||||||
import fedmsg.config
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.plugins.callback import CallbackBase
|
|
||||||
except ImportError:
|
|
||||||
# Ansible v1 compat
|
|
||||||
CallbackBase = object
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.utils.hashing import secure_hash
|
|
||||||
except ImportError:
|
|
||||||
from ansible.utils import md5 as secure_hash
|
|
||||||
|
|
||||||
def getlogin():
|
|
||||||
try:
|
|
||||||
user = os.getlogin()
|
|
||||||
except OSError, e:
|
|
||||||
user = pwd.getpwuid(os.geteuid())[0]
|
|
||||||
return user
|
|
||||||
|
|
||||||
|
|
||||||
class CallbackModule(CallbackBase):
|
|
||||||
""" Publish playbook starts and stops to fedmsg. """
|
|
||||||
|
|
||||||
CALLBACK_NAME = 'fedmsg_callback2'
|
|
||||||
CALLBACK_TYPE = 'notification'
|
|
||||||
CALLBACK_VERSION = 2.0
|
|
||||||
CALLBACK_NEEDS_WHITELIST = True
|
|
||||||
|
|
||||||
playbook_path = None
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
config = fedmsg.config.load_config()
|
|
||||||
config.update(dict(
|
|
||||||
name='relay_inbound',
|
|
||||||
cert_prefix='shell',
|
|
||||||
active=True,
|
|
||||||
))
|
|
||||||
# It seems like recursive playbooks call this over and over again and
|
|
||||||
# fedmsg doesn't like to be initialized more than once. So, here, just
|
|
||||||
# catch that and ignore it.
|
|
||||||
try:
|
|
||||||
fedmsg.init(**config)
|
|
||||||
except ValueError:
|
|
||||||
pass
|
|
||||||
self.play = None
|
|
||||||
self.playbook = None
|
|
||||||
|
|
||||||
super(CallbackModule, self).__init__()
|
|
||||||
|
|
||||||
def set_play_context(self, play_context):
|
|
||||||
self.play_context = play_context
|
|
||||||
|
|
||||||
def v2_playbook_on_start(self, playbook):
|
|
||||||
self.playbook = playbook
|
|
||||||
|
|
||||||
def v2_playbook_on_play_start(self, play):
|
|
||||||
# This gets called once for each play.. but we just issue a message once
|
|
||||||
# for the first one. One per "playbook"
|
|
||||||
if self.playbook:
|
|
||||||
# figure out where the playbook FILE is
|
|
||||||
path = os.path.abspath(self.playbook._file_name)
|
|
||||||
|
|
||||||
# Bail out early without publishing if we're in --check mode
|
|
||||||
if self.play_context.check_mode:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self.playbook_path:
|
|
||||||
fedmsg.publish(
|
|
||||||
modname="ansible", topic="playbook.start",
|
|
||||||
msg=dict(
|
|
||||||
playbook=path,
|
|
||||||
userid=getlogin(),
|
|
||||||
extra_vars=play._variable_manager.extra_vars,
|
|
||||||
inventory=play._variable_manager._inventory._sources,
|
|
||||||
playbook_checksum=secure_hash(path),
|
|
||||||
check=self.play_context.check_mode,
|
|
||||||
),
|
|
||||||
)
|
|
||||||
self.playbook_path = path
|
|
||||||
|
|
||||||
def v2_playbook_on_stats(self, stats):
|
|
||||||
if not self.playbook_path:
|
|
||||||
return
|
|
||||||
|
|
||||||
results = dict([(h, stats.summarize(h)) for h in stats.processed])
|
|
||||||
fedmsg.publish(
|
|
||||||
modname="ansible", topic="playbook.complete",
|
|
||||||
msg=dict(
|
|
||||||
playbook=self.playbook_path,
|
|
||||||
userid=getlogin(),
|
|
||||||
results=results,
|
|
||||||
),
|
|
||||||
)
|
|
|
@ -1,103 +0,0 @@
|
||||||
# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
|
|
||||||
# based on the log_plays example
|
|
||||||
# skvidal@fedoraproject.org
|
|
||||||
# rbean@redhat.com
|
|
||||||
# karsten@redhat.com changes for fedora-messaging
|
|
||||||
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
import os
|
|
||||||
import pwd
|
|
||||||
import logging
|
|
||||||
|
|
||||||
from fedora_messaging.api import Message, publish
|
|
||||||
from fedora_messaging.exceptions import PublishReturned, ConnectionException
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.plugins.callback import CallbackBase
|
|
||||||
except ImportError:
|
|
||||||
# Ansible v1 compat
|
|
||||||
CallbackBase = object
|
|
||||||
|
|
||||||
LOGGER = logging.getLogger(__name__)
|
|
||||||
|
|
||||||
def getlogin():
|
|
||||||
try:
|
|
||||||
user = os.getlogin()
|
|
||||||
except OSError as e:
|
|
||||||
user = pwd.getpwuid(os.geteuid())[0]
|
|
||||||
return user
|
|
||||||
|
|
||||||
|
|
||||||
class CallbackModule(CallbackBase):
|
|
||||||
""" Publish playbook starts and stops to fedora-messaging. """
|
|
||||||
|
|
||||||
playbook_path = None
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def playbook_on_play_start(self, pattern):
|
|
||||||
# This gets called once for each play.. but we just issue a message once
|
|
||||||
# for the first one. One per "playbook"
|
|
||||||
play = getattr(self, "play", None)
|
|
||||||
if play:
|
|
||||||
# figure out where the playbook FILE is
|
|
||||||
path = os.path.abspath(play.playbook.filename)
|
|
||||||
|
|
||||||
# Bail out early without publishing if we're in --check mode
|
|
||||||
if play.playbook.check:
|
|
||||||
return
|
|
||||||
|
|
||||||
if not self.playbook_path:
|
|
||||||
try:
|
|
||||||
msg = Message(
|
|
||||||
topic="ansible.playbook.start",
|
|
||||||
body={
|
|
||||||
'playbook': path,
|
|
||||||
'userid': getlogin(),
|
|
||||||
'extra_vars': play.playbook.extra_vars,
|
|
||||||
'inventory': play.playbook.inventory.host_list,
|
|
||||||
'playbook_checksum': play.playbook.check,
|
|
||||||
'check': play.playbook.check
|
|
||||||
}
|
|
||||||
)
|
|
||||||
publish(msg)
|
|
||||||
except PublishReturned as e:
|
|
||||||
LOGGER.warning(
|
|
||||||
"Fedora Messaging broker rejected message %s: %s", msg.id, e
|
|
||||||
)
|
|
||||||
except ConnectionException as e:
|
|
||||||
LOGGER.warning("Error sending message %s: %s", msg.id, e)
|
|
||||||
self.playbook_path = path
|
|
||||||
|
|
||||||
def playbook_on_stats(self, stats):
|
|
||||||
if not self.playbook_path:
|
|
||||||
return
|
|
||||||
|
|
||||||
results = dict([(h, stats.summarize(h)) for h in stats.processed])
|
|
||||||
try:
|
|
||||||
msg = Message(
|
|
||||||
topic="ansible.playbook.complete",
|
|
||||||
body={
|
|
||||||
'playbook': self.playbook_path,
|
|
||||||
'userid': getlogin(),
|
|
||||||
'results': results
|
|
||||||
}
|
|
||||||
)
|
|
||||||
publish(msg)
|
|
||||||
except PublishReturned as e:
|
|
||||||
LOGGER.warning("Fedora Messaging broker rejected message %s: %s", msg.id, e)
|
|
||||||
except ConnectionException as e:
|
|
||||||
LOGGER.warning("Error sending message %s: %s", msg.id, e)
|
|
|
@ -21,7 +21,11 @@ import os
|
||||||
import time
|
import time
|
||||||
import json
|
import json
|
||||||
import pwd
|
import pwd
|
||||||
from ansible import utils
|
|
||||||
|
try:
|
||||||
|
from ansible.utils.hashing import secure_hash
|
||||||
|
except ImportError:
|
||||||
|
from ansible.utils import md5 as secure_hash
|
||||||
|
|
||||||
try:
|
try:
|
||||||
from ansible.plugins.callback import CallbackBase
|
from ansible.plugins.callback import CallbackBase
|
||||||
|
@ -96,8 +100,8 @@ class LogMech(object):
|
||||||
def task_to_json(self, task):
|
def task_to_json(self, task):
|
||||||
res = {}
|
res = {}
|
||||||
res['task_name'] = task.name
|
res['task_name'] = task.name
|
||||||
res['task_module'] = task.module_name
|
res['task_module'] = task.action
|
||||||
res['task_args'] = task.module_args
|
res['task_args'] = task.args
|
||||||
if self.playbook_id == 'ansible-cmd':
|
if self.playbook_id == 'ansible-cmd':
|
||||||
res['task_userid'] = getlogin()
|
res['task_userid'] = getlogin()
|
||||||
for k in ("delegate_to", "environment", "with_first_found",
|
for k in ("delegate_to", "environment", "with_first_found",
|
||||||
|
@ -164,7 +168,7 @@ class CallbackModule(CallbackBase):
|
||||||
"""
|
"""
|
||||||
logs playbook results, per host, in /var/log/ansible/hosts
|
logs playbook results, per host, in /var/log/ansible/hosts
|
||||||
"""
|
"""
|
||||||
CALLBACK_NAME = 'logdetail'
|
CALLBACK_NAME = 'logdetail2'
|
||||||
CALLBACK_TYPE = 'notification'
|
CALLBACK_TYPE = 'notification'
|
||||||
CALLBACK_VERSION = 2.0
|
CALLBACK_VERSION = 2.0
|
||||||
CALLBACK_NEEDS_WHITELIST = True
|
CALLBACK_NEEDS_WHITELIST = True
|
||||||
|
@ -172,98 +176,65 @@ class CallbackModule(CallbackBase):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
self._task_count = 0
|
self._task_count = 0
|
||||||
self._play_count = 0
|
self._play_count = 0
|
||||||
|
self.task = None
|
||||||
|
self.playbook = None
|
||||||
|
|
||||||
def on_any(self, *args, **kwargs):
|
super(CallbackModule, self).__init__()
|
||||||
pass
|
|
||||||
|
|
||||||
|
def set_play_context(self, play_context):
|
||||||
|
self.play_context = play_context
|
||||||
|
|
||||||
def runner_on_failed(self, host, res, ignore_errors=False):
|
def v2_runner_on_failed(self, result, ignore_errors=False):
|
||||||
category = 'FAILED'
|
category = 'FAILED'
|
||||||
task = getattr(self,'task', None)
|
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
|
||||||
|
|
||||||
|
def v2_runner_on_ok(self, result):
|
||||||
def runner_on_ok(self, host, res):
|
|
||||||
category = 'OK'
|
category = 'OK'
|
||||||
task = getattr(self,'task', None)
|
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
|
||||||
|
|
||||||
|
def v2_runner_on_skipped(self, result):
|
||||||
def runner_on_error(self, host, res):
|
|
||||||
category = 'ERROR'
|
|
||||||
task = getattr(self,'task', None)
|
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
|
||||||
|
|
||||||
def runner_on_skipped(self, host, item=None):
|
|
||||||
category = 'SKIPPED'
|
category = 'SKIPPED'
|
||||||
task = getattr(self,'task', None)
|
|
||||||
res = {}
|
res = {}
|
||||||
res['item'] = item
|
res['item'] = self._get_item(getattr(result._result, 'results', {}))
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
logmech.log(result._host.get_name(), category, res, self.task, self._task_count)
|
||||||
|
|
||||||
def runner_on_unreachable(self, host, output):
|
def v2_runner_on_unreachable(self, result):
|
||||||
category = 'UNREACHABLE'
|
category = 'UNREACHABLE'
|
||||||
task = getattr(self,'task', None)
|
|
||||||
res = {}
|
res = {}
|
||||||
res['output'] = output
|
res['output'] = result._result
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
logmech.log(result._host.get_name(), category, res, self.task, self._task_count)
|
||||||
|
|
||||||
def runner_on_no_hosts(self):
|
def v2_runner_on_async_failed(self, result):
|
||||||
pass
|
|
||||||
|
|
||||||
def runner_on_async_poll(self, host, res, jid, clock):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def runner_on_async_ok(self, host, res, jid):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def runner_on_async_failed(self, host, res, jid):
|
|
||||||
category = 'ASYNC_FAILED'
|
category = 'ASYNC_FAILED'
|
||||||
task = getattr(self,'task', None)
|
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
||||||
logmech.log(host, category, res, task, self._task_count)
|
|
||||||
|
|
||||||
def playbook_on_start(self):
|
def v2_playbook_on_start(self, playbook):
|
||||||
pass
|
self.playbook = playbook
|
||||||
|
|
||||||
def playbook_on_notify(self, host, handler):
|
def v2_playbook_on_task_start(self, task, is_conditional):
|
||||||
pass
|
self.task = task
|
||||||
|
|
||||||
def playbook_on_no_hosts_matched(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def playbook_on_no_hosts_remaining(self):
|
|
||||||
pass
|
|
||||||
|
|
||||||
def playbook_on_task_start(self, name, is_conditional):
|
|
||||||
logmech._last_task_start = time.time()
|
logmech._last_task_start = time.time()
|
||||||
self._task_count += 1
|
self._task_count += 1
|
||||||
|
|
||||||
def playbook_on_vars_prompt(self, varname, private=True, prompt=None, encrypt=None, confirm=False, salt_size=None, salt=None, default=None):
|
def v2_playbook_on_setup(self):
|
||||||
pass
|
|
||||||
|
|
||||||
def playbook_on_setup(self):
|
|
||||||
self._task_count += 1
|
self._task_count += 1
|
||||||
pass
|
|
||||||
|
|
||||||
def playbook_on_import_for_host(self, host, imported_file):
|
def v2_playbook_on_import_for_host(self, result, imported_file):
|
||||||
task = getattr(self,'task', None)
|
|
||||||
res = {}
|
res = {}
|
||||||
res['imported_file'] = imported_file
|
res['imported_file'] = imported_file
|
||||||
logmech.log(host, 'IMPORTED', res, task)
|
logmech.log(result._host.get_name(), 'IMPORTED', res, self.task)
|
||||||
|
|
||||||
def playbook_on_not_import_for_host(self, host, missing_file):
|
def v2_playbook_on_not_import_for_host(self, result, missing_file):
|
||||||
task = getattr(self,'task', None)
|
|
||||||
res = {}
|
res = {}
|
||||||
res['missing_file'] = missing_file
|
res['missing_file'] = missing_file
|
||||||
logmech.log(host, 'NOTIMPORTED', res, task)
|
logmech.log(result._host.get_name(), 'NOTIMPORTED', res, self.task)
|
||||||
|
|
||||||
def playbook_on_play_start(self, pattern):
|
def v2_playbook_on_play_start(self, play):
|
||||||
self._task_count = 0
|
self._task_count = 0
|
||||||
|
|
||||||
play = getattr(self, 'play', None)
|
|
||||||
if play:
|
if play:
|
||||||
# figure out where the playbook FILE is
|
# figure out where the playbook FILE is
|
||||||
path = os.path.abspath(play.playbook.filename)
|
path = os.path.abspath(self.playbook._file_name)
|
||||||
|
|
||||||
# tel the logger what the playbook is
|
# tel the logger what the playbook is
|
||||||
logmech.playbook_id = path
|
logmech.playbook_id = path
|
||||||
|
@ -275,11 +246,11 @@ class CallbackModule(CallbackBase):
|
||||||
pb_info['playbook_start'] = time.time()
|
pb_info['playbook_start'] = time.time()
|
||||||
pb_info['playbook'] = path
|
pb_info['playbook'] = path
|
||||||
pb_info['userid'] = getlogin()
|
pb_info['userid'] = getlogin()
|
||||||
pb_info['extra_vars'] = play.playbook.extra_vars
|
pb_info['extra_vars'] = play._variable_manager.extra_vars
|
||||||
pb_info['inventory'] = play.playbook.inventory.host_list
|
pb_info['inventory'] = play._variable_manager._inventory._sources
|
||||||
pb_info['playbook_checksum'] = utils.md5(path)
|
pb_info['playbook_checksum'] = secure_hash(path)
|
||||||
pb_info['check'] = play.playbook.check
|
pb_info['check'] = self.play_context.check_mode
|
||||||
pb_info['diff'] = play.playbook.diff
|
pb_info['diff'] = self.play_context.diff
|
||||||
logmech.play_log(json.dumps(pb_info, indent=4))
|
logmech.play_log(json.dumps(pb_info, indent=4))
|
||||||
|
|
||||||
self._play_count += 1
|
self._play_count += 1
|
||||||
|
@ -287,21 +258,21 @@ class CallbackModule(CallbackBase):
|
||||||
info = {}
|
info = {}
|
||||||
info['play'] = play.name
|
info['play'] = play.name
|
||||||
info['hosts'] = play.hosts
|
info['hosts'] = play.hosts
|
||||||
info['transport'] = play.transport
|
info['transport'] = self.play_context.connection
|
||||||
info['number'] = self._play_count
|
info['number'] = self._play_count
|
||||||
info['check'] = play.playbook.check
|
info['check'] = self.play_context.check_mode
|
||||||
info['diff'] = play.playbook.diff
|
info['diff'] = self.play_context.diff
|
||||||
logmech.play_info = info
|
logmech.play_info = info
|
||||||
logmech.play_log(json.dumps(info, indent=4))
|
logmech.play_log(json.dumps(info, indent=4))
|
||||||
|
|
||||||
|
|
||||||
def playbook_on_stats(self, stats):
|
def v2_playbook_on_stats(self, stats):
|
||||||
results = {}
|
results = {}
|
||||||
for host in stats.processed.keys():
|
for host in stats.processed.keys():
|
||||||
results[host] = stats.summarize(host)
|
results[host] = stats.summarize(host)
|
||||||
logmech.log(host, 'STATS', results[host])
|
logmech.log(host, 'STATS', results[host])
|
||||||
logmech.play_log(json.dumps({'stats': results}, indent=4))
|
logmech.play_log(json.dumps({'stats': results}, indent=4))
|
||||||
logmech.play_log(json.dumps({'playbook_end': time.time()}, indent=4))
|
logmech.play_log(json.dumps({'playbook_end': time.time()}, indent=4))
|
||||||
print 'logs written to: %s' % logmech.logpath_play
|
print('logs written to: %s' % logmech.logpath_play)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,278 +0,0 @@
|
||||||
# (C) 2012, Michael DeHaan, <michael.dehaan@gmail.com>
|
|
||||||
# based on the log_plays example
|
|
||||||
# skvidal@fedoraproject.org
|
|
||||||
|
|
||||||
# Ansible is free software: you can redistribute it and/or modify
|
|
||||||
# it under the terms of the GNU General Public License as published by
|
|
||||||
# the Free Software Foundation, either version 3 of the License, or
|
|
||||||
# (at your option) any later version.
|
|
||||||
#
|
|
||||||
# Ansible is distributed in the hope that it will be useful,
|
|
||||||
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
# GNU General Public License for more details.
|
|
||||||
#
|
|
||||||
# You should have received a copy of the GNU General Public License
|
|
||||||
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
|
|
||||||
from __future__ import absolute_import
|
|
||||||
|
|
||||||
import os
|
|
||||||
import time
|
|
||||||
import json
|
|
||||||
import pwd
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.utils.hashing import secure_hash
|
|
||||||
except ImportError:
|
|
||||||
from ansible.utils import md5 as secure_hash
|
|
||||||
|
|
||||||
try:
|
|
||||||
from ansible.plugins.callback import CallbackBase
|
|
||||||
except ImportError:
|
|
||||||
# Ansible v1 compat
|
|
||||||
CallbackBase = object
|
|
||||||
|
|
||||||
TIME_FORMAT="%b %d %Y %H:%M:%S"
|
|
||||||
|
|
||||||
MSG_FORMAT="%(now)s\t%(count)s\t%(category)s\t%(name)s\t%(data)s\n"
|
|
||||||
|
|
||||||
LOG_PATH = '/var/log/ansible'
|
|
||||||
|
|
||||||
def getlogin():
|
|
||||||
try:
|
|
||||||
user = os.getlogin()
|
|
||||||
except OSError, e:
|
|
||||||
user = pwd.getpwuid(os.geteuid())[0]
|
|
||||||
return user
|
|
||||||
|
|
||||||
class LogMech(object):
|
|
||||||
def __init__(self):
|
|
||||||
self.started = time.time()
|
|
||||||
self.pid = str(os.getpid())
|
|
||||||
self._pb_fn = None
|
|
||||||
self._last_task_start = None
|
|
||||||
self.play_info = {}
|
|
||||||
self.logpath = LOG_PATH
|
|
||||||
if not os.path.exists(self.logpath):
|
|
||||||
try:
|
|
||||||
os.makedirs(self.logpath, mode=0750)
|
|
||||||
except OSError, e:
|
|
||||||
if e.errno != 17:
|
|
||||||
raise
|
|
||||||
|
|
||||||
# checksum of full playbook?
|
|
||||||
|
|
||||||
@property
|
|
||||||
def playbook_id(self):
|
|
||||||
if self._pb_fn:
|
|
||||||
return os.path.basename(self._pb_fn).replace('.yml', '').replace('.yaml', '')
|
|
||||||
else:
|
|
||||||
return "ansible-cmd"
|
|
||||||
|
|
||||||
@playbook_id.setter
|
|
||||||
def playbook_id(self, value):
|
|
||||||
self._pb_fn = value
|
|
||||||
|
|
||||||
@property
|
|
||||||
def logpath_play(self):
|
|
||||||
# this is all to get our path to look nice ish
|
|
||||||
tstamp = time.strftime('%Y/%m/%d/%H.%M.%S', time.localtime(self.started))
|
|
||||||
path = os.path.normpath(self.logpath + '/' + self.playbook_id + '/' + tstamp + '/')
|
|
||||||
|
|
||||||
if not os.path.exists(path):
|
|
||||||
try:
|
|
||||||
os.makedirs(path)
|
|
||||||
except OSError, e:
|
|
||||||
if e.errno != 17: # if it is not dir exists then raise it up
|
|
||||||
raise
|
|
||||||
|
|
||||||
return path
|
|
||||||
|
|
||||||
def play_log(self, content):
|
|
||||||
# record out playbook.log
|
|
||||||
# include path to playbook, checksums, user running playbook
|
|
||||||
# any args we can get back from the invocation
|
|
||||||
fd = open(self.logpath_play + '/' + 'playbook-' + self.pid + '.info', 'a')
|
|
||||||
fd.write('%s\n' % content)
|
|
||||||
fd.close()
|
|
||||||
|
|
||||||
def task_to_json(self, task):
|
|
||||||
res = {}
|
|
||||||
res['task_name'] = task.name
|
|
||||||
res['task_module'] = task.action
|
|
||||||
res['task_args'] = task.args
|
|
||||||
if self.playbook_id == 'ansible-cmd':
|
|
||||||
res['task_userid'] = getlogin()
|
|
||||||
for k in ("delegate_to", "environment", "with_first_found",
|
|
||||||
"local_action", "notified_by", "notify",
|
|
||||||
"register", "sudo", "sudo_user", "tags",
|
|
||||||
"transport", "when"):
|
|
||||||
v = getattr(task, k, None)
|
|
||||||
if v:
|
|
||||||
res['task_' + k] = v
|
|
||||||
|
|
||||||
return res
|
|
||||||
|
|
||||||
def log(self, host, category, data, task=None, count=0):
|
|
||||||
if not host:
|
|
||||||
host = 'HOSTMISSING'
|
|
||||||
|
|
||||||
if type(data) == dict:
|
|
||||||
name = data.get('module_name',None)
|
|
||||||
else:
|
|
||||||
name = "unknown"
|
|
||||||
|
|
||||||
|
|
||||||
# we're in setup - move the invocation info up one level
|
|
||||||
if 'invocation' in data:
|
|
||||||
invoc = data['invocation']
|
|
||||||
if not name and 'module_name' in invoc:
|
|
||||||
name = invoc['module_name']
|
|
||||||
|
|
||||||
#don't add this since it can often contain complete passwords :(
|
|
||||||
del(data['invocation'])
|
|
||||||
|
|
||||||
if task:
|
|
||||||
name = task.name
|
|
||||||
data['task_start'] = self._last_task_start
|
|
||||||
data['task_end'] = time.time()
|
|
||||||
data.update(self.task_to_json(task))
|
|
||||||
|
|
||||||
if 'task_userid' not in data:
|
|
||||||
data['task_userid'] = getlogin()
|
|
||||||
|
|
||||||
if category == 'OK' and data.get('changed', False):
|
|
||||||
category = 'CHANGED'
|
|
||||||
|
|
||||||
if self.play_info.get('check', False) and self.play_info.get('diff', False):
|
|
||||||
category = 'CHECK_DIFF:' + category
|
|
||||||
elif self.play_info.get('check', False):
|
|
||||||
category = 'CHECK:' + category
|
|
||||||
|
|
||||||
# Sometimes this is None.. othertimes it's fine. Othertimes it has
|
|
||||||
# trailing whitespace that kills logview. Strip that, when possible.
|
|
||||||
if name:
|
|
||||||
name = name.strip()
|
|
||||||
|
|
||||||
sanitize_host = host.replace(' ', '_').replace('>', '-')
|
|
||||||
fd = open(self.logpath_play + '/' + sanitize_host + '.log', 'a')
|
|
||||||
now = time.strftime(TIME_FORMAT, time.localtime())
|
|
||||||
fd.write(MSG_FORMAT % dict(now=now, name=name, count=count, category=category, data=json.dumps(data)))
|
|
||||||
fd.close()
|
|
||||||
|
|
||||||
|
|
||||||
logmech = LogMech()
|
|
||||||
|
|
||||||
class CallbackModule(CallbackBase):
|
|
||||||
"""
|
|
||||||
logs playbook results, per host, in /var/log/ansible/hosts
|
|
||||||
"""
|
|
||||||
CALLBACK_NAME = 'logdetail2'
|
|
||||||
CALLBACK_TYPE = 'notification'
|
|
||||||
CALLBACK_VERSION = 2.0
|
|
||||||
CALLBACK_NEEDS_WHITELIST = True
|
|
||||||
|
|
||||||
def __init__(self):
|
|
||||||
self._task_count = 0
|
|
||||||
self._play_count = 0
|
|
||||||
self.task = None
|
|
||||||
self.playbook = None
|
|
||||||
|
|
||||||
super(CallbackModule, self).__init__()
|
|
||||||
|
|
||||||
def set_play_context(self, play_context):
|
|
||||||
self.play_context = play_context
|
|
||||||
|
|
||||||
def v2_runner_on_failed(self, result, ignore_errors=False):
|
|
||||||
category = 'FAILED'
|
|
||||||
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
|
||||||
|
|
||||||
def v2_runner_on_ok(self, result):
|
|
||||||
category = 'OK'
|
|
||||||
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
|
||||||
|
|
||||||
def v2_runner_on_skipped(self, result):
|
|
||||||
category = 'SKIPPED'
|
|
||||||
res = {}
|
|
||||||
res['item'] = self._get_item(getattr(result._result, 'results', {}))
|
|
||||||
logmech.log(result._host.get_name(), category, res, self.task, self._task_count)
|
|
||||||
|
|
||||||
def v2_runner_on_unreachable(self, result):
|
|
||||||
category = 'UNREACHABLE'
|
|
||||||
res = {}
|
|
||||||
res['output'] = result._result
|
|
||||||
logmech.log(result._host.get_name(), category, res, self.task, self._task_count)
|
|
||||||
|
|
||||||
def v2_runner_on_async_failed(self, result):
|
|
||||||
category = 'ASYNC_FAILED'
|
|
||||||
logmech.log(result._host.get_name(), category, result._result, self.task, self._task_count)
|
|
||||||
|
|
||||||
def v2_playbook_on_start(self, playbook):
|
|
||||||
self.playbook = playbook
|
|
||||||
|
|
||||||
def v2_playbook_on_task_start(self, task, is_conditional):
|
|
||||||
self.task = task
|
|
||||||
logmech._last_task_start = time.time()
|
|
||||||
self._task_count += 1
|
|
||||||
|
|
||||||
def v2_playbook_on_setup(self):
|
|
||||||
self._task_count += 1
|
|
||||||
|
|
||||||
def v2_playbook_on_import_for_host(self, result, imported_file):
|
|
||||||
res = {}
|
|
||||||
res['imported_file'] = imported_file
|
|
||||||
logmech.log(result._host.get_name(), 'IMPORTED', res, self.task)
|
|
||||||
|
|
||||||
def v2_playbook_on_not_import_for_host(self, result, missing_file):
|
|
||||||
res = {}
|
|
||||||
res['missing_file'] = missing_file
|
|
||||||
logmech.log(result._host.get_name(), 'NOTIMPORTED', res, self.task)
|
|
||||||
|
|
||||||
def v2_playbook_on_play_start(self, play):
|
|
||||||
self._task_count = 0
|
|
||||||
|
|
||||||
if play:
|
|
||||||
# figure out where the playbook FILE is
|
|
||||||
path = os.path.abspath(self.playbook._file_name)
|
|
||||||
|
|
||||||
# tel the logger what the playbook is
|
|
||||||
logmech.playbook_id = path
|
|
||||||
|
|
||||||
# if play count == 0
|
|
||||||
# write out playbook info now
|
|
||||||
if not self._play_count:
|
|
||||||
pb_info = {}
|
|
||||||
pb_info['playbook_start'] = time.time()
|
|
||||||
pb_info['playbook'] = path
|
|
||||||
pb_info['userid'] = getlogin()
|
|
||||||
pb_info['extra_vars'] = play._variable_manager.extra_vars
|
|
||||||
pb_info['inventory'] = play._variable_manager._inventory._sources
|
|
||||||
pb_info['playbook_checksum'] = secure_hash(path)
|
|
||||||
pb_info['check'] = self.play_context.check_mode
|
|
||||||
pb_info['diff'] = self.play_context.diff
|
|
||||||
logmech.play_log(json.dumps(pb_info, indent=4))
|
|
||||||
|
|
||||||
self._play_count += 1
|
|
||||||
# then write per-play info that doesn't duplcate the playbook info
|
|
||||||
info = {}
|
|
||||||
info['play'] = play.name
|
|
||||||
info['hosts'] = play.hosts
|
|
||||||
info['transport'] = self.play_context.connection
|
|
||||||
info['number'] = self._play_count
|
|
||||||
info['check'] = self.play_context.check_mode
|
|
||||||
info['diff'] = self.play_context.diff
|
|
||||||
logmech.play_info = info
|
|
||||||
logmech.play_log(json.dumps(info, indent=4))
|
|
||||||
|
|
||||||
|
|
||||||
def v2_playbook_on_stats(self, stats):
|
|
||||||
results = {}
|
|
||||||
for host in stats.processed.keys():
|
|
||||||
results[host] = stats.summarize(host)
|
|
||||||
logmech.log(host, 'STATS', results[host])
|
|
||||||
logmech.play_log(json.dumps({'stats': results}, indent=4))
|
|
||||||
logmech.play_log(json.dumps({'playbook_end': time.time()}, indent=4))
|
|
||||||
print('logs written to: %s' % logmech.logpath_play)
|
|
||||||
|
|
||||||
|
|
|
@ -77,7 +77,7 @@ roles_path = {{ ansible_base }}/ansible/roles:/usr/share/ansible/roles
|
||||||
|
|
||||||
# enable callback plugins, they can output to stdout but cannot be 'stdout' type.
|
# enable callback plugins, they can output to stdout but cannot be 'stdout' type.
|
||||||
#callback_whitelist = timer, mail
|
#callback_whitelist = timer, mail
|
||||||
callback_whitelist = fedmsg_callback2,profile_tasks,logdetail2
|
callback_whitelist = fedora_messaging,profile_roles,profile_tasks,logdetail
|
||||||
|
|
||||||
# Determine whether includes in tasks and handlers are "static" by
|
# Determine whether includes in tasks and handlers are "static" by
|
||||||
# default. As of 2.0, includes are dynamic by default. Setting these
|
# default. As of 2.0, includes are dynamic by default. Setting these
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue