| 1 |
#!/usr/bin/python |
| 2 |
|
| 3 |
# Copyright 2014, Max Riveiro, <[email protected]> |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: rollbar_deployment |
| 11 |
author: "Max Riveiro (@kavu)" |
| 12 |
short_description: Notify Rollbar about app deployments |
| 13 |
description: |
| 14 |
- Notify Rollbar about app deployments (see U(https://rollbar.com/docs/deploys_other/)). |
| 15 |
extends_documentation_fragment: |
| 16 |
- community.general._attributes |
| 17 |
attributes: |
| 18 |
check_mode: |
| 19 |
support: full |
| 20 |
diff_mode: |
| 21 |
support: none |
| 22 |
options: |
| 23 |
token: |
| 24 |
type: str |
| 25 |
description: |
| 26 |
- Your project access token. |
| 27 |
required: true |
| 28 |
environment: |
| 29 |
type: str |
| 30 |
description: |
| 31 |
- Name of the environment being deployed, for example V(production). |
| 32 |
required: true |
| 33 |
revision: |
| 34 |
type: str |
| 35 |
description: |
| 36 |
- Revision number/sha being deployed. |
| 37 |
required: true |
| 38 |
user: |
| 39 |
type: str |
| 40 |
description: |
| 41 |
- User who deployed. |
| 42 |
rollbar_user: |
| 43 |
type: str |
| 44 |
description: |
| 45 |
- Rollbar username of the user who deployed. |
| 46 |
comment: |
| 47 |
type: str |
| 48 |
description: |
| 49 |
- Deploy comment (for example what is being deployed). |
| 50 |
url: |
| 51 |
type: str |
| 52 |
description: |
| 53 |
- Optional URL to submit the notification to. |
| 54 |
default: 'https://api.rollbar.com/api/1/deploy/' |
| 55 |
validate_certs: |
| 56 |
description: |
| 57 |
- If V(false), SSL certificates for the target URL are not validated. This should only be used on personally controlled |
| 58 |
sites using self-signed certificates. |
| 59 |
default: true |
| 60 |
type: bool |
| 61 |
""" |
| 62 |
|
| 63 |
EXAMPLES = r""" |
| 64 |
- name: Rollbar deployment notification |
| 65 |
community.general.rollbar_deployment: |
| 66 |
token: AAAAAA |
| 67 |
environment: staging |
| 68 |
user: ansible |
| 69 |
revision: '4.2' |
| 70 |
rollbar_user: admin |
| 71 |
comment: Test Deploy |
| 72 |
|
| 73 |
- name: Notify rollbar about current git revision deployment by current user |
| 74 |
community.general.rollbar_deployment: |
| 75 |
token: "{{ rollbar_access_token }}" |
| 76 |
environment: production |
| 77 |
revision: "{{ lookup('pipe', 'git rev-parse HEAD') }}" |
| 78 |
user: "{{ lookup('env', 'USER') }}" |
| 79 |
""" |
| 80 |
|
| 81 |
import traceback |
| 82 |
from urllib.parse import urlencode |
| 83 |
|
| 84 |
from ansible.module_utils.basic import AnsibleModule |
| 85 |
from ansible.module_utils.urls import fetch_url |
| 86 |
|
| 87 |
|
| 88 |
def main(): |
| 89 |
module = AnsibleModule( |
| 90 |
argument_spec=dict( |
| 91 |
token=dict(required=True, no_log=True), |
| 92 |
environment=dict(required=True), |
| 93 |
revision=dict(required=True), |
| 94 |
user=dict(), |
| 95 |
rollbar_user=dict(), |
| 96 |
comment=dict(), |
| 97 |
url=dict(default="https://api.rollbar.com/api/1/deploy/"), |
| 98 |
validate_certs=dict(default=True, type="bool"), |
| 99 |
), |
| 100 |
supports_check_mode=True, |
| 101 |
) |
| 102 |
|
| 103 |
if module.check_mode: |
| 104 |
module.exit_json(changed=True) |
| 105 |
|
| 106 |
params = dict( |
| 107 |
access_token=module.params["token"], |
| 108 |
environment=module.params["environment"], |
| 109 |
revision=module.params["revision"], |
| 110 |
) |
| 111 |
|
| 112 |
if module.params["user"]: |
| 113 |
params["local_username"] = module.params["user"] |
| 114 |
|
| 115 |
if module.params["rollbar_user"]: |
| 116 |
params["rollbar_username"] = module.params["rollbar_user"] |
| 117 |
|
| 118 |
if module.params["comment"]: |
| 119 |
params["comment"] = module.params["comment"] |
| 120 |
|
| 121 |
url = module.params.get("url") |
| 122 |
|
| 123 |
try: |
| 124 |
data = urlencode(params) |
| 125 |
response, info = fetch_url(module, url, data=data, method="POST") |
| 126 |
except Exception as e: |
| 127 |
module.fail_json(msg=f"Unable to notify Rollbar: {e}", exception=traceback.format_exc()) |
| 128 |
else: |
| 129 |
if info["status"] == 200: |
| 130 |
module.exit_json(changed=True) |
| 131 |
else: |
| 132 |
module.fail_json(msg=f"HTTP result code: {info['status']} connecting to {url}") |
| 133 |
|
| 134 |
|
| 135 |
if __name__ == "__main__": |
| 136 |
main() |