Skip to content

Instantly share code, notes, and snippets.

@drmalex07
Last active April 11, 2021 13:02
Show Gist options
  • Save drmalex07/59049840dbad354de3cdad9ce89fbbda to your computer and use it in GitHub Desktop.
Save drmalex07/59049840dbad354de3cdad9ce89fbbda to your computer and use it in GitHub Desktop.
Setup nullmailer. #smtp #nullmailer

README - Setup nullmailer

1. Prepare the inventory

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).

2. Play the Ansible playbook

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

3. Test

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}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment