community.general.mail 模块 – 发送电子邮件
注意
此模块是 community.general 集合 (版本 10.1.0) 的一部分。
如果您使用的是 ansible 包,则您可能已经安装了此集合。它不包含在 ansible-core 中。要检查它是否已安装,请运行 ansible-galaxy collection list。
要安装它,请使用:ansible-galaxy collection install community.general。
要在 playbook 中使用它,请指定:community.general.mail。
概要
- 此模块可用于从 playbook 发送电子邮件。 
- 人们可能会好奇为什么要自动化发送电子邮件?在复杂的環境中,有时会有一些无法自动化的流程,原因可能是您缺乏相应的权限,或者是因为并非所有人都同意采用通用的方法。 
- 如果您无法自动化特定步骤,但该步骤是非阻塞的,那么向负责人发送电子邮件以让他们执行他们应承担的部分,是一种将责任转移到其他人身上的优雅方法。 
- 当然,发送邮件也可以作为通知团队中的一到多人已(成功)执行特定操作的一种有效方式。 
参数
| 参数 | 注释 | 
|---|---|
| 要附加到邮件的文件的路径名列表。 附加文件的内容类型将设置为  默认值:  | |
| 邮件被“秘密”抄送到的电子邮件地址。 这是一个列表,可能包含地址和短语部分。 默认值:  | |
| 正在发送的电子邮件正文。 | |
| 邮件被抄送到的电子邮件地址。 这是一个列表,可能包含地址和短语部分。 默认值:  | |
| 正在发送的电子邮件的字符集。 默认值:  | |
| 允许手动指定 EHLO 的主机。 | |
| 应添加到邮件的标头列表。 每个标头都指定为  默认值:  | |
| 邮件服务器。 默认值:  | |
| 如果 SMTP 需要密码。 | |
| 邮件服务器端口。 这必须是 1 到 65534 之间的有效整数 默认值:  | |
| 如果为  如果为  如果为  如果为  选项 
 | |
| 发送邮件的电子邮件地址。可能包含地址和短语。 默认值:  | |
| 正在发送的电子邮件的主题。 | |
| 次要 mime 类型,可以是  主要类型始终为  选项 
 | |
| 设置连接尝试的超时时间(秒)。 默认值:  | |
| 邮件发送到的邮箱地址。 这是一个列表,可能包含地址和短语部分。 默认值:  | |
| 如果SMTP需要用户名。 | 
属性
| 属性 | 支持 | 描述 | 
|---|---|---|
| 支持:不支持 | 可以在 | |
| 支持:不支持 | 在diff模式下,将返回关于已更改内容(或在 | 
示例
- name: Example playbook sending mail to root
  community.general.mail:
    subject: System {{ ansible_hostname }} has been successfully provisioned.
  delegate_to: localhost
- name: Sending an e-mail using Gmail SMTP servers
  community.general.mail:
    host: smtp.gmail.com
    port: 587
    username: username@gmail.com
    password: mysecret
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
  delegate_to: localhost
- name: Send e-mail to a bunch of users, attaching files
  community.general.mail:
    host: 127.0.0.1
    port: 2025
    subject: Ansible-report
    body: Hello, this is an e-mail. I hope you like it ;-)
    from: jane@example.net (Jane Jolie)
    to:
    - John Doe <j.d@example.org>
    - Suzie Something <sue@example.com>
    cc: Charlie Root <root@localhost>
    attach:
    - /etc/group
    - /tmp/avatar2.png
    headers:
    - Reply-To=john@example.com
    - X-Special="Something or other"
    charset: us-ascii
  delegate_to: localhost
- name: Sending an e-mail using the remote machine, not the Ansible controller node
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
- name: Sending an e-mail using Legacy SSL to the remote machine
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: always
- name: Sending an e-mail using StartTLS to the remote machine
  community.general.mail:
    host: localhost
    port: 25
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: starttls
- name: Sending an e-mail using StartTLS, remote server, custom EHLO, and timeout of 10 seconds
  community.general.mail:
    host: some.smtp.host.tld
    port: 25
    timeout: 10
    ehlohost: my-resolvable-hostname.tld
    to: John Smith <john.smith@example.com>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: starttls
