[ansible-playbook]4 持續集成環境之分布式部署利器 ansible play學習
3 ansible-play講的中太少了,今天稍微深入學習一點
預計閱讀時間:15分鐘
一: 安裝部署
參考 http://getansible.com/begin/an_zhuang_ansile
二:常用模塊
a. service 用於啟動檢查服務
b. file 用於文件刪除 鏈接 創建
c.shell 用於執行腳本(不推薦,因為shell操作有時並非冪等,而且不方便檢查執行結果)
d.copy 用於拷貝文件
e. vars+ template 用於根據模板文件基於變量創建配置文件
樣例:
test.yml
--- #Task1 - hosts: web vars: domain:0731 remote_user: root tasks: - name: test connection ping: - name: httpd check service: name=sshd state=started - name: remove foo.conf file: dest=/etc/foo.conf state=absent #- name: delete foo.conf #shell: rm -f /etc/foo.conf - name: copy foo.conf copy: src=/srv/myfiles/foo.conf dest=/etc/foo.conf owner=root group=root mode=064 notify: - Verify Conf - name: Add template Config template: src=serversforhackers.com.j2 dest=/etc/{{ domain }}.conf owner=root group=root handlers: - name: Verify Conf shell: file /etc/foo.conf #Task2 - hosts: server vars: sdomain: server_0731 domain: 0731 remote_user:root tasks: - name: Add template Config template: src=serversforhackers.com.j2 dest=/etc/{{ sdomain }}.conf owner=root group=root
模板文件: templates/serversforhackers.com.j2
server { # Enforce the use of HTTPS listen 80 default_server; server_name *.{{ domain }}; return 301 https://{{ domain }}$request_uri; }
hosts文件:
[web] 10.0.0.3 10.0.0.4 [server] 10.0.0.5 10.0.0.6 [gateway] 10.0.0.7
執行腳本: # ansible-playbook -i host test.yml (-i 表示指定讀取的host文件路徑)
PLAY [web] ******************************************************************************************************************************************************************************************************************************************************************* TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************* ok: [10.0.0.4] ok: [10.0.0.3] TASK [test connection] ******************************************************************************************************************************************************************************************************************************************************* ok: [10.0.0.4] ok: [10.0.0.3] TASK [httpd check] *********************************************************************************************************************************************************************************************************************************************************** ok: [10.0.0.3] ok: [10.0.0.4] TASK [remove foo.conf] ******************************************************************************************************************************************************************************************************************************************************* changed: [10.0.0.3] changed: [10.0.0.4] TASK [copy foo.conf] ********************************************************************************************************************************************************************************************************************************************************* changed: [10.0.0.4] changed: [10.0.0.3] TASK [Add template Config] *************************************************************************************************************************************************************************************************************************************************** ok: [10.0.0.4] ok: [10.0.0.3] RUNNING HANDLER [Verify Conf] ************************************************************************************************************************************************************************************************************************************************ changed: [10.0.0.3] changed: [10.0.0.4] PLAY [server] **************************************************************************************************************************************************************************************************************************************************************** TASK [Gathering Facts] ******************************************************************************************************************************************************************************************************************************************************* ok: [10.0.0.5] ok: [10.0.0.6] TASK [Add template Config] *************************************************************************************************************************************************************************************************************************************************** ok: [10.0.0.5] ok: [10.0.0.6] PLAY RECAP ******************************************************************************************************************************************************************************************************************************************************************* 10.0.0.3 : ok=7 changed=3 unreachable=0 failed=0 10.0.0.4 : ok=7 changed=3 unreachable=0 failed=0 10.0.0.5 : ok=2 changed=0 unreachable=0 failed=0 10.0.0.6 : ok=2 changed=0 unreachable=0 failed=0
註:完整模塊參見 http://docs.ansible.com/ansible/latest/modules_by_category.html
三 常見檢查
語法檢查 # ansible-playbook --syntax-check test.yml
註:報錯信息有可能位置不準,碰見過對齊格式不正確引發的錯誤
列出主機 # ansible-playbook -i hosts --list-hosts test.yml
四 最佳實踐
參考 http://docs.ansible.com/ansible/latest/playbooks_best_practices.html
根據 Roles分類,存放template 文件,加密yml(如果需要的話)
Git hub上的實例: https://github.com/ansible/ansible-examples
參考資料:
https://serversforhackers.com/c/an-ansible-tutorial
http://getansible.com/advance/best_practice/zui_jia_shi_yong_fang_fa
[ansible-playbook]4 持續集成環境之分布式部署利器 ansible play學習