網(wǎng)頁制作基礎(chǔ)教程第二版seo查詢 站長之家
目錄
- 一.playbook介紹
- 二.playbook格式
- 1.書寫格式
- 2.notify介紹
一.playbook介紹
playbook 是 ansible 用于配置,部署,和管理被控節(jié)點的劇本。通過 playbook 的詳細(xì)描述,執(zhí)行其中的一系列 tasks ,可以讓遠(yuǎn)端主機達(dá)到預(yù)期的狀態(tài)。playbook 就像 Ansible 控制器給被控節(jié)點列出的的一系列 to-do-list ,而被控節(jié)點必須要完成。也可以這么理解,playbook 字面意思,即劇本,現(xiàn)實中由演員按照劇本表演,在 Ansible 中,這次由計算機進(jìn)行表演,由計算機安裝,部署應(yīng)用,提供對外服務(wù),以及組織計算機處理各種各樣的事情
二.playbook格式
1.書寫格式
- 文件的第一行應(yīng)該以 “—” (三個連字符)開始,表明 YMAL 文件的開始。
- 在同一行中,# 之后的內(nèi)容表示注釋,類似于 shell,python 和 ruby。
- YMAL 中的列表元素以 ”-” 開頭然后緊跟著一個空格,后面為元素內(nèi)容。
- 同一個列表中的元素應(yīng)該保持相同的縮進(jìn)。否則會被當(dāng)做錯誤處理。
- play 中 hosts,variables,roles,tasks 等對象的表示方法都是鍵值中間以 “:”
分隔表示,“:” 后面還要增加一個空格
舉例 安裝掛載nfs服務(wù)
[root@tdm1 playbook]# cat nfs.yml
---
- hosts: webremote_user: roottasks: - name: install nfsyum: name=rpcbind,nfs-utils state=present- name: nfs configure filecopy: src=./export.j2 dest=/etc/exports backup=yes- name: mkdir share dirfile: path=/data state=directory owner=nfsnobody group=nfsnobody- name: start rpcbindservice: name=rpcbind state=started enabled=yes - name: start nfsservice: name=nfs state=started enabled=yes - name: mount localmount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mounted
文件名稱應(yīng)該以.yml結(jié)尾
hosts :使用 hosts 指示使用哪個主機或主機組來運行下面的 tasks ,每個 playbook 都必須指定 hosts ,hosts 也可以使用通配符格式。主機或主機組在 inventory 清單中指定,可以使用系統(tǒng)默認(rèn)的 /etc/ansible/hosts,也可以自己編輯,在運行的時候加上 -i 選項,指定清單的位置即可。在運行清單文件的時候,–list-hosts 選項會顯示那些主機將會參與執(zhí)行 task 的過程中。
remote_user:指定遠(yuǎn)端主機中的哪個用戶來登錄遠(yuǎn)端系統(tǒng),在遠(yuǎn)端系統(tǒng)執(zhí)行 task 的用戶,可以任意指定,也可以使用 sudo,但是用戶必須要有執(zhí)行相應(yīng) task 的權(quán)限。
tasks:指定遠(yuǎn)端主機將要執(zhí)行的一系列動作。tasks 的核心為 ansible 的模塊,前面已經(jīng)提到模塊的用法。tasks 包含 name 和要執(zhí)行的模塊,name 是可選的,只是為了便于用戶閱讀,不過還是建議加上去,模塊是必須的,同時也要給予模塊相應(yīng)的參數(shù)。
使用ansible-playbook運行playbook文件,得到以下輸出信息,輸出內(nèi)容為json格式,由不同顏色組成
綠色代表執(zhí)行成功,系統(tǒng)保持原樣
黃色代表系統(tǒng)狀態(tài)發(fā)生改變
紅色代表失敗,顯示錯誤輸出
執(zhí)行有三個步驟:
1.收集facts
2. 執(zhí)行tasks
3. 報告結(jié)果
2.notify介紹
Ansible提供了notify指令和handlers功能。如果在某個task中定義了notify指令,當(dāng)Ansible在監(jiān)控到該任務(wù) changed=1時,會觸發(fā)該notify指令所定義的handler,然后去執(zhí)行handler。所謂handler,其實就是task,無論在寫法上還是作用上它和task都沒有區(qū)別,唯一的區(qū)別在于hander是被觸發(fā)而被動執(zhí)行的,不像普通task一樣會按流程正常執(zhí)行
測試1
當(dāng)檢測到nfs的配置發(fā)生變化是,會重啟nfs服務(wù)和重新掛載
notify和handlers中定義的名稱必須一致
[root@tdm1 playbook]# cat nfs.yml
---
- hosts: webtasks: - name: install nfsyum: name=rpcbind,nfs-utils state=present- name: nfs configure filecopy: src=./export.j2 dest=/etc/exports backup=yesnotify: restart nfs #當(dāng)export.j2文件發(fā)生變化,就會由handlers來執(zhí)行。- name: mkdir share dirfile: path=/data state=directory owner=nfsnobody group=nfsnobody- name: start rpcbindservice: name=rpcbind state=started enabled=yes - name: start nfsservice: name=nfs state=started enabled=yes - name: mount localmount: src=47.93.98.117:/data path=/mnt fstype=nfs state=mountednotify: client remounthandlers: - name: restart nfs #名稱和notify中的一致service: name=nfs state=restarted - name: client remountservice: src=47.93.98.117:/data path=/mnt fstype=nfs state=remounted
#修改export.j2的內(nèi)容
vim export.j2
/data 47.93.98.0/24(rw,all_squash)
執(zhí)行劇本,觀察
#查看文件是否更改
[root@tdm1 playbook]# ansible web -m shell -a 'cat /etc/exports'
47.93.98.117 | CHANGED | rc=0 >>
/data 47.93.98.0/24(rw,all_squash)
測試2
修改nginx的文件,檢測到文件被修改,handlers下面任務(wù)會被執(zhí)行
[root@tdm1 playbook]# cat nginx.yml
---
- hosts: webtasks: - name: install nginx yum:name: nginxstate: installed- name: index filecopy:content: "This is ansible test"dest: /usr/share/nginx/html/index.html- name: nginx configure filecopy:src: ./nginx.conf.j2dest: /etc/nginx/conf.d/default.confbackup: yesnotify: restart nginx #文件被修改,重啟nginx- name: start nginxservice: name: nginxstate: startedhandlers:- name: restart nginx #重啟nginxservice: name: nginxstate: restarted
#修改nginx.conf.j2的文件
vim nginx.conf.j2
server {listen 81;server_name localhost;location / {root /usr/share/nginx/html;index index.html index.htm;}
}
參考:https://blog.csdn.net/u012562943/category_6298590.html