Ansible部署及使用

.什麼是ansible

    什麼是ansible?官方的title“Ansibleis Simple IT Automation”—>簡單的自動化IT工具。ansible功能:自動化部署APP;自動化管理配置項;自動化的持續交付;自動化的(AWS)雲服務管理。其本質就是在一個臺或者幾臺服務器上,批量的執行命令。

       fabricansible有什麼差別呢?簡單來說fabric像是一個工具箱,提供了很多好用的工具,用來在Remote執行命令,而ansible則是提供了一套簡單的流程,你要按照它的流程來做,就能輕鬆完成任務。當然,它們之間也是有共同點的——都是基於paramiko 開發的。paramiko是什麼呢?它是一個純Python實現的ssh協議庫。因此fabricansible還有一個共同點就是不需要在遠程主機上安裝client/agent,因爲它們是基於ssh來和遠程主機通訊的。

Ansible的執行過程如下圖所示,暖色調帶便已經模塊化。

wKioL1OZPP6REqMvAAKGlzjqxcQ006.jpg

.Ansible的安裝

   配置好yum,直接yum -yinstall ansible即可。

   

yum -y install ansible

安裝完成後會在/etc 下生成兩個文件:ansible的配置文件ansible.cfg和定義被管理節點的hosts.


三.Ansible的具體使用

 Ansible Server:10.0.0.122   

 節點信息:

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。
[email protected] (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

其他功能還在摸索中,用到後再做補充。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章