一,ansible使用playbook的優點
1,用ansible執行一些簡單的任務,使用ad-hoc命令就可以解決問題
如果執行復雜的功能,需要大量的操作,執行的ad-hoc命令會不夠方便,這時我們選擇使用playbook。
使用playbook你可以方便的重用代碼,可以移植到不同的機器上面
可以像函數一樣,最大化的復用代碼。
如果把常見的操作都編寫成playbook,之后管理服務器會變得十分簡單
2,playbook使用YMAL語言編寫
YMAL語言的格式:
文件的第一行以 "---" (三個連字符)開始,表示是YMAL文件的開始。 在同一行中,#之后的內容表示注釋 YMAL中的列表元素以”-”開頭然后緊跟着一個空格,后面為元素內容 同一個列表中的元素應該保持相同的縮進,否則會被當做錯誤處理 play中hosts/variables/roles/tasks等對象的表示方法都是鍵值中間以": "分隔表示(":"后面還有一個空格)
說明:劉宏締的架構森林是一個專注架構的博客,地址:https://www.cnblogs.com/architectforest
對應的源碼可以訪問這里獲取: https://github.com/liuhongdi/
說明:作者:劉宏締 郵箱: 371125307@qq.com
二,例子:編寫一個簡單的playbook
功能:安裝screen
#hosts:指定要操作的hosts,如果是所有機器,可以用all
#remote_user: 登錄用的用戶
#become:是否要切換用戶
#become_user:要切換到的用戶
#tasks:要執行的任務
#command: 用command模塊執行命令
#說明:用dnf安裝軟件其實應該用dnf模塊,我們這里只是做一個演示
[root@centos8 playbook]# vi installscreen.yml
代碼:
--- - hosts: yujian remote_user: webop become: yes become_user: root tasks: - name: "安裝screen" command: dnf install --quiet -y screen
測試執行:
[liuhongdi@centos8 playbook]$ ansible-playbook installscreen.yml PLAY [yujian] ****************************************************************************************** TASK [Gathering Facts] ********************************************************************************* TASK [安裝screen] *************************************************************************************** [WARNING]: Consider using the dnf module rather than running 'dnf'.
If you need to use command because dnf is insufficient you can add 'warn: false' to this command task or set 'command_warnings=False' in ansible.cfg to get rid of this message. changed: [121.122.123.47] PLAY RECAP ********************************************************************************************* 121.122.123.47 : ok=2 changed=1 unreachable=0 failed=0 skipped=0 rescued=0 ignored=0
說明:用dnf安裝軟件應該使用dnf模塊
ansible給出了一個warning,
我們這里只做為演示,不用管它
服務器端檢查是否安裝成功?
[root@blog ~]$ whereis screen screen: /usr/bin/screen /usr/share/screen /usr/share/man/man1/screen.1.gz /usr/share/info/screen.info.gz
安裝成功了
三,playbook參數的使用例子:
1,語法檢查
[root@centos8 installscreen]# ansible-playbook --syntax-check installscreen.yml
2,列出涉及到的host
[root@centos8 installscreen]# ansible-playbook --list-host installscreen.yml
3,檢查會發生的改變,但不是真的執行命令
[root@centos8 installscreen]# ansible-playbook --check installscreen.yml
4,列出tasks
[root@centos8 playbook]# ansible-playbook --list-tasks installscreen.yml
四,例子: 使用playbook執行script並查看執行結果
腳本的功能是發布git分支
1,腳本:
[liuhongdi@centos8 script]$ vi gitpubwww.sh
內容
cd /data/web/think_www; echo "---------------------------------------git status:\n"; git status; echo "---------------------------------------git pull:\n"; git pull origin master; echo "---------------------------------------git status:\n"; git status;
2,發布git分支的playbook
#register: 注冊一個變量,用來存儲返回的結果
#debug:啟用debug模塊,把輸出結果顯示出來
[root@centos8 playbook]# vi gitpubwww.yml
內容
--- - hosts: yujian remote_user: webop become: yes become_user: root tasks: - name: "www項目發布git分支" script: /data/ansible/script/gitpubwww.sh register: gitpub_out - name: "查看結果" debug: var=gitpub_out verbosity=0
3,執行playbook
[liuhongdi@centos8 playbook]$ ansible-playbook gitpubwww.yml
五,查看ansible的版本
[liuhongdi@centos8 ~]$ ansible --version ansible 2.9.7 config file = /etc/ansible/ansible.cfg configured module search path = ['/home/liuhongdi/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/lib/python3.6/site-packages/ansible executable location = /usr/bin/ansible python version = 3.6.8 (default, Nov 21 2019, 19:31:34) [GCC 8.3.1 20190507 (Red Hat 8.3.1-4)]