使用Ansible,在Amazon Linux 2023上安装Postfix

Ansible是什么?

我用三行来描述Ansible。

Ansible负责管理服务器的配置。
需要配置inventory(针对哪台服务器)和playbook(做什么)。
通过SSH或WinRM连接服务器,并且以无代理的方式运行。

首先,我会介绍一下在Ansible上执行任务的感觉。
当执行Ansible命令时,它会连接到服务器并执行指定的任务。
最后,它会用摘要方式呈现更改的结果。

ansible実行.gif

本次设置的概要图

以下是关于 Ansible 的概述图。我想通过使用在本地PC上配置的 Ansible 的 inventory 和 playbook 文件来进行邮件服务器的设置。

Ansibleざっくり概要図.png

这可能也适用于一些简单的实地操作。GitHub存储库在这里。

 

把事情準備好

安装Ansible

因为我使用的是Macbook,所以我会提及MacOS的安装。
官方文档推荐使用pip进行安装,但由于我也能用brew安装,所以我选择了从那里安装。

brew install ansible

# バージョンを確認
ansible --version
>> ansible [core 2.15.3]

VSCode 扩展功能

image.png
    • Ansibleのyamlファイルをハイライトやサジェスト機能を提供してくれます

 

    • 対象ファイルは

/playbooksディレクトリ配下のyamlファイル
拡張子が.ansible.ymlや.ansible.yaml

etc…

启动EC2

本次是通过AWS控制台创建的。

    • name: MailServer1、MailServer2

プラットフォーム: Amazon Linux 2023
pemフィアル名: ansible_mail.pem

セキュリティグループ: インバウンドルールに22番ポート(SSH)を許可

image.png

设置

那么,我们立即开始配置 Ansible。

服务器连接设置(清单)

    • デフォルトは/etc/ansible/hostsを参照する

 

    コマンド実行オプション -i <inventoryファイルのpath>でも指定可能なので、今回はinventory_hostsファイルを新規作成して設定していきます。
[mailservers]
mail-server1 ansible_host=ec2-54-249-100-242.ap-northeast-1.compute.amazonaws.com ansible_connection=ssh ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/ansible_mail.pem
mail-server2 ansible_host=ec2-52-192-45-175.ap-northeast-1.compute.amazonaws.com ansible_connection=ssh ansible_user=ec2-user ansible_ssh_private_key_file=~/.ssh/ansible_mail.pem

库存文件的设置值

[グループ名]: グループ名は任意の値です。後でこの設定値を使って対象サーバを特定します
ansible_host: AWSコンソールからパブリックDNSを確認してください
ansible_connection: SSH
ansible_user: 接続ユーザー名
ansible_ssh_private_key_file: pemファイルのパスを指定

※连接方法不仅限于上述的方式(如需了解更多,请参阅下方的官方文档)。

 

Postfix的安装。

在Ansible中,根据YAML文件中的playbook,按照顺序逐步处理。

剧本 (jù

- name: Setup Postfix on MailServer
  hosts:
    - mailservers
  tasks:
    - name: Install Postfix
      yum:
        name: postfix
        state: present
      become: yes
    • name: 任意の分かりやすい説明です

 

    • hosts: inventoryファイルで設定したグループ名を指定しています(これでMailServer1とMailServer2に対してインストールが実行されます)

 

    • tasks: 複数のタスクを設定できます。いったん一つだけ設定します

yumを使ってインストールしています
stateの値によってインストールやアンインストールを設定します

present:インストール
absent:アンインストール

become: yesを指定するとroot権限で実行します

未指定だとec2-userでPostfixのインストールを実行することになり、権限が無くてエラーになります

执行(ansible-playbook命令)

$ ansible-playbook -i inventory_hosts setup_postfix.ansible.yaml

PLAY [Setup Postfix on MailServer] **********************************************************************************

TASK [Gathering Facts] **********************************************************************************************
[WARNING]: Platform linux on host mail-server2 is using the discovered Python interpreter at /usr/bin/python3.9, but
future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [mail-server2]
[WARNING]: Platform linux on host mail-server1 is using the discovered Python interpreter at /usr/bin/python3.9, but
future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [mail-server1]

TASK [Install Postfix] **********************************************************************************************
changed: [mail-server2]
changed: [mail-server1]

PLAY RECAP **********************************************************************************************************
mail-server1               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
mail-server2               : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

确认

我确认安装了Postfix。(这里省略了MailServer2)

$ sudo yum list installed | grep postfix
postfix.x86_64                         2:3.7.2-4.amzn2023.0.4             @amazonlinux

更新Postfix的配置文件

下面是Postfix的设置。

在中国,有几种不同的Postfix配置文件类型,但基本上只要能更新/etc/postfix/main.cf文件,其他配置文件也可以使用相同的方法进行更新。

剧本

将要更新的main.cf文件放置在/etc/postfix/main.cf目录中。

# Global Postfix configuration file. This file lists only a subset
# of all parameters. For the syntax, and for a complete parameter
# list, see the postconf(5) manual page (command: "man 5 postconf").
#
...(省略)
- #myhostname = host.domain.tld
+ myhostname = example.com

将其添加到之前的playbook中。

- name: Setup Postfix on MailServer
  hosts:
    - mailservers
  tasks:
    - name: Install Postfix
      yum:
        name: postfix
        state: present
      become: yes
    # ■■■■ ここから追記 ■■■■
    - name: Edit Postfix Config
      copy:
        src: "{{ playbook_dir }}/etc/postfix/main.cf"
        dest: /etc/postfix/main.cf
      become: yes
    • copy: コピーコマンドです。今回はファイル単位でコピーしています

src: playbook_dirはAnsibleが提供しているマジック変数です。変数の参照は{{ }}で囲みます

执行(ansible-playbook命令)

$ ansible-playbook -i inventory_hosts setup_postfix.ansible.yaml

PLAY [Setup Postfix on MailServer] **********************************************************************************

TASK [Gathering Facts] **********************************************************************************************
[WARNING]: Platform linux on host mail-server1 is using the discovered Python interpreter at /usr/bin/python3.9, but
future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [mail-server1]
[WARNING]: Platform linux on host mail-server2 is using the discovered Python interpreter at /usr/bin/python3.9, but
future installation of another Python interpreter could change the meaning of that path. See
https://docs.ansible.com/ansible-core/2.15/reference_appendices/interpreter_discovery.html for more information.
ok: [mail-server2]

TASK [Install Postfix] **********************************************************************************************
ok: [mail-server1]
ok: [mail-server2]

TASK [Edit Postfix Config] ******************************************************************************************
changed: [mail-server1]
changed: [mail-server2]

PLAY RECAP **********************************************************************************************************
mail-server1               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
mail-server2               : ok=3    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0  

确认

我确认了main.cf文件已经更新。(忽略MailServer2)

$ cat /etc/postfix/main.cf | grep example.com
myhostname = example.com

尝试后我的感觉

只要能够通过SSH连接,就能够方便地管理配置文件。Ansible具备幂等性(无论执行多少次都能得到相同结果)的特性,这也意味着可以进行多次尝试和错误修正,这让人感到很可靠。尽管在生产环境中可能存在幂等性无法保持的情况…即便如此,通过将配置文件进行代码管理,我们可以追踪到当初的配置目的和更改历史,这对团队来说也是一种资产,我认为。

填補

    Amazon Linux 2 ではデフォルトで入っているpostfix.serviceサービスがAmazon Linux 2023 では入っていないので、自前でインストールする必要があります

请参考