allow config for callbaks and some fixes

* only complain about ini deprecation if value is set
 * set plugin config for stdout and other types
 * updated plugin docs, moved several plugins to new config
 * finished ssh docs
 * fixed some issues seen in plugins while modifying docs
 * placeholder for 'required'
 * callbacks must use _plugin_options as _options already in use
This commit is contained in:
Brian Coca 2017-09-10 17:53:49 -04:00 committed by Toshio Kuratomi
parent 942b6fb9bc
commit 869a318492
19 changed files with 480 additions and 402 deletions

View file

@ -1,20 +1,45 @@
# (C) 2014-2015, Matt Martz <matt@sivel.net>
# (C) 2017 Ansible Project
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
# This file is part of Ansible
#
# 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/>.
'''
DOCUMENTATION:
callback: slack
callback_type: notification
requirements:
- whitelist in configuration
- prettytable (python library)
short_description: Sends play events to a Slack channel
version_added: "2.1"
description:
- This is an ansible callback plugin that sends status updates to a Slack channel during playbook execution.
- Before 2.4 only environment variables were available for configuring this plugin
options:
webhook_url:
required: True
description: Slack Webhook URL
env:
- name: SLACK_WEBHOOK_URL
ini:
- section: callback_slack
key: webhook_url
channel:
default: "#ansible"
description: Slack room to post in.
env:
- name: SLACK_CHANNEL
ini:
- section: callback_slack
key: channel
username:
description: Username to post as.
env:
- name: SLACK_USERNAME
default: ansible
ini:
- section: callback_slack
key: username
'''
# Make coding more python3-ish
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
@ -42,17 +67,6 @@ except ImportError:
class CallbackModule(CallbackBase):
"""This is an ansible callback plugin that sends status
updates to a Slack channel during playbook execution.
This plugin makes use of the following environment variables:
SLACK_WEBHOOK_URL (required): Slack Webhook URL
SLACK_CHANNEL (optional): Slack room to post in. Default: #ansible
SLACK_USERNAME (optional): Username to post as. Default: ansible
SLACK_INVOCATION (optional): Show command line invocation
details. Default: False
Requires:
prettytable
"""
CALLBACK_VERSION = 2.0
CALLBACK_TYPE = 'notification'
@ -64,9 +78,9 @@ class CallbackModule(CallbackBase):
self.disabled = False
if cli:
self._options = cli.options
self._plugin_options = cli.options
else:
self._options = None
self._plugin_options = None
super(CallbackModule, self).__init__(display=display)
@ -76,13 +90,10 @@ class CallbackModule(CallbackBase):
'installed. Disabling the Slack callback '
'plugin.')
self.webhook_url = os.getenv('SLACK_WEBHOOK_URL')
self.channel = os.getenv('SLACK_CHANNEL', '#ansible')
self.username = os.getenv('SLACK_USERNAME', 'ansible')
self.show_invocation = boolean(
os.getenv('SLACK_INVOCATION', self._display.verbosity > 1),
strict=False
)
self.webhook_url = self._plugin_options['webook_url']
self.channel = self._plugin_options['channel']
self.username = self._plugin_options['username']
self.show_invocation = (self._display.verbosity > 1)
if self.webhook_url is None:
self.disabled = True
@ -91,12 +102,13 @@ class CallbackModule(CallbackBase):
'the `SLACK_WEBHOOK_URL` environment '
'variable.')
self.playbook_name = None
else:
self.playbook_name = None
# This is a 6 character identifier provided with each message
# This makes it easier to correlate messages when there are more
# than 1 simultaneous playbooks running
self.guid = uuid.uuid4().hex[:6]
# This is a 6 character identifier provided with each message
# This makes it easier to correlate messages when there are more
# than 1 simultaneous playbooks running
self.guid = uuid.uuid4().hex[:6]
def send_msg(self, attachments):
payload = {
@ -125,13 +137,13 @@ class CallbackModule(CallbackBase):
'*Playbook initiated* (_%s_)' % self.guid
]
invocation_items = []
if self._options and self.show_invocation:
tags = self._options.tags
skip_tags = self._options.skip_tags
extra_vars = self._options.extra_vars
subset = self._options.subset
if self._plugin_options and self.show_invocation:
tags = self._plugin_options.tags
skip_tags = self._plugin_options.skip_tags
extra_vars = self._plugin_options.extra_vars
subset = self._plugin_options.subset
inventory = os.path.basename(
os.path.realpath(self._options.inventory)
os.path.realpath(self._plugin_options.inventory)
)
invocation_items.append('Inventory: %s' % inventory)
@ -145,7 +157,7 @@ class CallbackModule(CallbackBase):
invocation_items.append('Extra Vars: %s' %
' '.join(extra_vars))
title.append('by *%s*' % self._options.remote_user)
title.append('by *%s*' % self._plugin_options.remote_user)
title.append('\n\n*%s*' % self.playbook_name)
msg_items = [' '.join(title)]