| 1 |
--- |
| 2 |
#################################################################### |
| 3 |
# WARNING: These are designed specifically for Ansible tests # |
| 4 |
# and should not be used as examples of how to write Ansible roles # |
| 5 |
#################################################################### |
| 6 |
|
| 7 |
# Copyright (c) Ansible Project |
| 8 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 9 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 10 |
|
| 11 |
# TODO: Our current implementation does not handle SMTP authentication |
| 12 |
|
| 13 |
- when: |
| 14 |
# TODO: https://github.com/ansible-collections/community.general/issues/4656 |
| 15 |
- ansible_facts.python.version.major != 3 or ansible_facts.python.version.minor < 12 |
| 16 |
block: |
| 17 |
|
| 18 |
# NOTE: If the system does not support smtpd-tls (python 2.6 and older) we do basic tests |
| 19 |
- name: Attempt to install smtpd-tls |
| 20 |
pip: |
| 21 |
name: smtpd-tls |
| 22 |
state: present |
| 23 |
ignore_errors: true |
| 24 |
register: smtpd_tls |
| 25 |
|
| 26 |
- name: Install test smtpserver |
| 27 |
copy: |
| 28 |
src: '{{ item }}' |
| 29 |
dest: '{{ remote_tmp_dir }}/{{ item }}' |
| 30 |
loop: |
| 31 |
- smtpserver.py |
| 32 |
- smtpserver.crt |
| 33 |
- smtpserver.key |
| 34 |
|
| 35 |
# FIXME: Verify the mail after it was send would be nice |
| 36 |
# This would require either dumping the content, or registering async task output |
| 37 |
- name: Start test smtpserver |
| 38 |
shell: '{{ ansible_facts.python.executable }} {{ remote_tmp_dir }}/smtpserver.py 10025:10465' |
| 39 |
async: 45 |
| 40 |
poll: 0 |
| 41 |
register: smtpserver |
| 42 |
|
| 43 |
- name: Send a basic test-mail |
| 44 |
mail: |
| 45 |
port: 10025 |
| 46 |
subject: Test mail 1 (smtp) |
| 47 |
secure: never |
| 48 |
|
| 49 |
- name: Send a test-mail with body and specific recipient |
| 50 |
mail: |
| 51 |
port: 10025 |
| 52 |
from: ansible@localhost |
| 53 |
to: root@localhost |
| 54 |
subject: Test mail 2 (smtp + body) |
| 55 |
body: Test body 2 |
| 56 |
secure: never |
| 57 |
|
| 58 |
- name: Send a test-mail with attachment |
| 59 |
mail: |
| 60 |
port: 10025 |
| 61 |
from: ansible@localhost |
| 62 |
to: root@localhost |
| 63 |
subject: Test mail 3 (smtp + body + attachment) |
| 64 |
body: Test body 3 |
| 65 |
attach: /etc/group |
| 66 |
secure: never |
| 67 |
|
| 68 |
# NOTE: This might fail if smtpd-tls is missing or python 2.7.8 or older is used |
| 69 |
- name: Send a test-mail using starttls |
| 70 |
mail: |
| 71 |
port: 10025 |
| 72 |
from: ansible@localhost |
| 73 |
to: root@localhost |
| 74 |
subject: Test mail 4 (smtp + starttls + body + attachment) |
| 75 |
body: Test body 4 |
| 76 |
attach: /etc/group |
| 77 |
secure: starttls |
| 78 |
ignore_errors: true |
| 79 |
register: starttls_support |
| 80 |
|
| 81 |
# NOTE: This might fail if smtpd-tls is missing or python 2.7.8 or older is used |
| 82 |
- name: Send a test-mail using TLS |
| 83 |
mail: |
| 84 |
port: 10465 |
| 85 |
from: ansible@localhost |
| 86 |
to: root@localhost |
| 87 |
subject: Test mail 5 (smtp + tls + body + attachment) |
| 88 |
body: Test body 5 |
| 89 |
attach: /etc/group |
| 90 |
secure: always |
| 91 |
ignore_errors: true |
| 92 |
register: tls_support |
| 93 |
|
| 94 |
- fail: |
| 95 |
msg: Sending mail using starttls failed. |
| 96 |
when: smtpd_tls is succeeded and starttls_support is failed and tls_support is succeeded |
| 97 |
|
| 98 |
- fail: |
| 99 |
msg: Send mail using TLS failed. |
| 100 |
when: smtpd_tls is succeeded and tls_support is failed and starttls_support is succeeded |
| 101 |
|
| 102 |
- name: Send a test-mail with body, specific recipient and specific ehlohost |
| 103 |
mail: |
| 104 |
port: 10025 |
| 105 |
ehlohost: some.domain.tld |
| 106 |
from: ansible@localhost |
| 107 |
to: root@localhost |
| 108 |
subject: Test mail 6 (smtp + body + ehlohost) |
| 109 |
body: Test body 6 |
| 110 |
secure: never |