はじめに
Postfixをインストールして設定の反映までを実施するansible-playbookです
ansible-playbookの簡単な使い方はわかる前提で記載しています
gmailなどのSMTPサービスを使用する前提です
環境の前提
- Ubuntu 20.04
- 導入するサーバは、内側から外側へインターネット接続できること
- メール送信にはポート587を使用するためFW設定などは適切に設定すること
ファイル構成
L postfix
- main.cf
- sender_canonical.db
- sasl_passwd.db
- postfix_server.yml
- hosts
main.cfの作成
main.cfを事前に作成しておきます
- mynetworks
Postfixを導入するサーバを利用してメール送信をおこなうネットワークセグメントを指定してください - relayhost
利用するSMTPサービスのアドレスを指定してください - smtp_sasl_password_maps
SMTPサービスの登録をするときに設定したパスワードです。postmapによりハッシュ化したものを指定します - sender_canonical_maps
送信元のメールアドレスを指定します。こちらもハッシュ化したものを指定します
inet_protocols=ipv4
mynetworks=127.0.0.0/8, xxx.yyy.zzz.0/24
relayhost = smtp.xxx.xxx.com:587
smtp_sasl_auth_enable = yes
smtp_sasl_security_options = noanonymous
smtp_sasl_password_maps = hash:/etc/postfix/sasl_passwd
smtp_use_tls = yes
smtp_tls_security_level = encrypt
smtp_tls_note_starttls_offer = yes
sender_canonical_maps = hash:/etc/postfix/sender_canonical
smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no
append_dot_mydomain = no
readme_directory = no
compatibility_level = 2
hosts
IPアドレスは導入するサーバの値を指定します
[postfix_server]
aaa.bbb.ccc.ddd
postfix_server.yml
やっていることは、以下の5つです
- Postfixをインストールする
- main.cfのコピー(事前に作成したファイルに置き換える)
- sasl_passwd.dbをコピー
- sender_canonical.dbをコピー
- postfixサービスを再起動してmain.cfの設定を反映させる
- name: setup postfix server
hosts: postfix_server
tasks:
- name: configure postfix using debconf
become: true
debconf:
name: postfix
question: "{{ item.question }}"
value: "{{ item.value }}"
vtype: "{{ item.vtype }}"
with_items:
- { question: postfix/main_mailer_type, value: "No configuration", vtype: select }
- name: install postfix
become: true
apt:
name: postfix
update_cache: yes
- name: copy main.cf
become: true
copy:
src: ./postfix/main.cf
dest: /etc/postfix
- name: copy sasl_passwd.db
become: true
copy:
src: ./postfix/sasl_passwd.db
dest: /etc/postfix
- name: copy sender_canonical.db
become: true
copy:
src: ./postfix/sender_canonical.db
dest: /etc/postfix
- name: postfix service restart
become: true
systemd:
name: postfix
state: restarted
enabled: yes
おわりに
説明不足だと思いますので不明な点があればコメントいただければわかる範囲で追記します