一.什麼是ansiblepython
什麼是ansible?官方的title是「Ansibleis Simple IT Automation」—>簡單的自動化IT工具。ansible功能:自動化部署APP;自動化管理配置項;自動化的持續交付;自動化的(AWS)雲服務管理。其本質就是在一個臺或者幾臺服務器上,批量的執行命令。nginx
fabric和ansible有什麼差異呢?簡單來講fabric像是一個工具箱,提供了不少好用的工具,用來在Remote執行命令,而ansible則是提供了一套簡單的流程,你要按照它的流程來作,就能輕鬆完成任務。固然,它們之間也是有共同點的——都是基於paramiko 開發的。paramiko是什麼呢?它是一個純Python實現的ssh協議庫。所以fabric和ansible還有一個共同點就是不須要在遠程主機上安裝client/agent,由於它們是基於ssh來和遠程主機通信的。git
Ansible的執行過程以下圖所示,暖色調帶便已經模塊化。github
二.Ansible的安裝shell
配置好yum,直接yum -yinstall ansible便可。centos
yum -y install ansible
安裝完成後會在/etc 下生成兩個文件:ansible的配置文件ansible.cfg和定義被管理節點的hosts.bash
三.Ansible的具體使用服務器
Ansible Server:10.0.0.122 併發
節點信息:app
cat /etc/ansible/hosts 10.0.0.128 #nginx01 10.0.0.129 #nginx02 10.0.0.130 #nginx03
創建Server及各節點的信任關係:
ssh-keygen -t rsa -P '' -f .ssh/id_rsa ssh-copy-id -i .ssh/id_rsa.pub 10.0.0.128--10.0.0.130
ansible命令的用法:
創建完信任關係後,測試下各節點與Server的連通性:
[root@cobbler ~]# ansible all -m ping #all表示全部節點,-m指定模塊 默認爲command,ping指定動做 10.0.0.129 | success >> { "changed": false, "ping": "pong" } 10.0.0.130 | success >> { "changed": false, "ping": "pong" } 10.0.0.128 | success >> { "changed": false, "ping": "pong" }
ansible批量執行命令示例:
[root@cobbler ~]# ansible all -a 'uptime' #all全部節點, -a用於指定命令,‘uptime’執行的命令,若是命令有參數必定要用引號;ansible默認使用command模塊,因此能夠不用指定。 10.0.0.130 | success | rc=0 >> 11:31:50 up 2:01, 1 user, load average: 0.00, 0.00, 0.00 10.0.0.129 | success | rc=0 >> 11:31:50 up 2:02, 1 user, load average: 0.00, 0.00, 0.00 10.0.0.128 | success | rc=0 >> 11:31:50 up 2:05, 1 user, load average: 0.00, 0.00, 0.00 [root@cobbler ~]# ansible-doc -l #查看ansible的全部模塊 acl Sets and retrieves file ACL information. add_host add a host (and alternatively a group) to the ansible-playbo airbrake_deployment Notify airbrake about app deployments apt Manages apt-packages [root@cobbler ~]# ansible-doc yum #查看指定模塊的文檔 > YUM Installs, upgrade, removes, and lists packages and groups with the `yum' package manager. [root@cobbler ~]# ansible all -m yum -a "name=httpd state=present" #用yum模塊給各節點安裝httpd。name指定安裝包名,state指定動做:安裝能夠用present和latest,卸載使用absent。 10.0.0.128 | success >> { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed" ] } 10.0.0.130 | success >> { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed" ] } 10.0.0.129 | success >> { "changed": false, "msg": "", "rc": 0, "results": [ "httpd-2.2.15-30.el6.centos.x86_64 providing httpd is already installed" ] }
ansible執行shell比較麻煩,建議安裝ansible-shell
ansible-shell內置的命令主要有四個:
cd : 切換到指定的組/表達式篩選的機器集合上
list: 顯示目前的機器集合,list groups 能夠列出全部的組(對咱們可能沒啥用)
serial:運行時的併發度,默認是20
help: 顧名思義,他能生成簡單的模塊幫助信息,方便即時查詢
yum -y install git git clone https://github.com/dominis/ansible-shell.git yum -y install python-pip pip install -e ./ansible-shell
[root@cobbler ~]# ansible-shell Welcome to the ansible-shell. Type help or ? to list commands. root@ (0)[s:2]$ cd 10.0.0.128 #cd到指定host。 root@10.0.0.128 (1)[s:2]$ ls #list出了10.0.0.128這個主機上/root下的文件 =============== 10.0.0.128 ================ anaconda-ks.cfg install.log install.log.syslog nginx-1.4.7 nginx-1.4.7.tar.gz nginx_install.sh
其餘功能還在摸索中,用到後再作補充。