安裝自動化工具ansible

最近問了個朋友關於代碼發佈的問題,他們測試環境是用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

安裝

  • ubuntu
sudo apt-get install ansible

安裝後的配置文件在/etc/ansible/目錄,ansible.cfg爲ansible配置文件,hosts爲遠程主機配置文件github

  • pip安裝

若還未安裝pip(安裝和管理python包的工具),可執行下列命令安裝web

sudo easy_install pip

而後安裝pipshell

sudo pip install ansible

特色

  • 使用python編寫
  • ansible無需客戶端,直接經過ssh來進行鏈接,這也意味着它比較慢,這也讓ansible不須要在遠程主機上啓動守護進程,並且ssh數據傳輸是通過加密的,主機不容易被攻破,更安全

命令介紹

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 使用模塊的參數安全

playbook

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

注意點

  • command模塊有個須要注意的地方
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模塊來代替,

相關文章
相關標籤/搜索