文章目錄
- ansible 自動化運維工具 詳解
- 關于作者
- 作者介紹
- 一、ansible 概述
- 1.1 ansible 概述
- 1.2 是什么要使用 ansible
- 1.3 ansible 功能
- 1.4 ansible 優點
- 1.5 ansible作業機制
- 1.6 ansible 作業原理/流程圖
- 二、ansible 批量管理服務部署
- 2.1 實驗環境
- 2.2 官網安裝教程
- 2.2.1 ansible 安裝
- 2.2.2 ansible 命令引數介紹
- 2.2.3 定義主機清單
- 2.2.4 基于ssh 免密登錄
- 2.2.5 管理端 運行命令測驗
- 三、ansible 批量管理服務 應用
- 3.1 ansible ping 模塊實戰
- 3.1.0 ping模塊引數介紹
- 3.1.1 ansible 實作自己管理自己
- 3.1.2 ansible 匹配一個網段機器進行管理
- 3.2 ansible shell 模塊實戰
- 3.2.0 shell模塊介紹
- 3.2.1通過ansible 在docker 服務器上創建/查看目錄
- 3.2.2 通過ansible shell 模塊遠程查看nginx 服務是否啟動
- 3.2.3 通過ansible shell 模塊遠程查看防火墻狀態
- 3.2.4 通過ansible 批量修改 用戶密碼
- 3.3 ansible command 模塊實戰
- 3.3.0 command 模塊介紹
- 3.3.1 通過ansible 使用df -h 查看磁盤空間使用資訊
- 3.3.2 ansible 遠程查看 系統時間
- 3.3.3 Ansible 遠程執行下載http安裝包
- 3.4 ansible copy 模塊實戰
- 3.4.0 copy 模塊介紹
- 3.4.1 將 ansible 服務器上的 httpd安裝包遠程考到ip為10.0.0.5 服務器上
- 3.5 ansible yum 模塊實戰
- 3.5.0 yum 模塊介紹
- 3.5.1 ansible 服務器上 遠程在 10.0.0.6 服務器上安裝/啟動/卸載 httpd 服務
- 3.5.2 將www 組服務器中的所有yum 行程都殺掉
- 3.6 ansible file 模塊實戰(創建、洗掉、修改、權限、屬性的維護和管理)
- 3.6.0 ansible file 模塊介紹
- 3.6.1 在 docker 服務器上 /usr/local 目錄下面創建一個rivers的目錄
- 3.6.2 在docker 服務器上遠程創建一 腳本檔案
- 3.7 ansible cron 模塊實戰
- 3.7.0 cron 模塊介紹
- 3.7.1 利用cron 模塊創建/洗掉計劃任務---時間同步(阿里云)
- 3.7.2 給定時任務設定注釋資訊
- 3.8 ansible user 模塊實戰
- 3.8.0 模塊介紹
- 3.8.1 給docker 服務器創建一個用戶名稱 dock普通管理用戶
- 3.8.2 給web 服務器上的apache 服務創建/洗掉一個apache用戶
- 3.9 Ansible service模塊實戰
- 3.9.0 service 模塊介紹
- 3.9.1 遠程重啟web服務器上的防火墻 服務
- 3.9.2 遠程重啟eht0 網卡(平滑啟動)
- 3.9.3 遠程設定nfs 開啟自啟動,啟動級別3,5級別
- 3.10 ansible synchronize 實戰模塊
- 3.10.0 synchronize 模塊介紹
- 3.10.1 將源目錄同步至目標目錄(增量同步)
- 3.10.2 將源目錄同步至目標目錄(完全同步)
- 四、ansible 批量管理服務
- 4.1 ansible 劇本 介紹
- 4.1.0 ansible playbook劇本介紹和基礎使用說明
- 4.1.1 playbook 常見的組件及作用
- 4.1.2 遠程主機安裝httpd-2.4.46.tar.bz2 web服務案例演示
- 4.2 playbook 企業案列實戰
- 4.2.0 使用 ansible playbook 安裝 nginx WEB服務,安裝完并啟動Nginx 實戰
- 4.2.1 使用ansible playbook 批量創建系統用戶
- 五、ansible 綜合應用實戰 ---部署lnmp架構
- 5.1 實驗環境介紹
- 5.1.1 使用ansible playbook 搭建一鍵部署lnmp架構
- 5.1.2 整合 lnmp 架構、進行測驗
- 六、ansible 調優
- 6.1 ansible 組態檔詳解
- 6.2 ansible 常見優化介紹
- 6.1.1 關閉ssh 密鑰檢測
- 6.1.2 openssh 連接優化
- 6.1.3 加速 ansible 優化
- 總結
ansible 自動化運維工具 詳解
關于作者
-
作者介紹
🍊 博客主頁:作者主頁
🍊 簡介:云計算領域優質創作者🏆、學長的it故事 公眾號創作者🎓、在校期間參與眾多計算機相關的省賽、國賽,斬獲系列榮譽,考取華為資深工程師、紅帽工程師等系列認證,🍊 關注我:關注我簡歷模板、學習資料、檔案下載、技術支持
讓生命像一團熱烈燃燒的火,直到死亡才能使它熄滅
前言
? 最近有老鐵私信給我,讓我出一份ansible的博文,今天小編就和帶著學習新知識的思想,和大家一起復習自動化運維工具,在早起的自動化運維工具中,常見的有puppet、saltstack、ansible 等軟體,目前常用的就是ansible 和saltstack 2款工具,saltstack 與puppet 都是基于 C/S 模式作業的,他們需要安裝服務端與客戶端,基于Python 撰寫,加入MQ訊息同步,可以是執行命令和執行結果高效回傳,但執行程序需要等客戶端回傳,所以如果客戶端沒有及時回傳或回應,就可能導致部分機器沒有執行結果,目前用的較少,
今天就主要分享ansible 自動化運維工具,我將詳細的從概念、到基礎搞高級,層層遞進,讓你輕松掌握ansible自動化運維
當然,每一篇文章都不是100%的全,我能做的就是將我會的,最大限度的分享給你,讓你也可以掌握,
一、ansible 概述
1.1 ansible 概述
-
Ansible是一款自動化運維工具,基于Python開發,集合了眾多運維工具(puppet、cfengine、chef、func、fabric)的優點,實作了批量系統配置、批量程式部署、批量運行命令等功能,
-
Ansible 是一種安裝在控制節點上的無代理自動化工具,Ansible 從控制節點遠程管理機器和其他設備(默認情況下,通過 SSH 協議),Ansible 與Saltstack均是基于Python 語言開發,Ansible只需要在一臺普通的服務器上運行即可,不需要在客戶端服務器上安裝客戶端,因為 Ansible是基于SSH遠程管理,而Linux 服務器大都離不開SSH,所以Ansible不需要為配置作業添加額外的支持,
-
Ansible是基于模塊作業的,本身沒有批量部署的能力,真正具有批量部署的是Ansible所運行的模塊,Ansible只是提供一種框架,主要包括:
- (1) 連接插件connection plugins:負責和被監控端實作通信;
(2) host inventory:指定操作的主機,是一個組態檔里面定義監控的主機;
(3) 各種模塊核心模塊、command模塊、自定義模塊;
(4) 借助于插件完成記錄日志郵件等功能;
(5) playbook:劇本執行多個任務時,非必需可以讓節點一次性運行多個任務
- (1) 連接插件connection plugins:負責和被監控端實作通信;
-
架構圖
-

-
ansible 官網
1.2 是什么要使用 ansible
- 提高作業的效率
- 提高作業準確度
- 減少維護的成本
- 減少重復性作業
1.3 ansible 功能
- 可以實作批量系統操作配置
- 可以實作批量軟體服務部署
- 可以實作批量檔案資料分發
- 可以實作批量系統資訊收集
1.4 ansible 優點
- 管理端不需要啟動服務程式(no server)
- 管理端不需要撰寫組態檔(/etc/ansible/ansible.cfg)
- 受控端不需要安裝軟體程式(libselinux-python)
- 受控端不需要啟動服務程式(no agent)
- 服務程式管理操作模塊眾多(module)
- 利用劇本撰寫來實作自動化(playbook)
- 支持sudo 普通用戶
1.5 ansible作業機制
-

# 由上面的圖可以看到 Ansible 的組成由 5 個部分組成: #Ansible : ansible核心 #Modules : 包括 Ansible 自帶的核心模塊及自定義模塊 #Plugins : 完成模塊功能的補充,包括連接插件、郵件插件等 #Playbooks : 劇本;定義 Ansible 多任務組態檔,由Ansible 自動執行 #Inventory : 定義 Ansible 管理主機的清單 [??nv?ntri] 清單
1.6 ansible 作業原理/流程圖
二、ansible 批量管理服務部署
2.1 實驗環境
-
實驗環境
名稱 系統 ip地址 ansible_server Centos 7.6 10.0.0.61 Host-01 Centos 7.6 10.0.0.5 Host-02 Centos 7.6 10.0.0.6 Host-03 Centos 7.6 10.0.0.7 -
實驗拓撲
-

2.2 官網安裝教程
2.2.1 ansible 安裝
-
官網安裝步驟
# Centso 需要設定EPEL倉庫 [root@ansible_server~]# yum install epel-release # 使用yum 安裝ansible [root@ansible_server~]# yum install ansible # 查看ansible 安裝版本 [root@ansible_server~]# ansible --version ansible 2.9.25 config file = /etc/ansible/ansible.cfg configured module search path = [u'/root/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python2.7/site-packages/ansible executable location = /usr/bin/ansible python version = 2.7.5 (default, Oct 30 2018, 23:45:53) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
2.2.2 ansible 命令引數介紹
-
ansible 常見引數
# ansible 命令引數 # anisble命令語法: ansible [-i 主機檔案] [-f 批次] [組名] [-m 模塊名稱] [-a 模塊引數] ansible詳細引數: -v,-verbose # 詳細模式,如果命令執行成功,輸出詳細的結果 (-vv –vvv -vvvv) -i PATH, -inventory=PATH # 指定 host 檔案的路徑,默認是在 /etc/ansible/hosts inventory [??nv?ntri] 庫存 -f NUM,-forks=NUM # NUM 是指定一個整數,默認是 5 ,指定 fork 開啟同步行程的個數, -m NAME,-module-name=NAME # 指定使用的 module 名稱,默認使用 command模塊 -a,MODULE_ARGS #指定 module 模塊的引數 -k,-ask-pass #提示輸入 ssh 的密碼,而不是使用基于 ssh 的密鑰認證 -sudo # 指定使用 sudo 獲得 root 權限 -K,-ask-sudo-pass #提示輸入 sudo 密碼,與 -sudo 一起使用 -u USERNAME,-user=USERNAME # 指定移動端的執行用戶 -C,-check #測驗此命令執行會改變什么內容,不會真正的去執行 # ansible-doc詳細引數: ansible-doc -l #列出所有的模塊串列 ansible-doc -s 模塊名 #查看指定模塊的引數 -s, snippet [?sn?p?t] 片斷 [root@ansible_server~]# ansible-doc -s service #列出模塊使用簡介 [root@ansible_server~]# ansible-doc -l # 指定一個模塊詳細說明 [root@ansible_server~]# ansible-doc -s fetch # 查詢模塊在劇本中應用方法 [root@ansible_server~]# ansible-doc fetch -
Ansible基于多模塊管理,常用的Ansible工具管理模塊包括:
command、shell、script、yum、copy、File、async、docker、cron、mysql_user、ping、sysctl、user、acl、add_host、easy_install、haproxy_等, -
command 不支持管道符,可以使用shell模塊
-
可以使用
ansible-doc -l more查看ansible支持的模塊,也可以查看每個模塊的幫助檔案,ansible-doc module name -

2.2.3 定義主機清單
-
定義主機清單
[root@ansible_server~]# cd /etc/ansible/ [root@ansible_server/etc/ansible]# ll total 24 -rw-r--r-- 1 root root 19985 Aug 22 04:07 ansible.cfg -rw-r--r-- 1 root root 1016 Aug 22 04:07 hosts drwxr-xr-x 2 root root 6 Aug 22 04:07 roles # 操作組態檔前,習慣先備份 [root@ansible_server/etc/ansible]# cp -f hosts hosts.backup [root@ansible_server/etc/ansible]# ll total 28 -rw-r--r-- 1 root root 19985 Aug 22 04:07 ansible.cfg -rw-r--r-- 1 root root 1016 Aug 22 04:07 hosts -rw-r--r-- 1 root root 1016 Sep 14 15:34 hosts.backup drwxr-xr-x 2 root root 6 Aug 22 04:07 roles # 情況hosts 組態檔內容,寫hosts檔案 [root@ansible_server/etc/ansible]# cat /dev/null > hosts [root@ansible_server/etc/ansible]# cat hosts # 寫入主機IP ,這些寫了[webserver]、[docker] 等都是分組,可以不寫(這樣不方便管理) [root@ansible_server/etc/ansible]# cat hosts [webserver] 10.0.0.5 10.0.0.6 # 管理10.0.0.7 docker服務器 [docker] 10.0.0.7 # 本機也可以管理自己管自己 [manager] 10.0.0.61 [root@ansible_server/etc/ansible]#
2.2.4 基于ssh 免密登錄
-
ssh 免密登錄
# 第一個歷程: 創建秘鑰對(管理端服務器) sh-keygen -t 秘鑰的型別(dsa|rsa) #第二個歷程: 將公鑰進行分發(被管理端服務器) ssh-copy-id -i /root/.ssh/id_dsa.pub root@10.0.0.5 # 本地生成rsa 密鑰對 [root@ansible_server~]# ssh-keygen Generating public/private rsa key pair. Enter file in which to save the key (/root/.ssh/id_rsa): # 直接回車 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:HhsjmGY6DJoSREojVpJmSI63vuoXKy6sK2ESh/eQJr0 root@ansible_server The key's randomart image is: +---[RSA 2048]----+ |+Bo. | |X+o | |==.. | |=.B. o | |o*.+= . S | |+BE=. o = | |B.= o o | |+o = | |X=+ | +----[SHA256]-----+ # 上傳到被管理端 [root@web01_server~]# ssh-copy-id root@10.0.0.5 [root@web01_server~]# ssh-copy-id root@10.0.0.6 [root@web01_server~]# ssh-copy-id root@10.0.0.7
2.2.5 管理端 運行命令測驗
-
ping模塊檢查網路連通性
# 常用引數 -i # 指定 host 檔案的路徑,默認是在 /etc/ansible/hosts ,用的時候可以省略 -m # 指定使用的ping模塊 [root@ansible_server~]# ansible -i /etc/ansible/hosts 'www' -m ping 10.0.0.5 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } 10.0.0.6 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } [root@ansible_server~]# -

三、ansible 批量管理服務 應用
3.1 ansible ping 模塊實戰
3.1.0 ping模塊引數介紹
-
模塊介紹
Ansible最基礎的模塊為ping模塊,主要用于判斷遠程客戶端是否在線,用于ping本身服務器,回傳值為changed、ping,
3.1.1 ansible 實作自己管理自己
-
自己管理自己
# ansible基于ssh連接-i (inventory)引數后指定的遠程主機時,也可以寫埠,用戶,密碼, 格式:ansible_ssh_port:指定ssh埠 ansible_ssh_user:指定 ssh 用戶 ansible_ssh_pass:指定 ssh 用戶登錄是認證密碼(明文密碼不安全) ansible_sudo_pass:指明 sudo 時候的密碼 [root@ansible_server~]# cat /etc/ansible/hosts [www] 10.0.0.5 10.0.0.6 [docker] 10.0.0.7 [manager] 10.0.0.61 ansible_ssh_port=22 ansible_ssh_user=root ansible_ssh_pass=123456 [root@ansible_server~]# # 同時還可以基于SSH 免密登錄,2種方式選一種, # 將自己的密鑰拷貝到本地,然后ssh 連接一下即可, [root@web01_server~]# ssh-copy-id root@10.0.0.61
3.1.2 ansible 匹配一個網段機器進行管理
-
ping 網段的連通性
# ping 某一個網段 [root@ansible_server~]# ansible 10.0.0.* -m ping # 路徑是可以省略的 -i ,因為默認路徑就是 /etc/ansible/hosts [root@ansible_server~]# ansible -i /etc/ansible/hosts "docker" -m ping 10.0.0.7 | SUCCESS => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": false, "ping": "pong" } [root@ansible_server~]# -

3.2 ansible shell 模塊實戰
3.2.0 shell模塊介紹
-
模塊介紹
# Ansible shell模塊主要用于遠程客戶端上執行各種Shell命令或者運行腳本,遠程執行命令通過/bin/sh環境來執行,支持比command更多的指令,Shell模塊使用詳解: # 常用引數 chdir 執行命令前,切換到目錄; creates 當該檔案存在時,則不執行該步驟; executable 換用shell環境執行命令; free_form 需要執行的腳本; removes 當該檔案不存在時,則不執行該步驟; warn 如果在ansible.cfg中存在告警,如果設定了False,不會警告此行,
3.2.1通過ansible 在docker 服務器上創建/查看目錄
-
創建/查看目錄
# 在docker服務器上 ,在/opt 目錄下創建一個名字為rivers+當前時間的目錄 [root@ansible_server~]# ansible docker -m shell -a "mkdir /opt/rivers`date +%F`;ls /opt" [WARNING]: Consider using the file module with state=directory rather than running 'mkdir'. If you need to use command because file is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. 10.0.0.7 | CHANGED | rc=0 >> rivers2021-09-16 # 通過ls 查看是否創建 [root@ansible_server~]# ansible docker -m shell -a "ls /opt" 10.0.0.7 | CHANGED | rc=0 >> rivers2021-09-16 [root@ansible_server~]# -

3.2.2 通過ansible shell 模塊遠程查看nginx 服務是否啟動
-
遠程查看nginx 服務狀態
# 遠程查看docker服務器上nginx 行程資訊 [root@ansible_server~]# ansible docker -m shell -a "ps -ef|grep nginx" 10.0.0.7 | CHANGED | rc=0 >> root 11711 1 0 15:25 ? 00:00:00 nginx: master process nginx nginx 11712 11711 0 15:25 ? 00:00:00 nginx: worker process root 11775 11770 3 15:26 pts/0 00:00:00 /bin/sh -c ps -ef|grep nginx root 11777 11775 0 15:26 pts/0 00:00:00 grep nginx [root@ansible_server~]# -

3.2.3 通過ansible shell 模塊遠程查看防火墻狀態
-
遠程查看docker 服務器上的防火墻狀態
# ansible 服務器上遠程查看 dockerf 服務器行的防火墻狀態是否運行 [root@ansible_server~]# ansible docker -m shell -a "firewall-cmd --state" 10.0.0.7 | CHANGED | rc=0 >> running [root@ansible_server~]# -

3.2.4 通過ansible 批量修改 用戶密碼
-
批量修改用戶密碼
# 通通過shell 遠程批量修改密碼 [root@ansible_server~]# ansible www -m shell -a "echo 123456|passwd --stdin root" 10.0.0.5 | CHANGED | rc=0 >> Changing password for user root. passwd: all authentication tokens updated successfully. 10.0.0.6 | CHANGED | rc=0 >> Changing password for user root. passwd: all authentication tokens updated successfully. -

3.3 ansible command 模塊實戰
3.3.0 command 模塊介紹
-
command 模塊介紹
# Ansible command模塊為ansible默認模塊,主要用于執行Linux基礎命令,可以執行遠程服務器命令執行、任務執行等操作,Command模塊使用詳解: Chdir 執行命令前,切換到目錄; Creates 當該檔案存在時,則不執行該步驟; Executable 換用shell環境執行命令; Free_form 需要執行的腳本; Removes 當該檔案不存在時,則不執行該步驟; Warn 如果在ansible.cfg中存在告警,如果設定了False,不會警告此行,
3.3.1 通過ansible 使用df -h 查看磁盤空間使用資訊
-
查看磁盤空間
# 使用df -h 查看 docker組中機器的磁盤使用情況 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.7 -m command -a "df -h" 10.0.0.7 | CHANGED | rc=0 >> Filesystem Size Used Avail Use% Mounted on /dev/sda3 99G 1.7G 97G 2% / devtmpfs 442M 0 442M 0% /dev tmpfs 453M 0 453M 0% /dev/shm tmpfs 453M 7.2M 446M 2% /run tmpfs 453M 0 453M 0% /sys/fs/cgroup /dev/sda1 197M 105M 93M 54% /boot tmpfs 91M 0 91M 0% /run/user/0 [root@ansible_server~]# # 簡便方式寫(效果一樣的) [root@ansible_server~]# ansible docker -m command -a "df -h" # 能否使用shell 查看呢 ? [root@ansible_server~]# ansible docker -m shell -a "df -h" # 答案:可以查看,shell 本質上和command 區別不大,知識支持的命令更多 -

3.3.2 ansible 遠程查看 系統時間
-
使用date 命令查看時間
# 查看docker 服務器上的時間 [root@ansible_server~]# ansible docker -m command -a "date %F" 10.0.0.7 | FAILED | rc=1 >> date: invalid date ‘%F’non-zero return code [root@ansible_server~]# -

3.3.3 Ansible 遠程執行下載http安裝包
-
遠程下載
# 先遠程查看docker服務器上有沒有httpd安裝包 [root@ansible_server~]# ansible docker -m command -a "ls" 10.0.0.7 | CHANGED | rc=0 >> anaconda-ks.cfg # 在docker 服務器上遠程下載httpd-2.4.46.tar.bz2 [root@ansible_server~]# ansible docker -m command -a "wget https://downloads.apache.org/httpd/httpd-2.4.46.tar.bz2" -

3.4 ansible copy 模塊實戰
3.4.0 copy 模塊介紹
-
模塊介紹
# Ansible copy模塊主要用于檔案或者目錄拷貝,支持檔案、目錄、權限、用戶組功能,copy模塊使用詳解: # 常用引數 src Ansible端源檔案或者目錄,空檔案夾不拷貝; content 用來替代src,用于將指定檔案的內容,拷貝到遠程檔案內; dest 客戶端目標目錄或者檔案,需要絕對路徑; backup # 拷貝之前,先備份遠程節點上的原始檔案;backup=yes directory_mode 用于拷貝檔案夾,新建的檔案會被拷貝,而老舊的不會被拷貝; follow 支持link檔案拷貝; force 覆寫遠程主機不一致的內容; group 設定遠程主機檔案夾的組名; mode 指定遠程主機檔案及檔案及的權限; owner 設定遠程主機檔案夾的用戶名,擁有者,
3.4.1 將 ansible 服務器上的 httpd安裝包遠程考到ip為10.0.0.5 服務器上
-
遠程拷貝
#將ansible 服務器上的http 安裝包遠程 拷貝到 10.0.0.5 服務器上,且權限設定為 755 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m command -a "ls" 10.0.0.5 | CHANGED | rc=0 >> anaconda-ks.cfg [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m copy -a "src=/root/httpd-2.4.46.tar.bz2 dest=/root mode=755" 10.0.0.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "checksum": "1b7cd10ff3a2a07a576d77e34f0204d95fa4aceb", "dest": "/root/httpd-2.4.46.tar.bz2", "gid": 0, "group": "root", "md5sum": "7d661ea5e736dac5e2761d9f49fe8361", "mode": "0755", "owner": "root", "size": 7187805, "src": "/root/.ansible/tmp/ansible-tmp-1631779035.15-12735-92572188724853/source", "state": "file", "uid": 0 } [root@ansible_server~]# # 查看10.0.0.5 檔案是否拷貝成功 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.5 -m command -a "ls" 10.0.0.5 | CHANGED | rc=0 >> anaconda-ks.cfg httpd-2.4.46.tar.bz2 [root@ansible_server~]# # 查看同一個www組中的 10.0.0.6 是否也有 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "ls" 10.0.0.6 | CHANGED | rc=0 >> anaconda-ks.cfg [root@ansible_server~]#
3.5 ansible yum 模塊實戰
3.5.0 yum 模塊介紹
-
yum 模塊介紹
#Ansible yum 模塊主要用于軟體的安裝、升級、卸載,支持紅帽.rpm軟體的管理,YUM模塊使用詳解: # 常用引數 conf_file 設定遠程yum執行時所依賴的yum組態檔 disable_gpg_check 安裝軟體包之前是否堅持gpg key; name 需要安裝的軟體名稱,支持軟體組安裝; update_cache 安裝軟體前更新快取; enablerepo 指定repo源名稱; skip_broken 跳過例外軟體節點; state #軟體包狀態,包括:installed、present、latest、absent、removed,
3.5.1 ansible 服務器上 遠程在 10.0.0.6 服務器上安裝/啟動/卸載 httpd 服務
-
遠程安裝/卸載
# 遠程在 10.0.0.6 服務器上安裝 httpd 服務 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m yum -a "name=httpd state=installed" # 遠程啟動 httpd 服務 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "systemctl start httpd" 10.0.0.6 | CHANGED | rc=0 >> # 遠程查看http服務行程 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m shell -a "ps -ef|grep httpd" 10.0.0.6 | CHANGED | rc=0 >> root 10924 1 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 10925 10924 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 10926 10924 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 10928 10924 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 10929 10924 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND apache 10930 10924 0 16:11 ? 00:00:00 /usr/sbin/httpd -DFOREGROUND root 11066 11061 0 16:12 pts/0 00:00:00 /bin/sh -c ps -ef|grep httpd root 11068 11066 0 16:12 pts/0 00:00:00 grep httpd # 停止 httpd 服務 [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m command -a "systemctl stop httpd" 10.0.0.6 | CHANGED | rc=0 >> # 遠程卸載 httpd [root@ansible_server~]# ansible -i /etc/ansible/hosts 10.0.0.6 -m yum -a "name=httpd state=removed" -

3.5.2 將www 組服務器中的所有yum 行程都殺掉
-
停止yum 行程
# 停止正在作業的yum行程 [root@ansible_server~]# ansible www -m shell -a "pkill -yum" # 查看行程 [root@ansible_server~]# ansible www -m shell -a "ps -ef|grep yum" 10.0.0.6 | CHANGED | rc=0 >> root 12032 12027 0 09:12 pts/1 00:00:00 /bin/sh -c ps -ef|grep yum root 12034 12032 0 09:12 pts/1 00:00:00 grep yum 10.0.0.5 | CHANGED | rc=0 >> root 12260 12255 0 09:12 pts/0 00:00:00 /bin/sh -c ps -ef|grep yum root 12262 12260 0 09:12 pts/0 00:00:00 grep yum [root@ansible_server~]#
3.6 ansible file 模塊實戰(創建、洗掉、修改、權限、屬性的維護和管理)
3.6.0 ansible file 模塊介紹
-
file 模塊介紹
# Ansible file模塊主要用于對檔案的創建、洗掉、修改、權限、屬性的維護和管理,File模塊使用詳解: # 常用引數介紹 src Ansible端源檔案或者目錄; follow 支持link檔案拷貝; force 覆寫遠程主機不一致的內容; group 設定遠程主機檔案夾的組名; mode 指定遠程主機檔案及檔案及的權限; owner 設定遠程主機檔案夾的用戶名; path 目標路徑,也可以用dest,name代替; state #狀態包括:file、link、directory、hard、touch、absent; attributes 檔案或者目錄特殊屬性,
3.6.1 在 docker 服務器上 /usr/local 目錄下面創建一個rivers的目錄
-
使用file模塊創建目錄
# 在 docker 服務器上 /usr/local 目錄下面創建一個rivers的目錄,名字為 rivers,屬主為nginx [root@ansible_server~]# ansible docker -m file -a "path=/usr/local/rivers state=directory mode=644 owner=nginx" 10.0.0.7 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "gid": 0, "group": "root", "mode": "0644", "owner": "nginx", "path": "/usr/local/rivers", "size": 6, "state": "directory", "uid": 998 } # 查看常見的目錄 [root@ansible_server~]# ansible docker -m command -a "ls /usr/local/rivers" 10.0.0.7 | FAILED | rc=2 >> # path表示目錄的名稱和路徑, state=directory表示創建目錄 -

3.6.2 在docker 服務器上遠程創建一 腳本檔案
-
遠程創建檔案
#在/usr/local/rivers/script.sh檔案 [root@ansible_server~]# ansible docker -m file -a "path=/usr/local/rivers/script.sh state=touch mode=777" 10.0.0.7 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "dest": "/usr/local/rivers/script.sh", "gid": 0, "group": "root", "mode": "0777", "owner": "root", "size": 0, "state": "file", "uid": 0 } [root@ansible_server~]# # path表示目錄的名稱和路徑, state=touch 表示創建檔案 -

3.7 ansible cron 模塊實戰
3.7.0 cron 模塊介紹
-
cron 模塊介紹
# Ansible cron模塊主要用于添加、洗掉、更新作業系統Crontab任務計劃,Cron模塊使用詳解: # 常用引數 name 任務計劃名稱; cron_file 替換客戶端該用戶的任務計劃的檔案; minute 分( 0-59 ,* ,*/2 ); hour 時( 0-23 ,* ,*/2 ); day 日( 1-31 ,* ,*/2 ); month 月( 1-12 ,* ,*/2 ); weekday 周( 0-6 或 1-7 ,* ); job 任何計劃執行的命令,state要等于present; backup 是否備份之前的任務計劃; user 新建任務計劃的用戶; state 指定任務計劃present、absent,
3.7.1 利用cron 模塊創建/洗掉計劃任務—時間同步(阿里云)
-
基本同步用法
# docker 服務器 自動同步 阿里云 [root@ansible_server~]# ansible 10.0.0.7 -m cron -a "minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" [DEPRECATION WARNING]: The 'name' parameter will be required in future releases.. This feature will be removed in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. 10.0.0.7 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "envs": [], "jobs": [ "None" ] } # 如何洗掉定時任務 [root@ansible_server~]# ansible docker -m cron -a "name='#Ansible' state=absent"
3.7.2 給定時任務設定注釋資訊
-
添加定時任務 名稱注釋
# 添加注釋資訊 [root@ansible_server~]# ansible 10.0.0.6 -m cron -a "name='time sync' minute=0 hour=2 job='/usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1'" 10.0.0.6 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "envs": [], "jobs": [ "time sync" ] } #如何查看10.0.0.6這臺服務器上的定時任務 [root@ansible_server~]# ansible 10.0.0.6 -m shell -a "cat /var/spool/cron/root" 10.0.0.6 | CHANGED | rc=0 >> #time sync by lidao at 2017-03-08 */5 * * * * /usr/sbin/ntpdate ntp.aliyun.com >/dev/null 2>&1 #Ansible: time sync 0 2 * * * /usr/sbin/ntpdate ntp1.aliyun.com >/dev/null 2>&1
3.8 ansible user 模塊實戰
3.8.0 模塊介紹
-
user 模塊介紹
# Ansible user模塊主要用于作業系統用戶、組、權限、密碼等操作,user模塊使用詳解: system 默認創建為普通用戶,為yes則創建系統用戶; append 添加一個新的組; comment 新增描述資訊; createhome 給用戶創建家目錄; force 用于洗掉強制洗掉用戶; group 創建用戶主組; groups 將用戶加入組或者附屬組添加; home 指定用戶的家目錄; name 表示狀態,是否create、remove、modify; password 指定用戶的密碼,此處為加密密碼; remove 洗掉用戶; shell 設定用戶的shell登錄環境; uid 設定用戶id; update_password 修改用戶密碼; state 用戶狀態,默認為present表示新建用戶,absent表示洗掉
3.8.1 給docker 服務器創建一個用戶名稱 dock普通管理用戶
-
創建普通用戶
# 在docker 服務器上創建一個普通用戶 nidaye,name=用戶名,home=用戶家目錄 [root@ansible_server~]# ansible docker -m user -a "name=nidaye home=/home/admin" 10.0.0.7 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/home/admin", "name": "nidaye", "shell": "/bin/bash", "state": "present", "system": false, "uid": 1001 } # 查看剛新建的用戶 nidaye [root@ansible_server~]# ansible docker -m shell -a "id nidaye" 10.0.0.7 | CHANGED | rc=0 >> uid=1001(nidaye) gid=1001(nidaye) groups=1001(nidaye) [root@ansible_server~]# -

3.8.2 給web 服務器上的apache 服務創建/洗掉一個apache用戶
-
創建apache用戶
# 在10.0.0.5 服務器上 創建一個 名字apache ,shell /sbin/nologin 家目錄為/opt [root@ansible_server~]# ansible 10.0.0.5 -m user -a "name=apache home=/opt shell=/sbin/nologin" 10.0.0.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "comment": "", "create_home": true, "group": 1001, "home": "/opt", "name": "apache", "shell": "/sbin/nologin", "state": "present", # 洗掉apache用戶 [root@ansible_server~]# ansible 10.0.0.5 -m user -a "name=apache state=absent force=yes" 10.0.0.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "force": true, "name": "apache", "remove": false, "state": "absent" } [root@ansible_server~]# -

-

3.9 Ansible service模塊實戰
3.9.0 service 模塊介紹
-
service 模塊介紹
# Ansible service模塊主要用于遠程客戶端各種服務管理,包括啟動、停止、重啟、重新加載等,service模塊使用詳解: # 常用引數 enabled 是否開啟啟動服務; name 服務名稱; runlevel 服務啟動級別; arguments 服務命令列引數傳遞; state 服務操作狀態,狀態包括started, stopped, restarted, reloaded,
3.9.1 遠程重啟web服務器上的防火墻 服務
-
遠程重啟/關閉firewalld服務
# 遠程重啟10.0.0.5 服務器上的 firewalld 服務器 [root@ansible_server~]# ansible 10.0.0.5 -m service -a "name=firewalld state=restarted" 10.0.0.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "firewalld", "state": "started", "status": { "ActiveEnterTimestampMonotonic": "0", "ActiveExitTimestampMonotonic": "0", "ActiveState": "inactive", …… [root@ansible_server~]# # 遠程關閉防火墻 [root@ansible_server~]# ansible 10.0.0.5 -m service -a "name=firewalld state=stopped"
3.9.2 遠程重啟eht0 網卡(平滑啟動)
-
平滑啟動 10.0.0.5 服務器上的網卡
# 遠程啟動 eht0 網卡 [root@ansible_server~]# ansible 10.0.0.5 -m service -a "name=network args=eth0 state=restarted" [WARNING]: Ignoring "args" as it is not used in "systemd" 10.0.0.5 | CHANGED => { "ansible_facts": { "discovered_interpreter_python": "/usr/bin/python" }, "changed": true, "name": "network", "state": "started", "status": { "ActiveEnterTimestamp": "Fri 2021-08-27 16:30:41 CST", ……
3.9.3 遠程設定nfs 開啟自啟動,啟動級別3,5級別
-
遠程設定開機自啟動
# 遠程開機啟動nfs服務,設定3,5級別自動啟動 root@ansible_server~]# ansible -k all -m service -a "name=nfs enabled=yes runlevel=3,5" SSH password: [WARNING]: Ignoring "runlevel" as it is not used in "systemd" [WARNING]: Ignoring "runlevel" as it is not used in "systemd" [WARNING]: Ignoring "runlevel" as it is not used in "systemd"
3.10 ansible synchronize 實戰模塊
3.10.0 synchronize 模塊介紹
-
模塊介紹
# Ansible synchronize模塊主要用于目錄、檔案同步,基于Rsync命令同步目錄,Synchronize模塊使用詳解: # 常用引數 compress 開啟壓縮,默認為開啟; archive 是否采用歸檔模式同步,保證源和目標檔案屬性一致; checksum 是否效驗; dirs 以非遞回的方式傳輸目錄; links 同步鏈接檔案; recursive 是否遞回yes/no; rsync_opts 使用rsync 的引數; copy_links 同步的時候是否復制連接; delete 洗掉源中沒有而目標存在的檔案; src 源目錄及檔案; dest 目標目錄及檔案; dest_port 目標接受的埠; rsync_path 服務的路徑,指定 rsync 命令來在遠程服務器上運行; rsync_timeout 指定rsync操作的IP超時時間; set_remote_user 設定遠程用戶名; --exclude=.log 忽略同步.log結尾的檔案; mode 同步的模式,rsync同步的方式PUSH、PULL,默認都是推送push,
3.10.1 將源目錄同步至目標目錄(增量同步)
-
將源目錄同步至目標目錄(增量同步)
# 將本地/tmp 目錄遠程同步到 docker服務器上(增量同步) [root@ansible_server~]# ansible docker -m synchronize -a 'src=/tmp/ dest=/tmp/' 10.0.0.7 | CHANGED => { "changed": true, "cmd": "/usr/bin/rsync --delay-updates -F --compress --archive --rsh=/usr/bin/ssh -S none -o StrictHostKeyChecking=no -o UserKnownHostsFile=/dev/null --out-format=<<CHANGED>>%i %n%L /tmp/ 10.0.0.7:/tmp/", "msg": ".d..t...... ./\n<f+++++++++ xxx.xx\ncd+++++++++ ansible_synchronize_payload_Bxuy_S/\n<f+++++++++ ansible_synchronize_payload_Bxuy_S/ansible_synchronize_payload.zip\ncd+++++++++ vmware-root_6044-692160431/\n", "rc": 0, "stdout_lines": [ ".d..t...... ./", "<f+++++++++ xxx.xx", "cd+++++++++ ansible_synchronize_payload_Bxuy_S/", "<f+++++++++ ansible_synchronize_payload_Bxuy_S/ansible_synchronize_payload.zip", "cd+++++++++ vmware-root_6044-692160431/" ] } [root@ansible_server~]# -

3.10.2 將源目錄同步至目標目錄(完全同步)
-
將源目錄同步至目標目錄(增量同步)
# 完全同步,尾巴后添加 delete=yes [root@ansible_server/tmp]# ansible docker -m synchronize -a 'src=/tmp/ dest=/tmp/ delete=yes'
四、ansible 批量管理服務
4.1 ansible 劇本 介紹
4.1.0 ansible playbook劇本介紹和基礎使用說明
-
劇本介紹
# 通過Playbook任務,能夠集中管理多個任務,將多個任務關聯起來,從而實作更加復雜作業,滿足生產環境的各個需求,提高運維人員管理服務器效率 # Playbook劇本的產物YAML檔案,類似XML介面(Tomcat)檔案,內部有固定語法、引數等,要掌握常用的YAML語法撰寫,掌握滿足運維管理方向的語法即可 -
playbook 語法
# yaml 格式語法 # Playbook是以YAML格式撰寫的文本檔案,通常使用擴展名yml保存,Playbook使用空格字符縮進來表示其資料結構,YAML對用于縮進的空格數量沒有嚴格的要求,但有兩個基本的規則: 處于層次結構中同一級別的資料元素(例如同一串列中的專案)必須具有相同的縮進量, 如果專案屬于其他專案的子項,其縮進量必須大于父項 # 只有空格字符可用于縮進,不允許使用tab鍵,約定俗成的縮進量一般是一級2個空格, # Playbook開頭的一行由三個破折號(---)組成,這是檔案開始標記,其末尾可能使用三個圓點(...)作為檔案結束標記,盡管在實踐中這通常會省略, -
playbook 運行/語法檢查
# 運行 lnmp檔案 [root@ansible_server~]# ansible-playbook lnmp.yml # 檢查lnmp 語法 [root@ansible_server~]# ansible-playbook --syntax-check lnmp.yml playbook是一個不同于使用ansible命令列執行方式的模式,功能更強大更靈活, 1、在playbooks 中常見的定義任務: name: task description #任務描述資訊 module_name: module_args #需要使用的模塊名字: 模塊引數 files:存放需要同步到異地服務器的原始碼檔案及組態檔; handlers:當服務的組態檔發生變化時需要進行的操作,比如:重啟服務,重新加載組態檔, handlers meta:角色定義,可留空; tasks:需要進行的執行的任務; templates:用于執行lamp安裝的模板檔案,一般為腳本 vars:本次安裝定義的變數 #playbook是由一個或多個"play"組成的串列,play的主要功能在于將事先歸為一組的主機裝扮成事先通過ansible中的task定義好的角色, #github上提供了大量的實體供大家參考 https://github.com/ansible/ansible-examples
4.1.1 playbook 常見的組件及作用
-
playbook 組件
-
Target 定義playbook的遠程主機組;
-
常用引數:
-
hosts 定義遠程主機組;
-
user 執行該任務的用戶;
-
sudo 設定為yes的時候,執行任務的時候使用root權限;
-
sudo_user 指定sudo普通用戶;
-
connection 默認基于SSH連接客戶端;
-
gather_facks 獲取遠程主機facts基礎資訊,
-
-
-
Variable 定義playbook使用的變數;
- 常用引數:
- vars 定義格式,變數名:變數值;
- vars_files 指定變數檔案;
- vars_prompt 用戶互動模式自定義變數;
- setup 模塊去遠程主機的值;
- 常用引數:
-
Task 定義遠程主機上執行的任務串列;
- 常用引數:
- name 任務顯示名稱也即螢屏顯示資訊;
- action 定義執行的動作;
- copy 復制本地檔案到遠程主機;
- template 復制本地檔案到遠程主機,可以參考本地變數;
- service 定義服務的狀態,
- 常用引數:
-
Handler 當服務的組態檔發生變化時需要進行的操作,比如:
重啟服務,重新加載配置
-
4.1.2 遠程主機安裝httpd-2.4.46.tar.bz2 web服務案例演示
-
遠程安裝演示
# 通過ansible 給www組服務器里面的 10.0.0.5 這臺服務器安裝httpd-2.4.46.tar.bz2服務 [root@ansible_server~]# cat httpd_install.yaml #author rivers --- - hosts: 10.0.0.5 remote_user: root tasks: - name: install httpd-2.4.tar.bz2 yum: name=apr,apr-devel,apr-util,apr-util-devel,gcc-c++,bzip2 state=installed - name: apache httpd-2.4 install process shell: cd /opt/; rm -rf httpd-2.4.46.tar.bz2; wget https://downloads.apache.org/httpd/httpd-2.4.46.tar.bz2; tar xjf httpd-2.4.46.tar.bz2 -C /usr/local/; cd /usr/local/httpd-2.4.46/; ./configure --prefix=/usr/local/apache --with-pmp=worker; make -j4; make install; ... -

-
檢查 httpd_install.yaml 語法檔案
# 檢查 httpd_install.yaml 語法,下面是正確通過的 [root@ansible_server~]# ansible-playbook --syntax-check httpd_install.yaml playbook: httpd_install.yaml # 常見報錯提示 [root@ansible_server~]# ansible-playbook --syntax-check httpd_install.yaml ERROR! We were unable to read either as JSON nor YAML, these are the errors we got from each: JSON: No JSON object could be decoded Syntax Error while loading YAML. mapping values are not allowed in this context The error appears to be in '/root/httpd_install.yaml': line 8, column 10, but may be elsewhere in the file depending on the exact syntax problem. The offending line appears to be: -name: install httpd-2.4.tar.bz2 yum: name=apr apr-devel apr-util apr-util-devel gcc-c++ bzip2 state=installed ^ here -

-
安裝 成功
-

4.2 playbook 企業案列實戰
4.2.0 使用 ansible playbook 安裝 nginx WEB服務,安裝完并啟動Nginx 實戰
-
playbook劇本
# 判斷是否安裝有安裝后的nginx目錄,如果有直接啟動,如果沒有安裝nginx #author rivershosts: 10.0.0.7 remote_user: root tasks: - name: install yum_package yum: name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++ state=installed - name: echo -e "\033[44;37m 判斷是否存在nginx安裝目錄 \033[0m" file: path=/usr/local/nginx state=directory notify: - nginx start - nginx install handlers: - name: nginx install shell: cd /opt/; rm -rf nginx-1.20.1.tar.gz; wget http://nginx.org/download/nginx-1.20.1.tar.gz; tar xf nginx-1.20 .1.tar.gz -C /usr/local/; useradd -M -s /sbin/nologin nginx; mkdir -p /var/log/nginx;cd /usr/local/nginx-1.20.1; ./confi gure --user=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_mo dule --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/ng inx/access.log --error-log-path=/var/log/nginx/error.log --without-http_rewrite_module; make && make install; /usr/local /nginx/sbin/nginx -t; - name: nginx start shell: /usr/local/nginx/sbin/nginx4.2.1 使用ansible playbook 批量創建系統用戶
-
批量創建系統用戶
# 使用 user 模塊批量創建rivers01、02、03用戶 # author rivers - hosts: 10.0.0.7 remote_user: root tasks: - name: Add User list. user: name={{ item }} state=present with_items: - rivers01 - rivers02 - rivers03 - name: check system user shell: num= `tail -3 /etc/passwd|grep -c rivers`; if [ $num ge 3 ]; then echo is ok!; fi -

-

五、ansible 綜合應用實戰 —部署lnmp架構
5.1 實驗環境介紹
-
實驗環境介紹
名稱 系統 ip地址 部署服務 ansible_server Centos 7.6 10.0.0.61 ansible Host01 Centos 7.6 10.0.0.5 nginx+php Host02 Centos 7.6 10.0.0.6 mysql
5.1.1 使用ansible playbook 搭建一鍵部署lnmp架構
-
分布式部署 lnmp 架構
#撰寫 ansible-playbook lnmp.yaml 劇本 [root@ansible_server~]# cat lnmp_install.yaml # author # 任務1:安裝nginx - hosts: 10.0.0.5 remote_user: root tasks: - name: 修改主機名,判斷、安裝/啟動nginx shell: hostnamectl set-hostname Hosts-01 - name: install yum_package yum: name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++ state=installed - name: echo -e "\033[44;37m 判斷是否存在nginx安裝目錄 \033[0m" file: path=/usr/local/nginx state=directory notify: - nginx start - nginx install handlers: - name: nginx install shell: cd /opt/; rm -rf nginx-1.20.1.tar.gz; wget http://nginx.org/download/nginx-1.20.1.tar.gz; tar xf nginx-1.20.1.tar.gz -C /usr/local/; useradd -M -s /sbin/nologin nginx; mkdir -p /var/log/nginx;cd /usr/local/nginx-1.20.1; ./configure --user=nginx --prefix=/usr/local/nginx --with-http_ssl_module --with-http_realip_module --with-http_image_filter_module --with-http_gunzip_module --with-http_gzip_static_module --with-http_stub_status_module --http-log-path=/var/log/nginx/access.log --error-log-path=/var/log/nginx/error.log --without-http_rewrite_module; make && make install; /usr/local/nginx/sbin/nginx -t; - name: nginx start shell: /usr/local/nginx/sbin/nginx # 任務2 安裝mysql - hosts: 10.0.0.6 remote_user: root tasks: - name: install yum_package yum: name=pcre-devel,openssl,openssl-devel,gd-devel,gcc,gcc-c++,ncurses-devel state=installed - name: 下載安裝包、并解壓 shell: cd /opt/; tar -xf mysql-5.1.63.tar.gz -C /usr/local/ - name: 開始編譯安裝 shell: cd /usr/local/mysql-5.1.63/; ./configure --prefix=/usr/local/mysql --enable-assembler; make -j8&&make -j8 install; \cp /usr/local/mysql/share/mysql/my-medium.cnf /etc/my.cnf; \cp /usr/local/mysql/share/mysql/mysql.server /etc/rc.d/init.d/mysqld; - name: 配置啟動/重啟 shell: chkconfig --add mysqld;chkconfig --level 35 mysqld on; service mysqld restart # 任務3 安裝php - hosts: 10.0.0.5 remote_user: root tasks: - name: install php_package yum: name=libxml2,libxml2-devel,openssl,openssl-devel,bzip2,bzip2-devel,libcurl,libcurl-devel,libjpeg,libjpeg-devel,libpng,libpng-devel,freetype,freetype-devel,gmp,gmp-devel,libmcrypt,libmcrypt-devel,readline,readline-devel,libxslt,libxslt-devel,pcre-devel,ncurses,ncurses-devel,bison,cmake state=installed - name: 下載安裝包、并解壓 shell: cd /opt/; wget http://mirrors.sohu.com/php/php-7.2.10.tar.gz; tar xf php-7.2.10.tar.gz -C /usr/local/; cd /usr/local/php-7.2.10/; ./configure --prefix=/usr/local/php --disable-fileinfo --enable-fpm --with-config-file-path=/etc --with-config-file-scan-dir=/etc/php.d --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-xmlrpc --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --with-mcrypt=/usr/local/libmcrypt --enable-zip --enable-mysqlnd --with-mysqli=mysqlnd --with-pdo-mysql=mysqlnd --with-mysql-sock=/var/lib/mysql/mysql.sock --without-pear --enable-bcmath; make && make install; ln -s /usr/local/php/bin/* /usr/local/bin/; \cp php.ini-production /etc/php.ini; \cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf; \cp /usr/local/php/etc/php-fpm.d/www.conf.default /usr/local/php/etc/php-fpm.d/www.conf; \cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm; chmod +x /etc/init.d/php-fpm; chkconfig --add php-fpm - name: 啟動php服務 shell: systemctl start php-fpm -

5.1.2 整合 lnmp 架構、進行測驗
-
修改nginx 組態檔
# 修改 /usr/local/nginx/conf/nginx.conf 檔案 # 1.將user 改為 nginx # 2.添加一下內容 location / { root html; index index.php index.html index.htm; } location ~ \.php$ { root html; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME /usr/local/nginx/html$fastcgi_script_name; include fastcgi_params; } # 設定php 網站檔案 vim /usr/local/nginx/html/index.php [root@hosts-01/usr/local/nginx/html]# cat index.php <?php phpinfo(); ?> [root@hosts-01/usr/local/nginx/html]# # 重啟nginx 服務 /usr/local/nginx/sbin/nginx -

-

-
瀏覽器輸入 10.0.0.5 訪問測驗
-

六、ansible 調優
6.1 ansible 組態檔詳解
-
組態檔說明
# Ansible默認組態檔為/etc/ansible/ansible.cfg,組態檔中可以對ansible進行各項引數的調整,包括并發執行緒、用戶、模塊路徑、配置優化等,如下為Ansible.cfg常用引數詳解: [defaults] 通用默認配置段; inventory = /etc/ansible/hosts 被控端IP或者DNS串列; library = /usr/share/my_modules/ Ansible默認搜尋模塊的位置; remote_tmp = $HOME/.ansible/tmp Ansible遠程執行臨時檔案; pattern = * 對所有主機通信; forks = 5 并行行程數; poll_interval = 15 回頻率或輪訓間隔時間; sudo_user = root sudo遠程執行用戶名; ask_sudo_pass = True 使用sudo,是否需要輸入密碼; ask_pass = True 是否需要輸入密碼; transport = smart 通信機制; remote_port = 22 遠程SSH埠; module_lang = C 模塊和系統之間通信的語言; gathering = implicit 控制默認facts收集(遠程系統變數); roles_path= /etc/ansible/roles 用于playbook搜索Ansible roles; host_key_checking = False 檢查遠程主機密鑰; #sudo_exe = sudo sudo遠程執行命令; #sudo_flags = -H 傳遞sudo之外的引數; timeout = 10 SSH超時時間; remote_user = root 遠程登陸用戶名; log_path = /var/log/ansible.log 日志檔案存放路徑; module_name = command Ansible命令執行默認的模塊; #executable = /bin/sh 執行的Shell環境,用戶Shell模塊; #hash_behaviour = replace 特定的優先級覆寫變數; #jinja2_extensions 允許開啟Jinja2拓展模塊; #private_key_file = /path/to/file 私鑰檔案存盤位置; #display_skipped_hosts = True 顯示任何跳過任務的狀態; #system_warnings = True 禁用系統運行ansible潛在問題警告; #deprecation_warnings = True Playbook輸出禁用“不建議使用”警告; #command_warnings = False command模塊Ansible默認發出警告; #nocolor = 1 輸出帶上顏色區別,開啟/關閉:0/1; pipelining = False 開啟pipe SSH通道優化; [accelerate] accelerate快取加速, accelerate_port = 5099 accelerate_timeout = 30 accelerate_connect_timeout = 5.0 accelerate_daemon_timeout = 30 accelerate_multi_key = yes
6.2 ansible 常見優化介紹
6.1.1 關閉ssh 密鑰檢測
-
關閉ssh 檢測密鑰
# 在ansible 組態檔中加入以下代碼: host_key_checking = False
6.1.2 openssh 連接優化
-
關閉DNS決議
# 使用OpenSSH服務時,默認服務器端組態檔UseDNS=YES狀態,該選項會導致服務器根據客戶端的IP地址進行DNS PTR反向決議,得到客戶端的主機名,然后根據獲取到的主機名進行DNS正向A記錄查詢,并驗證該IP是否與原始IP一致,關閉DNS決議 sed -i '/^GSSAPI/s/yes/no/g;/UseDNS/d;/Protocol/aUseDNS no' /etc/ssh/sshd_config /etc/init.d/sshd restart
6.1.3 加速 ansible 優化
-
SSH pipelining加速Ansible
# SSH pipelining是一個加速 Ansible 執行速度的簡單方法,SSH pipelining 默認是關閉的,關閉是為了兼容不同的 sudo 配置,主要是requiretty 選項, 如果不使用Sudo建議開啟該選項,打開此選項可以減少Ansible 執行沒有檔案傳輸時,SSH在被控機器上執行任務的連接數,使用Sudo操作的時候, 必須在所有被管理的主機上將組態檔/etc/sudoers中requiretty選項禁用, sed -i '/^pipelining/s/False/True/g' /etc/ansible/ansible.cfg
總結
在里小編想說,其實ansible 不難,工具比較簡單,其中的shell 模塊是萬能的模塊,所以,如果其他模塊掌握不好,那就用shell 去實作,小撰寫這篇文章花費了3天左右,通過構思、實踐,完整的走了一遍,如果當你讀到這篇文章后,請認真看完,因為這是運維必備的技能,
日常運維中,還有很多細節需要注意,這里不做過多描述,當然這里介紹的并不是非常全,往后的學習中,還是要多去查看官網的說明檔案,上面介紹的最詳細,最權威,學習是一個漫長的程序,如果你想從事運維行業,能參加一個好點的培訓機構最好,如果經濟有限,不想花錢,也可以自學,買一本好點的書籍,看一套完整的視頻,然后做實驗,反復去練習,同時如果對我的云計算專欄感興趣,也可以訂閱我的專欄,進行學習,
以夢為馬,不負韶華,流年笑擲,未來可期!
????????????????—黃昏
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/302236.html
標籤:其他


