An example inventory hosts.yml
:
---
all:
vars:
# ...
smtp_smarthost: 'mail.localdomain'
smtp_user: 'monitor'
smtp_pass_file: 'secrets/smtp/user/monitor'
smtp_ca_file: 'secrets/smtp/ca.pem'
# ...
children:
# ...
Place certain files under secrets
directory (e.g for smtp_pass_file
).
Prepare a xinetd
configuration file for sendmail
(place at files/etc/xinetd.d/sendmail
so that following playbook will find it):
service sendmail
{
disable = no
bind = localhost
port = 25
socket_type = stream
protocol = tcp
wait = no
user = mail
server = /usr/sbin/sendmail
server_args = -bs
type = unlisted
log_type = SYSLOG mail info
log_on_failure = ATTEMPT
}
Play:
--
- hosts: all
tasks:
- debug: var=play_hosts
- debug: var=groups.all
- apt: pkg={{item}} state=latest
with_items: ['nullmailer', 'mailutils', 'xinetd']
- name: Expose an SMTP service on localhost
copy:
src: files/etc/xinetd.d/sendmail
dest: /etc/xinetd.d/sendmail
- name: Configure SMTP smarthost for nullmailer
copy:
content: >
{{smtp_smarthost}} smtp
--starttls --user={{smtp_user}} --pass={{lookup('password', smtp_pass_file) }}
dest: /etc/nullmailer/remotes
- name: Set the default domain for mail recipients
copy:
content: "{{smtp_default_domain| default('')}}"
dest: /etc/nullmailer/defaultdomain
- copy:
src: "{{smtp_ca_file}}"
dest: /usr/local/share/ca-certificates/smtp-ca.crt
force: no
when: smtp_ca_file is defined
- command:
cmd: /usr/sbin/update-ca-certificates
when: smtp_ca_file is defined
- systemd:
name: xinetd.service
state: restarted
- systemd:
name: nullmailer.service
state: restarted
Send a test email:
subject="Testing nullmailer"
from_addr="monitor@localdomain"
to_addr="admin@localdomain"
echo -e "subject: ${subject}\nfrom: ${from_addr}\n\nHello there!" | sendmail ${to_addr}