最近問了個朋友關於代碼發佈的問題,他們測試環境是用webhook,線上用的則是ansible。html
那ansible是什麼呢?在它的github主頁介紹有段話node
Ansible is a radically simple IT automation system. It handles configuration-management, application deployment, cloud provisioning, ad-hoc task-execution, and multinode orchestration - including trivializing things like zero downtime rolling updates with load balancers.
翻譯下來就是:python
ansible是一個很簡單的IT自動化系統。它能夠用來進行配置管理,應用部署,雲資源分配,執行ad-hoc任務,還有多節點編排 - 包括瑣碎的事情,如零停機更新與負載均衡器。git
sudo apt-get install ansible
安裝後的配置文件在/etc/ansible/
目錄,ansible.cfg爲ansible配置文件,hosts爲遠程主機配置文件github
若還未安裝pip(安裝和管理python包的工具),可執行下列命令安裝web
sudo easy_install pip
而後安裝pipshell
sudo pip install ansible
ansible中的臨時命令的執行是經過Ad-Hoc來完成,可以快速執行,並且不須要保存執行的命令,例如:ubuntu
ansible -i ~/hosts all -m command -a 'who' -u root ansible web -u Ponny -m script -a '/home/vagrant/my.sh' //腳本是主機上的腳本
主要參數以下:
-u username 指定ssh鏈接的用戶名,即執行後面命令的用戶
-i inventory_file 指定所使用的inventory文件的位置,默認爲/etc/ansible/hosts
-m module 指定使用的模塊,默認爲command
-f 10 指定併發數,併發量大的時候,提升該值
--sudo [-k] 當須要root權限執行的化,-k參數用來輸入root密碼
-a 使用模塊的參數安全
在github上有playbook的簡單示例,我本身編寫了個簡單的my.yml以下:併發
--- - hosts: web remote_user: Ponny tasks: - name: change dir command: cd /Users/Ponny/localhost - name: test connection file: path=my.test state=touch owner=Ponny group=staff mode=0777
原本想的作法是先change目錄,再建立文件,但彷佛並不成功,由於task不能更改work目錄
運行劇本:
ansible-playbook -i /etc/ansible/hosts my.yml
If you want to run a command through the shell (say you are using '<', '>','|', etc), you actually want the [shell] module instead. The [command] module is much more secure as it's not affected by the user's environment. 'creates', 'removes', and 'chdir' can be specified after the command. For instance, if you only want to run a command if a certain file does not exist, use this.
就是說若是你用的命令裏有 '<', '>', '|' 等這些符號,執行是不成功的,最好用shell模塊來代替,