自動化工具 ansible完整安裝

批量的在遠程服務器上執行命令html

1.什麼是ansible

ansible是個什麼東西呢?官方的title是「Ansible is Simple IT Automation」——簡單的自動化IT工具。這個工具的目標有這麼幾項:讓咱們自動化部署APP;自動化管理配置項;自動化的持續交付;自動化的(AWS)雲服務管理。node

全部的這幾個目標本質上來講都是在一個臺或者幾臺服務器上,執行一系列的命令而已。就像我以前有介紹過的Fabric,以及咱們基於Fabric開發的自動化應用部署的工具: Essay 。都是作了這麼個事——批量的在遠程服務器上執行命令 。python

那麼fabric和ansible有什麼差異呢?簡單來講fabric像是一個工具箱,提供了不少好用的工具,用來在Remote執行命令,而Ansible則是提供了一套簡單的流程,你要按照它的流程來作,就能輕鬆完成任務。這就像是庫和框架的關係同樣。服務器

固然,它們之間也是有共同點的——都是基於 paramiko 開發的。這個paramiko是什麼呢?它是一個純Python實現的ssh協議庫。所以fabric和ansible還有一個共同點就是不須要在遠程主機上安裝client/agents,由於它們是基於ssh來和遠程主機通信的。框架

2.快速安裝

上面簡單介紹了下這是個什麼東西,怎麼安裝呢?也很簡單,由於ansible是python開發的,所以能夠這麼安裝:dom

使用yum安裝ssh

yum install epel-release -y  
yum install ansible -y

3.配置

ansible經過文件來定義你要管理的主機,也就是說把你須要的管理的主機ip寫到一個文件中便可。
這個文件通常名爲hosts,它能夠放在多個路徑下,也能夠自定義名稱和路徑。 默認咱們用/etc/ansible/hosts這個文件便可ide

hosts文件的格式爲:工具

[node]
10.2.2.121  
10.2.2.122

默認ssh端口是22,若是主機端口號是其餘的,在ip後加:端口號便可,如10.2.1.203的端口是2211,屬於test組,格式以下:ui

[test]
10.2.1.201  
10.2.1.202  
10.2.1.203:2211

4.使用ssh-keygen產生ssh密鑰

[root@test-201 ~]# ssh-keygen 
Generating public/private rsa key pair.  
Enter file in which to save the key (/root/.ssh/id_rsa):  
Enter passphrase (empty for no passphrase):  
Enter same passphrase again:  
Your identification has been saved in /root/.ssh/id_rsa.  
Your public key has been saved in /root/.ssh/id_rsa.pub.  
The key fingerprint is:  
dd:20:23:7c:1a:2e:01:bf:b1:67:7a:08:87:5f:e6:7e root@test-201  
The key’s randomart image is:  
+–[ RSA 2048]—-+ 
| . | 
| o . | 
| + + + . | 
| . * = + o | 
| o = B S . . | 
| + X | 
| + o | 
| o E | 
| .. | 
+—————–+

將公鑰發送到要管理的服務器 使用ssh-copy-id命令

好比要發送到10.2.31.202,使用以下命令:

[root@test-201 ~]# ssh-copy-id -i /root/.ssh/id_rsa.pub 10.2.31.202 
root@10.2.31.202’s password:  
Now try logging into the machine, with 「ssh ‘10.2.31.202’」, and check in:

.ssh/authorized_keys

to make sure we haven’t added extra keys that you weren’t expecting.

5.使用ansible

命令格式以下: ansible + 主機組名稱 + -m + 模塊名稱 + -a + 參數

主機組名稱,即hosts中定義的主機組名稱

-m 指使用模塊,後加指定的模塊名稱
-a 指傳給模塊的參數

在不指定模塊時,默認調用command模塊。

如咱們想看下test組上的服務器的/tmp下面有哪些文件,可使用以下命令

ansible test -a 「ls /tmp」

[root@test-201 ~]# ansible test -a "ls /tmp"
10.2.31.203 | SUCCESS | rc=0 >>  
ansible_EMEGZI  
testabcdefg

咱們可使用copy模塊,將本地文件發送到目標服務器上,如:

ansible test -m copy -a 「src=/root/install.log dest=/tmp」

這個命令是將本地的/root/install.log發送到test組的/tmp下,執行的效果以下:

[root@test-201 ~]# ansible test -m copy -a "src=/root/install.log dest=/tmp"
10.2.31.203 | SUCCESS => {  
    "changed": true, 
    "checksum": "114ee153946d9cd2e690c405e5796a4fcc400542", 
    "dest": "/tmp/install.log", 
    "gid": 0, 
    "group": "root", 
    "md5sum": "17b18780156a31a65d62a324110d686e", 
    "mode": "0644", 
    "owner": "root", 
    "secontext": "unconfined_u:object_r:admin_home_t:s0", 
    "size": 43688, 
    "src": "/root/.ansible/tmp/ansible-tmp-1466157410.68-191787255687953/source", 
    "state": "file", 
    "uid": 0
}

你可使用ansible-doc –list查看當前的全部模塊

[root@test-201 ~]# ansible-doc  --list                       
….
….                                                        
authorized_key                     Adds or removes an SSH authorized key  
azure                              create or terminate a virtual machine in azure  
azure_rm_deployment                Create or destroy Azure Resource Manager template deployments  
azure_rm_networkinterface          Manage Azure network interfaces.  
azure_rm_networkinterface_facts    Get network interface facts.  
azure_rm_publicipaddress           Manage Azure Public IP Addresses.  
azure_rm_publicipaddress_facts     Get public IP facts.  
azure_rm_resourcegroup             Manage Azure resource groups.  
azure_rm_resourcegroup_facts       Get resource group facts.  
azure_rm_securitygroup             Manage Azure network security groups.  
….
…

ansible自帶了不少豐富的模塊,詳細請看:
http://docs.ansible.com/ansible/list_of_all_modules.html

6.小技巧:

有時候若是想直接操做某臺服務器,但又沒有在hosts裏定義這臺服務器時,可使用以下命令:

ansible all -i ‘服務器ip,’
注意服務器ip後面要加個,

ansible all -i ‘10.2.31.201,’ -u test -k -a ‘uptime’

BUG?

目前遇到兩個bug(也多是個人使用方式不對,正在關注中) 1.在中文路徑下沒法使用.
若是在一個含中文的路徑下面使用ansible,會沒法執行,提示: UnicodeDecodeError: ‘ascii’ codec can’t decode byte 0xe4 in position 14
因此不要跑到中文路徑下面去執行ansible

2.su命令不能用.
使用su命令不成功,無在目標機器上經過一個普通用戶su切換爲root執行相關命令 錯誤以下:

ansible Timeout (12s) waiting for privilege escalation prompt
相關文章
相關標籤/搜索