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 发送电子邮件。

  • 人们可能会好奇为什么要自动化发送电子邮件?在复杂的環境中,有时会有一些无法自动化的流程,原因可能是您缺乏相应的权限,或者是因为并非所有人都同意采用通用的方法。

  • 如果您无法自动化特定步骤,但该步骤是非阻塞的,那么向负责人发送电子邮件以让他们执行他们应承担的部分,是一种将责任转移到其他人身上的优雅方法。

  • 当然,发送邮件也可以作为通知团队中的一到多人已(成功)执行特定操作的一种有效方式。

参数

参数

注释

attach

列表 / 元素=路径

要附加到邮件的文件的路径名列表。

附加文件的内容类型将设置为 application/octet-stream

默认值: []

bcc

列表 / 元素=字符串

邮件被“秘密”抄送到的电子邮件地址。

这是一个列表,可能包含地址和短语部分。

默认值: []

body

字符串

正在发送的电子邮件正文。

cc

列表 / 元素=字符串

邮件被抄送到的电子邮件地址。

这是一个列表,可能包含地址和短语部分。

默认值: []

charset

字符串

正在发送的电子邮件的字符集。

默认值: "utf-8"

ehlohost

字符串

在 community.general 3.8.0 中添加

允许手动指定 EHLO 的主机。

headers

列表 / 元素=字符串

应添加到邮件的标头列表。

每个标头都指定为 header=value(参见下面的示例)。

默认值: []

host

字符串

邮件服务器。

默认值: "localhost"

message_id_domain

字符串

在 community.general 8.2.0 中添加

用于 Message-ID 标头 的域名。

请注意,这仅在 Python 3+ 中可用。在 Python 2 中,此值将被忽略。

默认值: "ansible"

password

字符串

如果 SMTP 需要密码。

port

整数

邮件服务器端口。

这必须是 1 到 65534 之间的有效整数

默认值: 25

secure

字符串

如果为 always,则只有在连接已加密时,连接才会发送电子邮件。如果服务器不接受加密连接,则会失败。

如果为 try,则连接会在尝试发送之前尝试建立安全的 SSL/TLS 会话。

如果为 never,则连接在发送之前不会尝试建立安全的 SSL/TLS 会话。

如果为 starttls,则连接会在发送之前尝试升级到安全的 SSL/TLS 连接。如果无法这样做,则会失败。

选项

  • "always"

  • "never"

  • "starttls"

  • "try" ← (默认)

sender

别名:from

字符串

发送邮件的电子邮件地址。可能包含地址和短语。

默认值: "root"

subject

别名:msg

字符串 / 必填

正在发送的电子邮件的主题。

subtype

字符串

次要 mime 类型,可以是 plainhtml

主要类型始终为 text

选项

  • "html"

  • "plain" ← (默认)

timeout

整数

设置连接尝试的超时时间(秒)。

默认值: 20

收件人

别名:recipients

列表 / 元素=字符串

邮件发送到的邮箱地址。

这是一个列表,可能包含地址和短语部分。

默认值: ["root"]

用户名

字符串

如果SMTP需要用户名。

属性

属性

支持

描述

check_mode

支持:不支持

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

diff_mode

支持:不支持

在diff模式下,将返回关于已更改内容(或在check_mode下可能需要更改的内容)的详细信息。

示例

- 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: [email protected]
    password: mysecret
    to: John Smith <[email protected]>
    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: [email protected] (Jane Jolie)
    to:
    - John Doe <[email protected]>
    - Suzie Something <[email protected]>
    cc: Charlie Root <root@localhost>
    attach:
    - /etc/group
    - /tmp/avatar2.png
    headers:
    - [email protected]
    - 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 <[email protected]>
    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 <[email protected]>
    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 <[email protected]>
    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 <[email protected]>
    subject: Ansible-report
    body: System {{ ansible_hostname }} has been successfully provisioned.
    secure: starttls

作者

  • Dag Wieers (@dagwieers)