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) 
参数
| 参数 | 注释 | 
|---|---|
| 要使用的 Sendgrid API 密钥,替代用户名/密码。 | |
| 要附加的文件的相对路径或显式路径列表(根据 SendGrid 文档,限制为 7MB)。 | |
| 要密送的电子邮件地址列表。 | |
| 电子邮件正文内容。 | |
| 要抄送的电子邮件地址列表。 | |
| 电子邮件“发件人”字段中的地址。 | |
| 您希望显示在发件人字段中的名称,例如“John Doe”。 | |
| 作为标头传递的字典。 | |
| 正文是否应呈现为 html 内容。 选项 
 | |
| 与用户名对应的密码。 仅当未提供  | |
| 所需的电子邮件主题。 | |
| 包含一个或多个收件人电子邮件地址的列表。 | |
| 登录 SendGrid 帐户的用户名。 仅当未提供  | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持:完全支持 | 可以在  | |
| 支持:不支持 | 处于差异模式时,将返回有关已更改内容(或可能需要在  | 
备注
注意
- 此模块是非幂等的,因为它通过外部 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: "ansible@mycompany.com"
    to_addresses:
      - "ops@mycompany.com"
    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: "build@mycompany.com"
      to_addresses:
        - "ops@mycompany.com"
        - "devteam@mycompany.com"
      subject: "Build failure!."
      body: "Unable to pull source repository from Git server."
  delegate_to: localhost
