# coding=utf8
# the above tag defines encoding for this document and is for Python 2.x compatibility
import re
regex = r"(?m)^DOCUMENTATION.*(\"{3}|'{3})\n-*\n?([\s\S]+?)^\1[\s\S]*"
test_str = ("#!/usr/bin/python\n"
"# -*- coding: utf-8 -*-\n\n"
"# (c) 2014, Ramon de la Fuente <ramon@delafuente.nl>\n"
"#\n"
"# This file is part of Ansible\n"
"#\n"
"# Ansible is free software: you can redistribute it and/or modify\n"
"# it under the terms of the GNU General Public License as published by\n"
"# the Free Software Foundation, either version 3 of the License, or\n"
"# (at your option) any later version.\n"
"#\n"
"# Ansible is distributed in the hope that it will be useful,\n"
"# but WITHOUT ANY WARRANTY; without even the implied warranty of\n"
"# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the\n"
"# GNU General Public License for more details.\n"
"#\n"
"# You should have received a copy of the GNU General Public License\n"
"# along with Ansible. If not, see <http://www.gnu.org/licenses/>.\n\n"
"DOCUMENTATION = \"\"\"\n"
"module: slack\n"
"short_description: Send Slack notifications\n"
"description:\n"
" - The M(slack) module sends notifications to U(http://slack.com) via the Incoming WebHook integration\n"
"version_added: 1.6\n"
"author: Ramon de la Fuente <ramon@delafuente.nl>\n"
"options:\n"
" domain:\n"
" description:\n"
" - Slack (sub)domain for your environment without protocol.\n"
" (i.e. C(future500.slack.com))\n"
" required: true\n"
" token:\n"
" description:\n"
" - Slack integration token\n"
" required: true\n"
" msg:\n"
" description:\n"
" - Message to send.\n"
" required: true\n"
" channel:\n"
" description:\n"
" - Channel to send the message to. If absent, the message goes to the channel selected for the I(token).\n"
" required: false\n"
" username:\n"
" description:\n"
" - This is the sender of the message.\n"
" required: false\n"
" default: ansible\n"
" icon_url:\n"
" description:\n"
" - Url for the message sender's icon (default C(http://www.ansible.com/favicon.ico))\n"
" required: false\n"
" icon_emoji:\n"
" description:\n"
" - Emoji for the message sender. See Slack documentation for options.\n"
" (if I(icon_emoji) is set, I(icon_url) will not be used)\n"
" required: false\n"
" link_names:\n"
" description:\n"
" - Automatically create links for channels and usernames in I(msg).\n"
" required: false\n"
" default: 1\n"
" choices:\n"
" - 1\n"
" - 0\n"
" parse:\n"
" description:\n"
" - Setting for the message parser at Slack\n"
" required: false\n"
" choices:\n"
" - 'full'\n"
" - 'none'\n"
" validate_certs:\n"
" description:\n"
" - If C(no), SSL certificates will not be validated. This should only be used\n"
" on personally controlled sites using self-signed certificates.\n"
" required: false\n"
" default: 'yes'\n"
" choices:\n"
" - 'yes'\n"
" - 'no'\n"
"\"\"\"\n\n"
"EXAMPLES = \"\"\"\n"
"- name: Send notification message via Slack\n"
" local_action:\n"
" module: slack\n"
" domain: future500.slack.com\n"
" token: thetokengeneratedbyslack\n"
" msg: \"{{ inventory_hostname }} completed\"\n\n"
"- name: Send notification message via Slack all options\n"
" local_action:\n"
" module: slack\n"
" domain: future500.slack.com\n"
" token: thetokengeneratedbyslack\n"
" msg: \"{{ inventory_hostname }} completed\"\n"
" channel: \"#ansible\"\n"
" username: \"Ansible on {{ inventory_hostname }}\"\n"
" icon_url: \"http://www.example.com/some-image-file.png\"\n"
" link_names: 0\n"
" parse: 'none'\n\n"
"\"\"\"\n\n\n"
"SLACK_INCOMING_WEBHOOK = 'https://%s/services/hooks/incoming-webhook?token=%s'\n\n"
"def build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse):\n"
" payload = dict(text=text)\n\n"
" if channel is not None:\n"
" payload['channel'] = channel if (channel[0] == '#') else '#'+channel\n"
" if username is not None:\n"
" payload['username'] = username\n"
" if icon_emoji is not None:\n"
" payload['icon_emoji'] = icon_emoji\n"
" else:\n"
" payload['icon_url'] = icon_url\n"
" if link_names is not None:\n"
" payload['link_names'] = link_names\n"
" if parse is not None:\n"
" payload['parse'] = parse\n\n"
" payload=\"payload=\" + module.jsonify(payload)\n"
" return payload\n\n"
"def do_notify_slack(module, domain, token, payload):\n"
" slack_incoming_webhook = SLACK_INCOMING_WEBHOOK % (domain, token)\n\n"
" response, info = fetch_url(module, slack_incoming_webhook, data=payload)\n"
" if info['status'] != 200:\n"
" obscured_incoming_webhook = SLACK_INCOMING_WEBHOOK % (domain, '[obscured]')\n"
" module.fail_json(msg=\" failed to send %s to %s: %s\" % (payload, obscured_incoming_webhook, info['msg']))\n\n"
"def main():\n"
" module = AnsibleModule(\n"
" argument_spec = dict(\n"
" domain = dict(type='str', required=True),\n"
" token = dict(type='str', required=True),\n"
" msg = dict(type='str', required=True),\n"
" channel = dict(type='str', default=None),\n"
" username = dict(type='str', default='Ansible'),\n"
" icon_url = dict(type='str', default='http://www.ansible.com/favicon.ico'),\n"
" icon_emoji = dict(type='str', default=None),\n"
" link_names = dict(type='int', default=1, choices=[0,1]),\n"
" parse = dict(type='str', default=None, choices=['none', 'full']),\n\n"
" validate_certs = dict(default='yes', type='bool'),\n"
" )\n"
" )\n\n"
" domain = module.params['domain']\n"
" token = module.params['token']\n"
" text = module.params['msg']\n"
" channel = module.params['channel']\n"
" username = module.params['username']\n"
" icon_url = module.params['icon_url']\n"
" icon_emoji = module.params['icon_emoji']\n"
" link_names = module.params['link_names']\n"
" parse = module.params['parse']\n\n"
" payload = build_payload_for_slack(module, text, channel, username, icon_url, icon_emoji, link_names, parse)\n"
" do_notify_slack(module, domain, token, payload)\n\n"
" module.exit_json(msg=\"OK\")\n\n"
"# import module snippets\n"
"from ansible.module_utils.basic import *\n"
"from ansible.module_utils.urls import *\n"
"main()")
subst = "---\\n\\3"
# You can manually specify the number of replacements by changing the 4th argument
result = re.sub(regex, subst, test_str, 1)
if result:
print (result)
# Note: for Python 2.7 compatibility, use ur"" to prefix the regex and u"" to prefix the test string and substitution.
Please keep in mind that these code samples are automatically generated and are not guaranteed to work. If you find any syntax errors, feel free to submit a bug report. For a full regex reference for Python, please visit: https://docs.python.org/3/library/re.html