簡介
playbook是一個非常簡單的配置管理和多主機部署系統。可作為一個適合部署復雜應用程序的基礎。
playbook可以定制配置,可以按指定的操作步驟有序執行,支持同步和異步方式。
playbook是通過YAML格式來進行描述定義的,可實現多台主機應用的部署,對不同分組的主機執行特定指令步驟。
playbook通過示例展示其用法
定制一個簡單的Nginx軟件包管理,內容包括安裝、配置模板、狀態管理等。
配置文件:nginx.yml
--- - hosts: webservers #hosts參數作用:定義操作的對象,本例操作對象為webservers組 vars: #vars參數作用:定義變量(配置模板時會用到),作用域只限於webservers組 worker_processes: 4 num_cpus: 4 max_open_file: 65506 root: /data remote_user: root #指定遠程操作的用戶名,默認是root,支持sudo運行,通過添加sudo:yes即可 tasks: #定義任務列表(自上而下順序執行) - name: ensure nginx is at the latest version #每個事務都可以定義一個name標簽,好處是增強可讀性,便於觀察結果輸出時了解運行的位置 yum: pkg=nginx state=latest #yum安裝最新版本的nginx - name: write the nginx config file template: src=/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf #根據模板配置nginx配置文件,src為主控端模板路徑,dest為被控端nginx配置文件路徑 notify: - restart nginx - name: ensure nginx is running service: name=nginx state=started #啟動nginx handlers: #通知處理程序(必須要有notify觸發才會執行),根據notify選擇handlers中對應的name標簽,從而進行相應操作。如notify中是restart nginx,則handlers中的name標簽內容也是restart nginx,才能執行 - name: restart nginx service: name=nginx state=restarted
模板:nginx2.conf
user nginx; worker_processes {{ worker_prcesses }}; {% if num_cpus == 2 %} worker_cpu_affinity 01 10; {% elif num_cpus == 4 %} worker_cpu_affinity 1000 0100 0010 0001; {% elif num_cpus >= 8 %} worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; {% else %} worker_cpu_affinity 1000 0100 0010 0001; {% endif %} worker_flimit_notifile {{ max_open_file }}; ... ...
執行playbook
格式:
ansible-playbook playbook.yml(playbook文件,可自定義名稱) [參數]
例:
ansible-playbook /home/test/ansible/playbooks/nginx.yml -f 10 #啟用10個並行進程數執行playbook(nginx.yml)
常用參數:
-u REMOTE_USER #手工指定playbook的系統用戶 --syntax-check #檢查playbook的語法 --list-hosts playbook #匹配到的主機列表 -T TIMEOUT #定義playbook執行超時時間 --step #以單任務分步驟運行,方便做每一步確認工作 --help #幫助信息
playbook角色與包含聲明
包含
當playbook文件非常大時,想要復用某些功能是就會顯得相當吃力,Ansible支持寫playbook文件時拆分成多個文件,通過包含(include)的形式進行引用。
例:
功能(復用)文件:tasks/fool.yml
--- #possibly saved as tasks/foo.yml - name: placeholder foo command: /bin/foo - name: placeholder bar command: /bin/bar
使用的playbook文件:playbook.yml
tasks: - include: tasks/foo.yml #通過include來引用復用的功能
角色
角色:Ansible定制好的一種標准規范,以不同級別目錄層次及文件對角色、變量、任務、處理程序等進行拆分,為后續功能擴展、可維護性打下基礎。
例:
以上面的nginx.yml為例進行拆分,結構如下:
說明:
hosts
#自定義主機,非必選項,默認將引用/etc/ansible/hosts的參數,要引用自定義hosts,需要通過-i file參數來實現,如:ansible-playbook -i hosts
[webservers]
192.168.1.111
192.168.1.112
group_vars
#定義組變量目錄,目錄中的文件名要與組名保持一致,組變量文件定義的變量作用域只在該組內,不能作用到其他組
【group_vars/all】 #代表所有主機
--- #Variables listed here are applicable to all host groups ntpserver: ntp.sjtu.edu.cn
【group_vars/webservers】 #webservers組
--- worker_processes: 4 num_cpus: 4 max_open_file: 66535 root: /data
site.yml
#全局配置文件,下面內容引用了兩個角色塊,角色的應用范圍及實現功能都不一樣
--- - name: apply common configuration to all nodes hosts: all roles: - common #對應目錄為:nginx/roles/common - name: configure and deploy the webservers and application code hosts: webservers roles: - web #對應目錄為:nginx/roles/web
roles
#角色目錄,通常每個角色對應着一個特定的功能服務
【roles/common】
handlers/main.yml #處理程序文件
---
- name: restart ntp
service: name=ntp state=restarted
tasks/main.yml #任務列表文件
--- - name: Install ntp yum: name=ntp state=present - name: Configure ntp file template: src=ntp.conf.j2 dest=/etc/ntp.conf #引用模板無需寫路徑,默認在上級的templates目錄中查找 notify: restart ntp - name: Start the ntp service service: name=ntp state=started enabled=true - name: test to see if selinux is running command: getenforce register: sestatus changed_when: false
templates/ngp.conf.j2 #模板
driftfile /var/lib/ntp/drift restrict 127.0.0.1 restrict -6 ::1 server {{ ntpserver }} #此處ntpserver引用vars/main.yml中定義的ntpserver變量 includefile /etc/ntp/rypto/pw keys /etc/ntp/keys
vars/main.yml #變量配置文件
--- #Variable listed here are applicable to all host groups ntpserver: 210.72.145.44
【roles/web】
handlers/main.yml #處理程序文件
---
- name: restart nginx
service: name=nginx state=restarted
tasks/main.yml #任務列表文件
--- - name: ensure nginx is at the latest version #每個事務都可以定義一個name標簽,好處是增強可讀性,便於觀察結果輸出時了解運行的位置 yum: pkg=nginx state=latest #yum安裝最新版本的nginx - name: write the nginx config file template: src=/home/test/ansible/nginx/nginx2.conf dest=/etc/nginx/nginx.conf #根據模板配置nginx配置文件,src為主控端模板路徑,dest為被控端nginx配置文件路徑 notify: - restart nginx - name: ensure nginx is running service: name=nginx state=started #啟動nginx
templates/nginx2.conf #模板
user nginx; worker_processes {{ worker_prcesses }}; {% if num_cpus == 2 %} worker_cpu_affinity 01 10; {% elif num_cpus == 4 %} worker_cpu_affinity 1000 0100 0010 0001; {% elif num_cpus >= 8 %} worker_cpu_affinity 00000001 00000010 00000100 00001000 00010000 00100000 01000000 10000000; {% else %} worker_cpu_affinity 1000 0100 0010 0001; {% endif %} worker_flimit_notifile {{ max_open_file }}; ... ...
運行角色
ansible-playbook -i hosts site.yml -f 10 #啟用10個並行進程數執行playbook。hosts文件通過-i指向自定義hosts,playbook配置文件為site.yml
參考資料:
根據劉天斯《Python自動化運維技術與最佳實踐》整理