community.general.sendgrid 模块 – 使用 SendGrid API 发送电子邮件

注意

此模块是 community.general 集合 (版本 10.1.0) 的一部分。

如果您使用的是 ansible 包,则可能已安装此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list

要安装它,请使用: ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,请参阅 要求 获取详细信息。

要在剧本中使用它,请指定: community.general.sendgrid

概要

  • 通过 SendGrid 的 API(而不是 SMTP 服务)使用 SendGrid 帐户发送电子邮件。

要求

执行此模块的主机需要以下要求。

  • sendgrid Python 库 1.6.22 或更低版本(支持 Sendgrid API V2)

参数

参数

注释

api_key

字符串

要使用的 Sendgrid API 密钥,替代用户名/密码。

attachments

列表 / 元素=路径

要附加的文件的相对路径或显式路径列表(根据 SendGrid 文档,限制为 7MB)。

bcc

列表 / 元素=字符串

要密送的电子邮件地址列表。

body

字符串 / 必需

电子邮件正文内容。

cc

列表 / 元素=字符串

要抄送的电子邮件地址列表。

from_address

字符串 / 必需

电子邮件“发件人”字段中的地址。

from_name

字符串

您希望显示在发件人字段中的名称,例如“John Doe”。

headers

字典

作为标头传递的字典。

html_body

布尔值

正文是否应呈现为 html 内容。

选项

  • false ← (默认)

  • true

password

字符串

与用户名对应的密码。

仅当未提供 api_key 时才需要。

subject

字符串 / 必需

所需的电子邮件主题。

to_addresses

列表 / 元素=字符串 / 必需

包含一个或多个收件人电子邮件地址的列表。

username

字符串

登录 SendGrid 帐户的用户名。

仅当未提供 api_key 时才需要。

属性

属性

支持

描述

check_mode

支持:完全支持

可以在 check_mode 下运行,并在不修改目标的情况下返回更改状态预测。

diff_mode

支持:不支持

处于差异模式时,将返回有关已更改内容(或可能需要在 check_mode 中更改的内容)的详细信息。

备注

注意

  • 此模块是非幂等的,因为它通过外部 API 发送电子邮件。只有在模块失败的情况下,它才是幂等的。

  • 与其他通知模块一样,此模块需要外部依赖项才能工作。在这种情况下,您需要一个活动的 SendGrid 帐户。

  • 要使用 api_key、cc、bcc、attachments、from_name、html_body、headers,您必须安装 sendgrid (pip install sendgrid)

示例

- name: Send an email to a single recipient that the deployment was successful
  community.general.sendgrid:
    username: "{{ sendgrid_username }}"
    password: "{{ sendgrid_password }}"
    from_address: "[email protected]"
    to_addresses:
      - "[email protected]"
    subject: "Deployment success."
    body: "The most recent Ansible deployment was successful."
  delegate_to: localhost

- name: Send an email to more than one recipient that the build failed
  community.general.sendgrid:
      username: "{{ sendgrid_username }}"
      password: "{{ sendgrid_password }}"
      from_address: "[email protected]"
      to_addresses:
        - "[email protected]"
        - "[email protected]"
      subject: "Build failure!."
      body: "Unable to pull source repository from Git server."
  delegate_to: localhost

作者

  • Matt Makai (@makaimc)