****前言****
前段時間總結(jié)了ansible的基本配置與使用傀履,那么說到了ansible的話呢捡多,playbook是必不可少的撑帖,playbook在我看來就是一個花花公子-playboy!
****playbook的介紹****
Playbooks are a completely different way to use ansible than in adhoc task execution mode, and are particularly powerful.
顧名思義肿孵,playbooks是一個"劇本",不同于ansible的使用方式唠粥,它是按照編排的任務(wù)智能地執(zhí)行,并且非常強大停做!
Simply put, playbooks are the basis for a really simple configuration management and multi-machine deployment system, unlike any that already exist, and one that is very well suited to deploying complex applications.
簡單地來說呢晤愧,playbooks適合簡單的配置管理以及多服務(wù)器機器的管理,同時還可以處理部署復(fù)雜的應(yīng)用雅宾。
要是服務(wù)器不僅僅復(fù)雜养涮,數(shù)量上還是成千上萬的呢,還是推薦使用重量級又古老文明的saltstack眉抬。
傳送ansible文檔詳解
自動化運維之a(chǎn)nsible
****playbook基礎(chǔ)語法****
playbook使用了YAML格式的語法贯吓,該語法還是相當(dāng)簡單的,可以體驗出程序構(gòu)造或執(zhí)行的過程蜀变。那么還是具體查看簡單的一個實例悄谐!
在docker服務(wù)器組使用root用戶執(zhí)行更新源命令,如下:
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "初始化更新源列表"
command: apt-get update
語法注意:"-"以及":"后面都需要一個空格库北。
語法已經(jīng)主意了吧~~~該注意該注意的內(nèi)容了爬舰,從上面的實例額可以看出有兩個必須的屬性,那就是服務(wù)器主機hosts以及遠程用戶remote_user寒瓦,有了它們就可以干點其它事了tasks情屹,自然而然在playbook執(zhí)行任務(wù)是需要通過模塊的來操控的。
使用命令檢查yaml的語法
ansible-playbook main.yml --syntax-check
****playbook模塊化task****
(⊙v⊙)嗯~~~模塊化的理論就不一一說明了杂腰,實踐通過時間來領(lǐng)悟理論精華垃你。該是動手的時候了!
- command
# 在docker服務(wù)器組執(zhí)行一條命令
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "初始化更新源列表"
command: apt-get update
說明:name的屬性就是一個任務(wù)的昵稱喂很,自定義惜颇。
- shell
# 在docker服務(wù)器組執(zhí)行shell命令
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "刪除/home/alic/demo.sh"
shell: rm -f /home/alic/demo.sh
- script
# 在docker服務(wù)器組執(zhí)行控制節(jié)點本地的shell腳本
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "被控節(jié)點執(zhí)行控制節(jié)點的shell腳本"
script: ../scripts/alic.sh
- copy
# 將控制節(jié)點的文件上傳到docker服務(wù)器【被控節(jié)點】上
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "控節(jié)點文件cp到被控制節(jié)點服務(wù)器"
copy:
src=/etc/ansible/hosts
dest=/etc/ansible/hosts
owner=root
group=root
mode=0644
注意:src代表控制節(jié)點路徑,dest代表被控節(jié)點路徑少辣,其它的為可選項目凌摄,顧名思義。
- yum
# 在docker用戶組以root用戶安裝vim編輯器
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "我正在在centOS安裝vim呢~~~"
yum: name=vim state=latest
說明:name為安裝某某的名稱漓帅,state則為安裝的版本锨亏,yum該模塊僅僅適合contOS相似的發(fā)行版,對于ubuntu呢忙干,還是推薦使用原生的bash咯~~~
- service
# 在docker用戶組以root用戶重啟apache2
- hosts: docker
remote_user: root
gather_facts: True
tasks:
- name: "我正在啟動apache2服務(wù)器~~~"
service: name=apache2 state=restarted
說明:service的狀態(tài)與我們平常使用的多了-ed stsrted stoped restarted
- notify 與 handlers
使用一句英文更好地闡述兩者的關(guān)系
The things listed in the notify
section of a task are called handlers.
還是說一下中文吧~~~
notify是一個通知屯伞,實質(zhì)上也是一個任務(wù),不同的是使用handlers定義的任務(wù)豪直,handlers里面定義的任務(wù)相當(dāng)于定義方法劣摇,提高復(fù)用性!
- hosts: docker
remote_user: root
tasks:
- name: "test notify"
shell: ls
notify:
- restart apache2
handlers:
- name: restart apache2
service: name=apache2 state=restarted
- vars
先說明一下弓乙,很明顯vars模塊用戶聲明變量
# vars 變量的定義與使用
- hosts: docker
remote_user: root
vars:
config_path: "/root/application/sise.conf"
tasks:
- name: "test notify"
command: touch {{config_path}}
然而說到變量還可以這樣使用:在yml使用變量末融,在執(zhí)行playbook命令是額外賦值,注意記得加上引號 "{{var}}"
ansible-playbook main.yml --extra-vars hosts=docker
hosts: "{{hosts}}"
remote_user: root
vars:
- dir_name: "public"
tasks:
- name: create dir
shell: mkdir {{dir_name}}
- when
(⊙o⊙)嗯~~~暇韧,when在運維時是一個重點勾习,不同Linux的發(fā)行版呢,有些命令就不一樣懈玻,但都是了解到了條件即可switch地處理巧婶。好比如yum只有centOS,RedHat等發(fā)行版才具有的包管理命令工具
重點:when主要用于處理不同的操作系統(tǒng)與處理邏輯上。
- hosts: docker
remote_user: root
tasks:
- name: "我正在紅帽子安裝vim呢~~~"
yum: name=vim state=latest
when: ansible_os_family == "RedHat"
- with_items
# 便利迭代 + when
- hosts: docker
remote_user: root
tasks:
- command: echo {{ item }}
with_items: [ 0, 2, 4, 6, 8, 10 ]
when: item > 5
****Tips and Tricks For Ansible-book Command****
1 查看任務(wù)所指定的host列表
$ ansible-playbook main.yml --list-hosts
2 If you need to specify a password to sudo, run ansible-playbook
with --ask-pass
or when using the old sudo syntax --ask-sudo-pass
即當(dāng)你使用普通用戶執(zhí)行命令需要輸入密碼時可使用
$ ansible-playbook main.ym --ask-sudo-pass --ask-pass
3 獲取docker server-group主機的所有基本信息
既然可以獲取主機信息 當(dāng)時用when的時候艺栈,該命令就其很大作用了英岭!
$ ansible docker -m setup
4 直接通過ansible-playbook命令來指定主機
$ ansible-playbook playbook.yml --limit docker
嘿嘿~~~這個野蠻好的( ⊙o⊙ )哇,有時候我想在某臺服務(wù)器搭建nginx的話湿右,task只寫好模板诅妹,需要的服務(wù)器就在執(zhí)行命令指定host或服務(wù)器組即可!
****Demo實踐目錄樹****
****價值源于技術(shù)毅人,貢獻源于分享****