|
|
@ -27,6 +27,8 @@ description:
|
|
|
|
- Sends an email with a SendGrid account through their API, not through
|
|
|
|
- Sends an email with a SendGrid account through their API, not through
|
|
|
|
the SMTP service.
|
|
|
|
the SMTP service.
|
|
|
|
notes:
|
|
|
|
notes:
|
|
|
|
|
|
|
|
- This module is non-idempotent because it sends an email through the
|
|
|
|
|
|
|
|
external API. It is idempotent only in the case that the module fails.
|
|
|
|
- Like the other notification modules, this one requires an external
|
|
|
|
- Like the other notification modules, this one requires an external
|
|
|
|
dependency to work. In this case, you'll need an active SendGrid
|
|
|
|
dependency to work. In this case, you'll need an active SendGrid
|
|
|
|
account.
|
|
|
|
account.
|
|
|
@ -51,31 +53,32 @@ options:
|
|
|
|
the desired subject for the email
|
|
|
|
the desired subject for the email
|
|
|
|
required: true
|
|
|
|
required: true
|
|
|
|
|
|
|
|
|
|
|
|
requirements: [ urllib, urllib2 ]
|
|
|
|
|
|
|
|
author: Matt Makai
|
|
|
|
author: Matt Makai
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
EXAMPLES = '''
|
|
|
|
EXAMPLES = '''
|
|
|
|
# send an email to a single recipient that the deployment was successful
|
|
|
|
# send an email to a single recipient that the deployment was successful
|
|
|
|
- local_action: sendgrid
|
|
|
|
- sendgrid:
|
|
|
|
username={{ sendgrid_username }}
|
|
|
|
username: "{{ sendgrid_username }}"
|
|
|
|
password={{ sendgrid_password }}
|
|
|
|
password: "{{ sendgrid_password }}"
|
|
|
|
from_address="ansible@mycompany.com"
|
|
|
|
from_address: "ansible@mycompany.com"
|
|
|
|
to_addresses:
|
|
|
|
to_addresses:
|
|
|
|
- "ops@mycompany.com"
|
|
|
|
- "ops@mycompany.com"
|
|
|
|
subject="Deployment success."
|
|
|
|
subject: "Deployment success."
|
|
|
|
body="The most recent Ansible deployment was successful."
|
|
|
|
body: "The most recent Ansible deployment was successful."
|
|
|
|
|
|
|
|
delegate_to: localhost
|
|
|
|
|
|
|
|
|
|
|
|
# send an email to more than one recipient that the build failed
|
|
|
|
# send an email to more than one recipient that the build failed
|
|
|
|
- local_action: sendgrid
|
|
|
|
- sendgrid
|
|
|
|
username={{ sendgrid_username }}
|
|
|
|
username: "{{ sendgrid_username }}"
|
|
|
|
password={{ sendgrid_password }}
|
|
|
|
password: "{{ sendgrid_password }}"
|
|
|
|
from_address="build@mycompany.com"
|
|
|
|
from_address: "build@mycompany.com"
|
|
|
|
to_addresses:
|
|
|
|
to_addresses:
|
|
|
|
- "ops@mycompany.com"
|
|
|
|
- "ops@mycompany.com"
|
|
|
|
- "devteam@mycompany.com"
|
|
|
|
- "devteam@mycompany.com"
|
|
|
|
subject="Build failure!."
|
|
|
|
subject: "Build failure!."
|
|
|
|
body="Unable to pull source repository from Git server."
|
|
|
|
body: "Unable to pull source repository from Git server."
|
|
|
|
|
|
|
|
delegate_to: localhost
|
|
|
|
'''
|
|
|
|
'''
|
|
|
|
|
|
|
|
|
|
|
|
# =======================================
|
|
|
|
# =======================================
|
|
|
@ -91,13 +94,15 @@ import base64
|
|
|
|
def post_sendgrid_api(module, username, password, from_address, to_addresses,
|
|
|
|
def post_sendgrid_api(module, username, password, from_address, to_addresses,
|
|
|
|
subject, body):
|
|
|
|
subject, body):
|
|
|
|
SENDGRID_URI = "https://api.sendgrid.com/api/mail.send.json"
|
|
|
|
SENDGRID_URI = "https://api.sendgrid.com/api/mail.send.json"
|
|
|
|
AGENT = "Ansible/1.7"
|
|
|
|
AGENT = "Ansible"
|
|
|
|
data = {'api_user': username, 'api_key':password,
|
|
|
|
data = {'api_user': username, 'api_key':password,
|
|
|
|
'from':from_address, 'subject': subject, 'text': body}
|
|
|
|
'from':from_address, 'subject': subject, 'text': body}
|
|
|
|
encoded_data = urllib.urlencode(data)
|
|
|
|
encoded_data = urllib.urlencode(data)
|
|
|
|
to_addresses_api = ''
|
|
|
|
to_addresses_api = ''
|
|
|
|
for recipient in to_addresses:
|
|
|
|
for recipient in to_addresses:
|
|
|
|
to_addresses_api += '&to[]=%s' % str(recipient)
|
|
|
|
if isinstance(recipient, unicode):
|
|
|
|
|
|
|
|
recipient = recipient.encode('utf-8')
|
|
|
|
|
|
|
|
to_addresses_api += '&to[]=%s' % recipient
|
|
|
|
encoded_data += to_addresses_api
|
|
|
|
encoded_data += to_addresses_api
|
|
|
|
request = urllib2.Request(SENDGRID_URI)
|
|
|
|
request = urllib2.Request(SENDGRID_URI)
|
|
|
|
request.add_header('User-Agent', AGENT)
|
|
|
|
request.add_header('User-Agent', AGENT)
|
|
|
@ -133,7 +138,7 @@ def main():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
response = post_sendgrid_api(module, username, password,
|
|
|
|
response = post_sendgrid_api(module, username, password,
|
|
|
|
from_address, to_addresses, subject, body)
|
|
|
|
from_address, to_addresses, subject, body)
|
|
|
|
except Exception, e:
|
|
|
|
except Exception:
|
|
|
|
module.fail_json(msg="unable to send email through SendGrid API")
|
|
|
|
module.fail_json(msg="unable to send email through SendGrid API")
|
|
|
|
|
|
|
|
|
|
|
|
module.exit_json(msg=subject, changed=False)
|
|
|
|
module.exit_json(msg=subject, changed=False)
|
|
|
|