community.general.sendgrid 模块 – 使用 SendGrid API 发送电子邮件
注意
此模块是 community.general 集合 (版本 10.1.0) 的一部分。
如果您正在使用 ansible 包,您可能已经安装了此集合。它不包含在 ansible-core 中。要检查是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。您需要其他要求才能使用此模块,请参阅 要求 了解详细信息。
要在 playbook 中使用它,请指定:community.general.sendgrid。
概要
- 使用 SendGrid 帐户通过其 API(而不是通过 SMTP 服务)发送电子邮件。 
要求
在执行此模块的主机上需要满足以下要求。
- sendgrid Python 库 1.6.22 或更低版本 (支持 Sendgrid API V2) 
参数
| 参数 | 注释 | 
|---|---|
| 要使用的 Sendgrid API 密钥,而不是用户名/密码。 | |
| 您要附加的文件的相对或显式路径的列表(根据 SendGrid 文档,限制为 7MB)。 | |
| 要密件抄送的电子邮件地址列表。 | |
| 电子邮件正文内容。 | |
| 要抄送的电子邮件地址列表。 | |
| 电子邮件“发件人”字段中的地址。 | |
| 您希望在发件人字段中显示的名字,例如 ‘John Doe’。 | |
| 要作为标头传递的字典。 | |
| 正文是否是应该呈现的 html 内容。 选项 
 | |
| 与用户名对应的密码。 仅当未提供  | |
| 电子邮件的所需主题。 | |
| 包含一个或多个收件人电子邮件地址的列表。 | |
| 登录 SendGrid 帐户的用户名。 仅当未提供  | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持: 完全 | 可以在  | |
| 支持: 无 | 当处于 diff 模式时,将返回有关已更改(或可能需要在  | 
注释
注意
- 此模块是非幂等的,因为它通过外部 API 发送电子邮件。仅当模块失败时,它才是幂等的。 
- 与其他通知模块一样,此模块需要外部依赖项才能工作。在这种情况下,您需要一个活动的 SendGrid 帐户。 
- 为了使用 api_key、cc、bcc、attachments、from_name、html_body、headers,您必须 pip 安装 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
