一、Content Organization
推薦使用roles,roles是極好的。
1.1 目錄結(jié)構(gòu)
頂層目錄結(jié)構(gòu)應(yīng)當(dāng)包括下列文件和目錄:
production # inventory file for production servers 關(guān)于生產(chǎn)環(huán)境服務(wù)器的清單文件
stage # inventory file for stage environment 關(guān)于 stage 環(huán)境的清單文件
group_vars/
group1 # here we assign variables to particular groups 這里我們給特定的組賦值
group2 # ""
host_vars/
hostname1 # if systems need specific variables, put them here 如果系統(tǒng)需要特定的變量,把它們放置在這里.
hostname2 # ""
library/ # if any custom modules, put them here (optional) 如果有自定義的模塊,放在這里(可選)
filter_plugins/ # if any custom filter plugins, put them here (optional) 如果有自定義的過濾插件,放在這里(可選)
site.yml # master playbook 主 playbook
webservers.yml # playbook for webserver tier Web 服務(wù)器的 playbook
dbservers.yml # playbook for dbserver tier 數(shù)據(jù)庫服務(wù)器的 playbook
roles/
common/ # this hierarchy represents a "role" 這里的結(jié)構(gòu)代表了一個(gè) "role"
tasks/ #
main.yml # <-- tasks file can include smaller files if warranted
handlers/ #
main.yml # <-- handlers file
templates/ # <-- files for use with the template resource
ntp.conf.j2 # <------- templates end in .j2
files/ #
bar.txt # <-- files for use with the copy resource
foo.sh # <-- script files for use with the script resource
vars/ #
main.yml # <-- variables associated with this role
defaults/ #
main.yml # <-- default lower priority variables for this role
meta/ #
main.yml # <-- role dependencies
webtier/ # same kind of structure as "common" was above, done for the webtier role
monitoring/ # ""
fooapp/ # ""
1.2 使用Role的劇本
---
# file: webservers.yml
- hosts: webservers
roles:
- common
- webtier
1.3 Task and Handler Organization For A Role
接下來的示例任務(wù)文件展示了一個(gè) role 是如何工作的.我們這里的普通 role 僅僅用來配置 NTP,但是如果我們想的話,它可以做更多:
---
# file: roles/common/tasks/main.yml
- name: be sure ntp is installed
yum: pkg=ntp state=installed
tags: ntp
- name: be sure ntp is configured
template: src=ntp.conf.j2 dest=/etc/ntp.conf
notify:
- restart ntpd
tags: ntp
- name: be sure ntpd is running and enabled
service: name=ntpd state=running enabled=yes
tags: ntp
這是個(gè)處理文件樣例.作為一種審核,它只有當(dāng)特定的任務(wù)報(bào)告發(fā)生變化時(shí)會(huì)被觸發(fā),并在每個(gè) play 結(jié)束時(shí)運(yùn)行:
---
# file: roles/common/handlers/main.yml
- name: restart ntpd
service: name=ntpd state=restarted
1.4 What This Organization Enables(Examples)
我們?cè)谇拔姆窒砹宋覀兓A(chǔ)的組織結(jié)構(gòu).
那這種結(jié)構(gòu)適用于何種應(yīng)用場(chǎng)景? 很多昌阿!若我想重新配置整個(gè)基礎(chǔ)設(shè)施,如此即可:
ansible-playbook -i production site.yml
那只重新配置所有的 NTP 呢?太容易了.:
ansible-playbook -i production site.yml --tags ntp
只重新配置我的 Web 服務(wù)器呢恳邀?:
ansible-playbook -i production webservers.yml
只重新配置我在波士頓的 Web服務(wù)器呢?:
ansible-playbook -i production webservers.yml --limit boston
二、深入探討
2.1 異步操作和輪詢
默認(rèn)情況下playbook中的任務(wù)執(zhí)行時(shí)會(huì)一直保持連接,直到該任務(wù)在每個(gè)節(jié)點(diǎn)都執(zhí)行完畢.有時(shí)這是不必要的,比如有些操作運(yùn)行時(shí)間比SSH超時(shí)時(shí)間還要長.
解決該問題最簡單的方式是一起執(zhí)行它們,然后輪詢直到任務(wù)執(zhí)行完畢.
你也可以對(duì)執(zhí)行時(shí)間非常長(有可能遭遇超時(shí))的操作使用異步模式.
為了異步啟動(dòng)一個(gè)任務(wù),可以指定其最大超時(shí)時(shí)間以及輪詢其狀態(tài)的頻率.如果你沒有為 poll 指定值,那么默認(rèn)的輪詢頻率是10秒鐘:
---
- hosts: all
remote_user: root
tasks:
- name: simulate long running op (15 sec), wait for up to 45 sec, poll every 5 sec
command: /bin/sleep 15
async: 45
poll: 5
注:async 并沒有默認(rèn)值,如果你沒有指定 async 關(guān)鍵字,那么任務(wù)會(huì)以同步的方式運(yùn)行,這是Ansible的默認(rèn)行為.
另外,如果你不需要等待任務(wù)執(zhí)行完畢,你可以指定 poll 值為0而啟用 “啟動(dòng)并忽略”
---
- hosts: all
remote_user: root
tasks:
- name: simulate long running op, allow to run for 45 sec, fire and forget
command: /bin/sleep 15
async: 45
poll: 0
注:對(duì)于要求排它鎖的操作,如果你需要在其之后對(duì)同一資源執(zhí)行其它任務(wù),那么你不應(yīng)該對(duì)該操作使用”啟動(dòng)并忽略”.比如yum事務(wù).
注:
--forks
參數(shù)值過大會(huì)更快的觸發(fā)異步任務(wù).也會(huì)加快輪詢的效率.
當(dāng)你想對(duì) “啟動(dòng)并忽略” 做個(gè)變種,改為”啟動(dòng)并忽略,稍后再檢查”,你可以使用以下方式執(zhí)行任務(wù):
---
# Requires ansible 1.8+
- name: 'YUM - fire and forget task'
yum: name=docker-io state=installed
async: 1000
poll: 0
register: yum_sleeper
- name: 'YUM - check on fire and forget task'
async_status: jid={{ yum_sleeper.ansible_job_id }}
register: job_result
until: job_result.finished
retries: 30
注:如果
async:
值太小,可能會(huì)導(dǎo)致 “稍后檢查” 任務(wù)執(zhí)行失敗,因?yàn)?async_status::
的臨時(shí)狀態(tài)文件還未被寫入信息,而”稍后檢查”任務(wù)就試圖讀取此文件.
2.2 Check Mode (“Dry Run”)
當(dāng)以 --check
參數(shù)來運(yùn)行 ansible-playbook 時(shí),將不會(huì)對(duì)遠(yuǎn)程的系統(tǒng)作出任何更改.相對(duì)的,任何帶有檢測(cè)功能的模塊(這幾乎包含了所有的主要核心模塊,但這不要求所有的模塊都需支持.) 只要支持 ‘檢測(cè)模式’ 將會(huì)報(bào)告它們會(huì)做出什么改變而不是直接進(jìn)行改變.其他不支持檢測(cè)模式的模塊將既不響應(yīng)也不提出相應(yīng)的報(bào)告.
檢測(cè)模式只是一種模擬.如果你的playbook是以先前命令的執(zhí)行結(jié)果作為條件的話,那它可能對(duì)你就沒有什么大用處了. 但是對(duì)于基于一次一節(jié)點(diǎn)的基礎(chǔ)配置管理的使用情形來說是很有用.
ansible-playbook foo.yml --check
2.3 Showing Differences with --diff
New in version 1.1: 對(duì) ansible-playbook 來說 --diff
選項(xiàng)與 --check
(詳情參下)配合使用效果奇佳,不過它也可以單獨(dú)使用.當(dāng)提供了相應(yīng)的標(biāo)識(shí)后,當(dāng)遠(yuǎn)程系統(tǒng)上任何模板文件的變化時(shí),ansible-playbook CLI 將會(huì)報(bào)告文件上任何文本的變化 (或者,如果使用了 --check
參數(shù),將報(bào)告會(huì)發(fā)生的變化.).因?yàn)?diff 特性會(huì)產(chǎn)生大量的輸出結(jié)果,所以它在一次檢測(cè)一個(gè)主機(jī)時(shí)使用為佳,如:
ansible-playbook foo.yml --check --diff --limit foo.example.com