Ansible 自動化運維工具:介紹與完整部署(RHEL 9)
Ansible 的介紹與安裝
一、自動化運維的必要性
傳統手動運維依賴圖形/命令行界面、檢查清單或記憶執行任務,存在以下核心問題:
- 易出錯:易跳過步驟或執行錯誤操作,結果驗證有限;
- 效率低:重復性任務占用大量時間,無法聚焦核心工作;
- 一致性差:不同服務器配置易出現差異,影響業務穩定性。
自動化運維可解決上述問題,實現快速、標準化、無差異的系統部署與配置,釋放運維人力。
二、什么是 Ansible
Ansible 是基于 Python 開發的開源自動化運維平臺,核心能力包括:
- 批量操作:支持批量系統配置、程序部署、命令執行;
- 無代理架構:無需在被管節點安裝客戶端(Agent),通過 SSH/WinRM 通信;
- 模塊化驅動:本身僅提供框架,實際功能由模塊實現(核心模塊+自定義模塊);
- 全生命周期管理:覆蓋配置管理、應用部署、工作流編排、網絡自動化;
其工作原理:控制主機通過 SSH 推送臨時模塊到被管節點,執行任務后自動刪除模塊,確保被管節點清潔。
三、Ansible 核心優點
- 跨平臺支持:覆蓋 Linux、Windows、Unix 及網絡設備,適配物理機、虛擬機、云、容器;
- 易讀的自動化腳本:通過 YAML 格式的 Playbook 編寫任務,人類可讀,便于團隊協作;
- 版本控制友好:Playbook 為純文本文件,可直接納入 Git 等版本控制系統;
- 動態清單:支持從外部來源(如云平臺、CMDB)動態獲取被管主機列表,適應架構變化;
- 靈活集成:可與 Puppet、Jenkins、紅帽衛星等現有系統集成,復用現有架構。
四、Ansible 管理架構
Ansible 由控制主機和被管節點組成,核心組件如下:
組件 | 作用 |
---|---|
控制主機 | 運行 Ansible 核心程序,發起任務執行(需安裝 Ansible,被管節點無需) |
被管節點 | 接收控制主機的任務指令,執行對應操作(僅需支持 SSH,無需安裝 Ansible) |
Host Inventory | 主機清單,記錄被管節點的 IP、端口、賬號等信息,支持分組管理 |
Modules | 任務執行單元(核心模塊/自定義模塊),如 ping (測試連通性)、yum (安裝軟件) |
Playbooks | 任務劇本,通過 YAML 組合多個模塊,定義被管節點的最終狀態(例行任務首選) |
Plugins | 擴展功能,如日志插件(記錄執行日志)、郵件插件(任務結果通知) |
Connection Plugins | 連接插件,默認使用 SSH 插件與被管節點通信 |
五、Ansible 任務執行模式
Ansible 提供兩種核心執行模式,適配不同場景:
模式 | 特點 | 適用場景 |
---|---|---|
Ad-hoc 模式(點對點) | 單條命令+單個模塊,無需保存,執行快速 | 臨時性操作(如批量查看磁盤、測試連通性) |
Playbook 模式(劇本) | 多模塊按邏輯組合,YAML 格式保存,可重復執行 | 例行性任務(如 Web 服務部署、數據庫備份) |
六、Ansible 完整部署流程(RHEL 9)
環境規劃
共 6 臺 RHEL 9 主機,1 臺控制主機(master)+ 5 臺被管節點(node1~node5),網絡信息如下:
主機名 | IP 地址 | 角色 | 用途 |
---|---|---|---|
master.example.com | 192.168.122.100 | 控制主機 | 運行 Ansible,管理被管節點 |
node1.example.com | 192.168.122.10 | 被管節點 | 測試節點(test01 組) |
node2.example.com | 192.168.122.20 | 被管節點 | 測試節點(test02 組) |
node3.example.com | 192.168.122.30 | 被管節點 | Web 節點(web 組) |
node4.example.com | 192.168.122.40 | 被管節點 | Web 節點(web 組) |
node5.example.com | 192.168.122.50 | 被管節點 | 測試節點(test05 組) |
ansible.example.com | 192.168.122.1 | 宿主機 | 提供 YUM 倉庫服務 |
步驟 1:基礎環境準備(所有主機)
1.1 安裝 RHEL 9 虛擬機
- 安裝 1 臺 RHEL 9 虛擬機作為“模板機”,配置 IP 后,通過
virt-manager
克隆出 5 臺,共 6 臺主機; - 注意:克隆后需刪除所有主機網卡配置文件的
UUID
(避免沖突),路徑:/etc/sysconfig/network-scripts/ifcfg-<網卡名>
(如ifcfg-ens33
)。
1.2 配置主機名與 /etc/hosts
所有主機統一配置 /etc/hosts
,實現主機名解析(無需 DNS):
vim /etc/hosts
# 添加以下內容
192.168.122.1 ansible.example.com ansible
192.168.122.100 master.example.com master
192.168.122.10 node1.example.com node1
192.168.122.20 node2.example.com node2
192.168.122.30 node3.example.com node3
192.168.122.40 node4.example.com node4
192.168.122.50 node5.example.com node5
步驟 2:配置控制主機(master)免密鑰登錄
Ansible 通過 SSH 通信,需實現 master 到所有被管節點(node1~node5)的免密登錄(root + student 用戶)。
2.1 生成 SSH 密鑰(master 主機)
分別切換 root
和 student
用戶,生成密鑰(一路回車,不設密碼):
# root 用戶
[root@master ~]# ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
/root/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa
Your public key has been saved in /root/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:fu+YtSh2VzPILf27GULLWKt/71xihlpGacQABgid3PU root@master.example.com
The key's randomart image is:
+---[RSA 3072]----+
| .+ +.o+.. |
| = .. . o |
| E o |
| . . |
| S .+* |
| . oO.O |
| . ..=B+=.|
| o..Xo+.+=|
| . o=+=..=B|
+----[SHA256]-----+# student 用戶(若不存在,先創建:useradd student && passwd student)
[root@master ~]# su - student
[student@master ~]$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/student/.ssh/id_rsa):
/home/student/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/student/.ssh/id_rsa
Your public key has been saved in /home/student/.ssh/id_rsa.pub
The key fingerprint is:
SHA256:N5v5lxQ1PcwK9bqjZllkxr7voQJ8IR8KzdBtYxjaiE4 student@master.example.com
The key's randomart image is:
+---[RSA 3072]----+
| ..+ ..o .|
| ..+o * .=o|
| E o+.o + .oo|
| o . + o *o |
| . S * B. . |
| = B oo |
| * o+.o |
| *..= .|
| o.oooo |
+----[SHA256]-----+
2.2 批量分發公鑰(master 主機)
通過循環腳本將公鑰分發到所有被管節點:
# 1. root 用戶分發到被管節點 root/student
[root@master ~]# for i in node{1..5}; dossh-copy-id -i ~/.ssh/id_rsa.pub root@$i # 免密登錄被管節點 rootssh-copy-id -i ~/.ssh/id_rsa.pub student@$i # 免密登錄被管節點 student
done# 2. student 用戶分發到被管節點 root/student(切換到 student 執行)
su - student
[root@master ~]# for i in node{1..5}; dossh-copy-id -i ~/.ssh/id_rsa.pub root@$issh-copy-id -i ~/.ssh/id_rsa.pub student@$i
done
驗證:執行 ssh node1
(root/student 用戶),無需輸入密碼即登錄成功。
[root@master ~]# for i in node{1..5}; do scp /etc/hosts root@$i:/etc/hosts; done
hosts 100% 437 520.7KB/s 00:00
hosts 100% 437 700.9KB/s 00:00
hosts 100% 437 472.2KB/s 00:00
hosts 100% 437 141.6KB/s 00:00
hosts 100% 437 660.6KB/s 00:00
步驟 3:配置 YUM 倉庫(宿主機 + master)
Ansible 安裝依賴自定義 YUM 倉庫(宿主機提供源,master 及被管節點使用)。
3.1 宿主機(ansible.example.com)配置倉庫
- 掛載 RHEL 9 鏡像,安裝 Apache 服務:
# 掛載鏡像 [root@ansible ~]# mount /dev/cdrom /mnt # 安裝 Apache [root@ansible ~]# yum -y install httpd [root@ansible ~]# systemctl restart httpd [root@ansible ~]# systemctl enable httpd # 關閉防火墻與 SELinux [root@ansible ~]# systemctl stop firewalld [root@ansible ~]# systemctl disable firewalld
[root@ansible ~]# setenforce 0
[root@ansible ~]# sed -i ‘s/SELINUX=enforcing/SELINUX=disabled/’ /etc/selinux/config
- 復制鏡像文件到 Apache 根目錄(供遠程訪問):
# 上傳 Ansible自動化平臺包 BaseOS 與 AppStream源到 /var/www/html/(需提前準備包自行上傳)[root@ansible ~]# ls /var/www/html/ansible-automation-platform materials rhel9 roles
3.2 master 主機配置 YUM 倉庫
創建 /etc/yum.repos.d/server.repo
,指向宿主機倉庫:
[root@master ~]$ vim /etc/yum.repos.d/server.repo[aa]
name=RHEL9 BaseOS
baseurl=http://ansible.example.com/rhel9/BaseOS
enabled=1
gpgcheck=0[cc]
name=RHEL9 AppStream
baseurl=http://ansible.example.com/rhel9/AppStream
enabled=1
gpgcheck=0[dd]
name=Ansible Automation Platform
baseurl=http://ansible.example.com/ansible-automation-platform
enabled=1
gpgcheck=0
驗證:執行 yum -y install vim
,確認能正常安裝軟件。
步驟 4:student 用戶提權(所有主機)
Ansible 通常使用普通用戶(student)執行任務,需配置 sudo 免密提權:
# master 主機創建提權文件
[root@master ~]# vim /etc/sudoers.d/studentstudent ALL=(ALL) NOPASSWD: ALL
# student 免密執行所有 sudo 命令# 批量分發到所有被管節點
[root@master ~]# [root@master ~]# for i in node{1..5}; do scp /etc/sudoers.d/student root@$i:/etc/sudoers.d/; done
student 100% 32 68.3KB/s 00:00
student 100% 32 61.5KB/s 00:00
student 100% 32 41.3KB/s 00:00
student 100% 32 37.0KB/s 00:00
student 100% 32 52.1KB/s 00:00
步驟 5:安裝 Ansible(master 主機)
切換到 student
用戶,安裝 Ansible 核心組件:
[root@master ~]$ su - student
[student@master ~]$ sudo yum -y install ansible-core ansible-navigator
步驟 6:配置 Ansible(master 主機,student 用戶)
在 student
家目錄下創建 Ansible 工作目錄、主機清單、配置文件。
6.1 創建工作目錄
# 新建核心目錄
[student@master ansible]$ mkdir -p /home/student/ansible/{inventory,roles,collections}
[student@master ansible]$ cd /home/student/ansible
6.2 定義主機清單(inventory)
按業務分組管理被管節點,編輯 inventory
文件:
[student@master ansible]$ vim inventorynode1
node1
node2
node3
node4
node5
6.3 生成并配置 Ansible 主配置(ansible.cfg)
-
生成默認配置文件:
[student@master ansible]$ ansible-config init --disabled > ansible.cfg
-
編輯配置文件,修改核心參數:
[student@master ansible]$ vim ansible.cfg [defaults] inventory=/home/student/ansible/inventory # 主機清單路徑 remote_user=student # 遠程執行用戶 roles_path=/home/student/ansible/roles # 角色存放路徑 host_key_checking=False # 關閉 SSH 主機密鑰檢查(避免首次登錄交互) collections_path=/home/student/ansible/collections # 集合存放路徑[privilege_escalation] become=True # 啟用權限提升(普通用戶→root) become_ask_pass=False # 提升權限不詢問密碼 become_method=sudo # 提升方式為 sudo become_user=root # 提升到 root 用戶
步驟 7:驗證 Ansible 環境
執行 ping 模塊,測試所有被管節點連通性,成功標志所有節點返回 SUCCESS
,示例如下:
[student@master ansible]$ ansible all -m ping
node5 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
node1 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
node4 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
node3 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
node2 | SUCCESS => {"ansible_facts": {"discovered_interpreter_python": "/usr/bin/python3"},"changed": false,"ping": "pong"
}
至此,Ansible 控制主機部署完成,可通過 Ad-hoc 命令或 Playbook 對被管節點執行自動化任務。