有事后,我们可能想通过邮件来接收一些通知,例如:
我们每隔3个小时会自动跑一遍Playbook来确保HTTP处于运行状态,如果HTTP不处于运行状态,我们希望ansible帮我启动它,并发邮件通知我(这不是ansible的常规用法,非战斗人员请紧急撤离,一般这种监控都是分钟级别的,不可能存在HTTP挂了30分钟的情况)
这个时候我们如果想发邮件,就必须用到ansible 的 mail 模块:
例子如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 |
# Example playbook sending mail to root - mail: subject: 'System {{ ansible_hostname }} has been successfully provisioned.' delegate_to: localhost # Sending an e-mail using Gmail SMTP servers - 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 # Send e-mail to a bunch of users, attaching files - 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/pavatar2.png headers: 'Reply-To=john@example.com|X-Special="Something or other"' charset: utf8 delegate_to: localhost # Sending an e-mail using the remote machine, not the Ansible controller node - mail: host: localhost port: 25 to: John Smith <john.smith@example.com> subject: Ansible-report body: 'System {{ ansible_hostname }} has been successfully provisioned.' # Sending an e-mail using Legacy SSL to the remote machine - 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 # Sending an e-mail using StartTLS to the remote machine - 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 |
具体的官方文档参数
parameter | required | default | choices | comments |
---|---|---|---|---|
attach | no |
A space-separated list of pathnames of files to attach to the message. Attached files will have their content-type set to
application/octet-stream . |
||
bcc | no |
The email-address(es) the mail is being ‘blind’ copied to. This is a comma-separated list, which may contain address and phrase portions.
|
||
body | no | $subject |
The body of the email being sent.
|
|
cc | no |
The email-address(es) the mail is being copied to. This is a comma-separated list, which may contain address and phrase portions.
|
||
charset | no | us-ascii |
The character set of email being sent
|
|
from | no | root |
The email-address the mail is sent from. May contain address and phrase.
|
|
headers | no |
A vertical-bar-separated list of headers which should be added to the message. Each individual header is specified as
header=value (see example below). |
||
host | no | localhost |
The mail server
|
|
password
(added in 1.9)
|
no |
If SMTP requires password
|
||
port | no | 25 |
The mail server port. This must be a valid integer between 1 and 65534
|
|
secure
(added in 2.3)
|
no | try |
|
If
always , the connection will only send email if the connection is Encrypted. If the server doesn’t accept the encrypted connection it will fail.If
try , the connection will attempt to setup a secure SSL/TLS session, before trying to send.If
never , the connection will not attempt to setup a secure SSL/TLS session, before sendingIf
starttls , the connection will try to upgrade to a secure SSL/TLS connection, before sending. If it is unable to do so it will fail. |
subject | yes |
The subject of the email being sent.
|
||
subtype
(added in 2.0)
|
no | plain |
The minor mime type, can be either text or html. The major type is always text.
|
|
timeout
(added in 2.3)
|
no | 20 |
Sets the Timeout in seconds for connection attempts
|
|
to | no | root |
The email-address(es) the mail is being sent to. This is a comma-separated list, which may contain address and phrase portions.
|
|
username
(added in 1.9)
|
no |
If SMTP requires username
|
Latest posts by Zhiming Zhang (see all)
- aws eks node 自动化扩展工具 Karpenter - 8月 10, 2022
- ReplicationController and ReplicaSet in Kubernetes - 12月 20, 2021
- public key fingerprint - 5月 27, 2021