簡述
學會寫playbook劇本,使用ansible-playbook執行可以實現的就是自動化運維,playbook由一個或多個模塊組成,完成統一的目的,實現自動化操作,
劇本編寫遵循yaml語法
配置文件的概述
playbook配置文件使用YAML語法,具有簡潔 明瞭,結構清晰等特點,playbook配置文件類似於shell腳本,是一個YAML格式的文件,用於保存對特定的需求的人物列表。YAML的文件擴展名通常是.yaml
或.yml
yaml的三要素
縮進:兩個字符,默認的tab鍵是四個字符,所以要使用tab鍵,需要修改.vimrc
vim /root/.vimrc
添加:
set tabstop=2
保存退出
冒號:冒號後面需要
空格: 除非以冒號結尾
短橫槓: 列表項,後面跟空格
playbook的核心元素
hosts
:任務的目標主機,多個主機用冒號分隔,一般/etc/ansible/hosts分組信息
remote_user
:遠程主機 上,運行此任務的身份默認爲root
tasks
:任務,自定義的具體任務
handlers
:觸發器,可通過“notify”通知給相應的handlers進行觸發執行
roles
:角色,由tasks,handlers等所一組特定的集合
templates
:存放模板文件的目錄
vars
:存放變量的目錄
files
:存放由copy或script等模塊調用的文件
對於playbook怎末學,都看多練就是好方法,就跟shell腳本類似,靈活掌握好他能使用的模塊。
下面以安裝httpd
爲例介紹:
---
- hosts: web #針對web羣組的操作
tasks: #任務列表
- name: install httpd #任務名稱
yum: name=httpd state=latest #任務:yum安裝最新httpd
- name: httpd config
copy: src=/etc/httpd/conf/httpd.conf #任務:複製配置文件,其中端口改成了8080 dest=/etc/httpd/conf/httpd.conf
notify: restart httpd #設置觸發條件,即完成該劇本任務後,調用"restart httpd "的觸發器
- name: start httpd
service: name=httpd state=started #保證能開啓服務
handlers: #觸發器
- name: restart httpd
service: name=httpd state=restarted
測試劇本,不會發生改變:
ansible-playbook -C 絕對路徑 httpd.yaml
執行劇本:
ansible-playbook 絕對路徑 httpd.yaml
[root@localhost roles]# ansible-playbook ./httpd.yaml
PLAY [web] ***********************************************************************
TASK [Gathering Facts] ***********************************************************
ok: [node1]
TASK [install httpd] *************************************************************
ok: [node1]
TASK [httpd config] **************************************************************
changed: [node1]
TASK [start httpd] ***************************************************************
changed: [node1]
RUNNING HANDLER [restart httpd] **************************************************
changed: [node1]
PLAY RECAP ***********************************************************************
node1 : ok=5 changed=3 unreachable=0 failed=0 skirescued=0 ignored=0
部署nginx例子:
分開裏面的核心組件進行編寫playbook,更通俗易懂每個組件是幹嘛的;將多種不同的tasks的文件集中存儲在某個目錄下,這個目錄就是角色,角色存放在/etc/ansible/roles
目錄,roles可以有很多子目錄
,每個子目錄對應一個角色,每個角色有自己的目錄結構
。
在roles目錄下建立nginx目錄和自己的目錄結構,如下圖:
編寫主劇本tasks/main.yaml
---
- name: install nginx packge
yum: name={{ item }} state=latest #定義一個item的變量,yum要安裝的東西
with_items:
- epel-release
- nginx
- name: copy index.html
copy: src=index.html dest=/usr/share/nginx/html/
- name: copy nginx.conf tempalte
template: src=nginx.conf.j2 dest=/etc/nginx/nginx.conf #調用nginx.conf.j2這個模板
notify: restart nginx #任務執行完成後開始執行restart nginx的觸發器
- name: make sure nginx service running
service: name=nginx state=started enabled=yes
準備模板文件
[root@localhost roles]# cp /etc/nginx/nginx.conf /etc/ansible/roles/nginx/templates/
[root@localhost roles]#
[root@localhost roles]# cd nginx/templates/
[root@localhost templates]# ls
nginx.conf
[root@localhost templates]# mv nginx.conf nginx.conf.j2
在模板文件添加設置變量
worker_processes {{ansible_processor_cores}};
爲系統變量,值取決於你的設備
worker_connections {{worker_connections}};
自定義變量
自定義變量需要修改/var/main.yaml文件
編輯觸發器文件
最後site.yaml就像是開關一樣,只要編輯完後執行,nginx就會安裝上來;
hosts:定義目標主機
roles:就是定義的nginx角色
以後可能會有裝有很多角色,一部分裝nginx,一部分裝 nfs ,一部分裝mysql等等,都分別建好目錄,方便區分和管理
ansible-playbook執行劇本
例:playbook配置web–nfs–rsync架構環境
搭建rsync服務器,使其web客戶端在發生變化時能快速的備份到rsync服務器,nfs服務器負責同步數據到各個web節點,保證數據的一致性
web節點搭建參考第一個例子
nfs搭建
nfs詳細學習參考博文:
https://editor.csdn.net/md/?articleId=105769837
編寫playbook劇本
nfs服務器搭建非常簡單,用不到很多組件和參數,只需把一份樣例文件copy過去就好
---
- name: install nfs-utils rpcbind
yum: name={{ item }} state=installed
with_items:
- rpcbind
- nfs-utils
- name: copy exports
copy: src=exports dest=/etc/exports
- name: create share_directory
file: path=/nfs state=directory recurse=yes owner=root group=root mode=755
- name: start rpcbind
service: name=rpcbind state=restarted
- name: start nfs
service: name=nfs state=restarted
在files目錄放置需要copy或script文件會直接調用或者使用絕對路徑指定位置
因爲Centos7的圖形化界面會默認自帶rpcbind,不需要手動下載了,
其他的web節點可以直接掛載使用,若是最小化安裝,需要安裝rpcbind才能訪問
最小化安裝的也可以使用ansible批量安裝一下:
ansible web -m shell -a 'yum -y install rpcbind nfs-utils'
開啓rpcbind,nfs服務:
ansible web -m shell -a 'systemctl start rpcbind nfs'
在web 節點查看共享目錄
web節點批量掛載共享目錄
ansible web -m shell -a 'mount 192.168.10.7:/nfs /var/www/html'
搭建rsync+inotify實時同步備份
詳細瞭解參考:
https://editor.csdn.net/md/?articleId=105788030
使用ansible搭建多臺rsync備份服務器,在web端安裝上inotify工具,會根據自己的變動向rsync服務器發送同步,結合nfs在web端的作用,實現全網備份數據。
rsync劇本結構:
check_rsync.sh腳本是web客戶端要使用的實時監測腳本
rsync劇本參考如下:
[root@localhost roles]# cat rsync/tasks/main.yaml
---
- name: install rsync
yum: name=rsync state=installed
- name: config rsync.conf
template: src=rsync.conf.j2 dest=/etc/rsync.conf
notify: restart rsync
- name: create rsync_directory
file: path=/rsync state=directory
- name: start rsync
service: name=rsyncd state=started enabled=yes
模板文件如下:
[root@localhost roles]# cat rsync/templates/rsync.conf.j2
uid = nobody
gid = nobody
port 873
address = 192.168.10.4
hosts allow = 192.168.10.0/24
max connections = 4
pid file = /var/run/rsyncd.pid
timeout = 900
dont compress = *.gz *.tgz *.zip *.z *.Z *.rpm *.deb *.bz2
[rsync]
path = /rsync
read only = yes
觸發器和調用總劇本文件
檢測沒有錯誤後
配置web端
編寫實時同步劇本
---
- hosts: web
tasks:
- name: install inotify-tools
yum: name=inotify-tools state=installed
- name: copy script
copy: src=/root/check_rsync.sh dest=/root/
- name: chmod
shell: chmod +x /root/check_rsync.sh
- name: run script
shell: sh /root/check_rsync.sh & #放到後臺運行
把在/rsync裏面的腳本拷貝到/root目錄下
腳本內容爲
#!/bin/bash
#2020年4月27日15:28:39
#Slave to master script
inotifywait -mrq -e create,delete,modify,move /var/www/html/ | while read a b c #將檢測到的結果賦值給變量aaa,當成功後開始循環do...done內容
do
rsync -avz --delete /var/www/html [email protected]:/rsync
done
執行劇本
此時,我們在web節點上的網頁根目錄/var/www/html下創建刪除文件,觀察rsync服務器的/rsync目錄和nfs服務器/nfs目錄
rsync的備份目錄
nfs的共享目錄和所有web節點目錄一致,因爲rsync的存在從而實現了全網備份,