如何使用 Ansible 在类 RedHat 系统中自动化部署 Apache HTTPD 网络服务器?以下是一个完整的指南,涵盖从安装软件包到配置自定义网页、启动服务、设置开机启动以及开放防火墙端口的全过程。

在类 RedHat 系统中部署 Apache HTTPD

使用的 Ansible 模块

  1. 安装软件包 ⇒ ansible.builtin.yum
  2. 创建自定义网页 ⇒ ansible.builtin.copy
  3. 启动服务 ⇒ ansible.builtin.service
  4. 开放防火墙端口 ⇒ ansible.posix.firewalld

以下是详细步骤:

  1. 安装 httpd 软件包及其依赖项,使用 ansible.builtin.yum 模块。
  2. 使用 ansible.builtin.copy 模块创建自定义的 index.html 文件。
  3. 启动 httpd 服务并设置开机启动,使用 ansible.builtin.service 模块。
  4. 开放防火墙中与 HTTP 服务相关的端口,使用 ansible.posix.firewalld 模块。

示例代码

以下是一个 Ansible Playbook,用于在类 RedHat 系统中部署 Apache HTTPD 网络服务器。

文件名:httpd_redhat.yml

---
- name: 部署网络服务器
  hosts: all
  become: true
  tasks:
    - name: 安装 httpd
      ansible.builtin.yum:
        name: httpd
        state: latest

    - name: 创建自定义 index.html
      ansible.builtin.copy:
        dest: /var/www/html/index.html
        content: |
          <html>
          <head><title>自定义网页</title></head>
          <body><h1>欢迎来到我的网络服务器!</h1></body>
          </html>

    - name: 启用 httpd 服务
      ansible.builtin.service:
        name: httpd
        enabled: true
        state: started

    - name: 开放防火墙端口
      ansible.posix.firewalld:
        service: http
        state: enabled
        immediate: true
        permanent: true

执行命令

$ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat.yml

执行结果

PLAY [部署网络服务器] *******************************************************

TASK [Gathering Facts] ******************************************************
ok: [demo.example.com]

TASK [安装 httpd] ***********************************************************
changed: [demo.example.com]

TASK [创建自定义 index.html] *************************************************
changed: [demo.example.com]

TASK [启用 httpd 服务] *******************************************************
changed: [demo.example.com]

TASK [开放防火墙端口] *********************************************************
changed: [demo.example.com]

PLAY RECAP *******************************************************************
demo.example.com           : ok=5    changed=4    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

脚本的幂等性测试

再次运行 Playbook 以验证幂等性:

$ ansible-playbook -i virtualmachines/demo/inventory services/httpd_redhat.yml

幂等性测试结果:

PLAY [部署网络服务器] *******************************************************

TASK [Gathering Facts] ******************************************************
ok: [demo.example.com]

TASK [安装 httpd] ***********************************************************
ok: [demo.example.com]

TASK [创建自定义 index.html] *************************************************
ok: [demo.example.com]

TASK [启用 httpd 服务] *******************************************************
ok: [demo.example.com]

TASK [开放防火墙端口] *********************************************************
ok: [demo.example.com]

PLAY RECAP *******************************************************************
demo.example.com           : ok=5    changed=0    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0

执行前的系统状态

$ ssh devops@demo.example.com
Last login: Sat Feb 12 10:08:51 2022 from 192.168.0.100
[devops@demo ~]$ sudo su
[root@demo devops]# cat /etc/os-release 
NAME="Red Hat Enterprise Linux"
VERSION="8.5 (Ootpa)"
ID="rhel"
ID_LIKE="fedora"
VERSION_ID="8.5"
PLATFORM_ID="platform:el8"
PRETTY_NAME="Red Hat Enterprise Linux 8.5 (Ootpa)"
ANSI_COLOR="0;31"
CPE_NAME="cpe:/o:redhat:enterprise_linux:8::baseos"
HOME_URL="https://www.redhat.com/"
DOCUMENTATION_URL="https://access.redhat.com/documentation/red_hat_enterprise_linux/8/"
BUG_REPORT_URL="https://bugzilla.redhat.com/"
REDHAT_BUGZILLA_PRODUCT="Red Hat Enterprise Linux 8"
REDHAT_BUGZILLA_PRODUCT_VERSION=8.5
REDHAT_SUPPORT_PRODUCT="Red Hat Enterprise Linux"
REDHAT_SUPPORT_PRODUCT_VERSION="8.5"

[root@demo devops]# dnf list installed httpd
Updating Subscription Management repositories.
Error: No matching Packages to list
[root@demo devops]# rpm -qa | grep httpd
[root@demo devops]# cat /var/www/html/index.html
cat: /var/www/html/index.html: No such file or directory
[root@demo devops]# ls -al /var/www
ls: cannot access '/var/www': No such file or directory
[root@demo devops]#

执行后的系统状态

$ ssh devops@demo.example.com
Last login: Sat Feb 12 10:12:47 2022 from 192.168.0.100
[devops@demo ~]$ sudo su
[root@demo devops]# dnf list installed httpd
Updating Subscription Management repositories.
Installed Packages
httpd.x86_64      2.4.37-43.module+el8.5.0+13806+b30d9eec.1       @rhel-8-for-x86_64-appstream-rpms
[root@demo devops]# rpm -qa | grep httpd
httpd-tools-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
httpd-filesystem-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.noarch
httpd-2.4.37-43.module+el8.5.0+13806+b30d9eec.1.x86_64
redhat-logos-httpd-84.5-1.el8.noarch
[root@demo devops]# cat /var/www/html/index.html 
<html>
<head><title>自定义网页</title></head>
<body><h1>欢迎来到我的网络服务器!</h1></body>
</html>
[root@demo devops]#

现在,你已经学会了如何使用 Ansible 在类 RedHat 系统中部署 Apache HTTPD 网络服务器。