Ansible 安裝 簡單使用

安裝ansible

只需要將管理節點安裝ansible ,被管理節點不需要安裝

但如果使用yum安裝的話,必須配置epel源

rpm -Uvh  http://dl.fedoraproject.org/pub/epel/6/x86_64/epel-release-6-8.noarch.rpm

rpm -Uvh  http://dl.fedoraproject.org/pub/epel/5Server/x86_64/epel-release-5-4.noarch.rpm

 

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-6

安裝依賴關係包

yum install python-dev python-yaml python-paramikopython-jinja2 git

 

使用yum安裝absible

[root@node013 yum.repos.d]# yum installansible

Loaded plugins: fastestmirror, product-id,security, subscription-manager

This system is not registered to Red HatSubscription Management. You can use subscription-manager to register.

Loading mirror speeds from cached hostfile

 *epel: mirrors.yun-idc.com

Setting up Install Process

Resolving Dependencies

--> Running transaction check

---> Package ansible.noarch 0:1.7-1.el6will be installed

--> Processing Dependency:python-setuptools for package: ansible-1.7-1.el6.noarch

--> Processing Dependency: python-paramikofor package: ansible-1.7-1.el6.noarch

--> Processing Dependency: python-keyczarfor package: ansible-1.7-1.el6.noarch

--> Processing Dependency: python-httplib2for package: ansible-1.7-1.el6.noarch

--> Processing Dependency: python-crypto2.6for package: ansible-1.7-1.el6.noarch

--> Running transaction check

---> Package python-crypto2.6.x86_640:2.6.1-1.el6 will be installed

---> Package python-httplib2.noarch0:0.7.7-1.el6 will be installed

---> Package python-keyczar.noarch 0:0.71c-1.el6will be installed

--> Processing Dependency: python-pyasn1for package: python-keyczar-0.71c-1.el6.noarch

---> Package python-paramiko.noarch0:1.7.5-2.1.el6 will be installed

---> Package python-setuptools.noarch0:0.6.10-3.el6 will be installed

--> Running transaction check

---> Package python-pyasn1.noarch0:0.0.12a-1.el6 will be installed

--> Finished Dependency Resolution

Dependencies Resolved

===============================================================================

 Package                                               Arch                                       Version                                              Repository                                 Size

===============================================================================

Installing:

 ansible                                               noarch                                      1.7-1.el6                                            epel                                     874 k

Installing for dependencies:

 python-crypto2.6                                      x86_64                                     2.6.1-1.el6                                          epel                                     530 k

 python-httplib2                                        noarch                                     0.7.7-1.el6                                           epel                                       70 k

 python-keyczar                                        noarch                                     0.71c-1.el6                                          epel                                     219 k

 python-paramiko                                       noarch                                     1.7.5-2.1.el6                                         base                                      728 k

 python-pyasn1                                         noarch                                     0.0.12a-1.el6                                        base                                       70 k

 python-setuptools                                     noarch                                     0.6.10-3.el6                                         base                                     336 k

 

Transaction Summary

===============================================================================

Install      7 Package(s)

 

Total download size: 2.8 M

Installed size: 16 M

Is this ok [y/N]: y

Downloading Packages:

(1/7): ansible-1.7-1.el6.noarch.rpm                                                                                                                                               |874 kB     00:18    

(2/7):python-crypto2.6-2.6.1-1.el6.x86_64.rpm                                                                                                                                    | 530 kB     00:08    

(3/7):python-httplib2-0.7.7-1.el6.noarch.rpm                                                                                                                                     |  70 kB     00:01     

(4/7):python-keyczar-0.71c-1.el6.noarch.rpm                                                                                                                                      | 219 kB     00:03    

(5/7):python-paramiko-1.7.5-2.1.el6.noarch.rpm                                                                                                                                    |728 kB     00:15    

(6/7):python-pyasn1-0.0.12a-1.el6.noarch.rpm                                                                                                                                     |  70 kB    00:01    

(7/7):python-setuptools-0.6.10-3.el6.noarch.rpm                                                                                                                                  | 336 kB     00:09    

-------------------------------------------------------------------------------

Total                                                                                                                                                                     40 kB/s | 2.8 MB     01:11    

Running rpm_check_debug

Running Transaction Test

Transaction Test Succeeded

Running Transaction

 Installing : python-setuptools-0.6.10-3.el6.noarch                                                                                                                                                 1/7

 Installing : python-paramiko-1.7.5-2.1.el6.noarch                                                                                                                                                  2/7

 Installing : python-pyasn1-0.0.12a-1.el6.noarch                                                                                                                                                    3/7

 Installing : python-keyczar-0.71c-1.el6.noarch                                                                                                                                                      4/7

 Installing : python-crypto2.6-2.6.1-1.el6.x86_64                                                                                                                                                   5/7

 Installing : python-httplib2-0.7.7-1.el6.noarch                                                                                                                                                    6/7

 Installing : ansible-1.7-1.el6.noarch                                                                                                                                                              7/7

 Verifying  :python-keyczar-0.71c-1.el6.noarch                                                                                                                                                     1/7

 Verifying  :python-httplib2-0.7.7-1.el6.noarch                                                                                                                                                    2/7

 Verifying  :ansible-1.7-1.el6.noarch                                                                                                                                                        3/7

Verifying : python-crypto2.6-2.6.1-1.el6.x86_64                                                                                                                                              4/7

 Verifying  :python-pyasn1-0.0.12a-1.el6.noarch                                                                                                                                                    5/7

 Verifying  :python-paramiko-1.7.5-2.1.el6.noarch                                                                                                                                                   6/7

 Verifying  :python-setuptools-0.6.10-3.el6.noarch                                                                                                                                                 7/7

 

Installed:

 ansible.noarch 0:1.7-1.el6                                                                                                                                                                             

 

Dependency Installed:

 python-crypto2.6.x86_64 0:2.6.1-1.el6    python-httplib2.noarch 0:0.7.7-1.el6  python-keyczar.noarch 0:0.71c-1.el6  python-paramiko.noarch 0:1.7.5-2.1.el6  python-pyasn1.noarch 0:0.0.12a-1.el6 

 python-setuptools.noarch 0:0.6.10-3.el6 

 

Complete!

 

 

軟件包:

ansible

 PyYAML

 libyaml

 python-babel

 python-crypto

 python-crypto2.6

 python-httplib2 python-jinja2

python-keyczar

 python-paramiko

 python-pyasn1

 python-setuptools

 

 

ansbible配置文件

安裝後的配置文件位於/etc/ansible

[root@node1ansible]# pwd

/etc/ansible

 

[root@node1ansible]# ll
-rw-r--r--. 1 root root 5507 May  1 17:53 ansible.cfg     #主配置文件
-rw-r--r--. 1 root root  965 Mar 15 13:07 hosts           #目標主機

目標被管理主機要定義在host中,否則只能一個一個去手寫了

配置文件ansible.cfg:

hostfile  = /etc/ansible/hosts      

#默認目標主機文件
library = /usr/share/ansible      

#庫文件,裝載模塊時候其模塊裝載的路徑,因爲ansible是模塊化的工具

module_name =command        

#模塊的名稱,意思是沒有使用指定模塊的時候其默認使用的模塊,默認是命令,意爲只執行命令

forks = 5                           

#啓動的子進程

remote_port  = 22                   

#被管理主機的端口,默認爲22,如果ssh端口有變動則需要修改此參數

sudo_user = root                     

#如果以普通用戶登錄進行sudo那麼sudo的默認賬戶是root

 

host配置文件

一般按照安全性要求,應該禁止 root 登錄,禁用密碼登錄,一律使用證書登錄。

ansible 支持通過 sudo 來執行

所有管理主機,每個被管理主機都可以使用主機名或者ip地址都可以

使用中括號可以組,緊跟着中括號下面的都是同一組內被管理的主機

[root@node1ansible]#grep -v "^#" hosts | grep -v "^$"
green.example.com
blue.example.com
192.168.100.1
192.168.100.10
[webservers]
alpha.example.org
beta.example.org
192.168.1.100
192.168.1.110
www[001:006].example.com          #
定義組的時候還可以使用通配符
[dbservers]
db01.intranet.mydomain.net
db02.intranet.mydomain.net
10.25.1.56
10.25.1.57
db-[99:101]-node.example.com

使用absible批量管理節點

假如這個配置文件不符合我們的意向,可以cp一份作爲參考模板,方便以後借鑑

這裏我們定義2臺被管理節點

#使用[]指定分組

[root@node1ansible]# grep -v '#'  hosts
[hac]
10.10.18.184

10.10.18.185

 

ssh祕鑰

要基於ssh工作,所以本地要生成一對ssh祕鑰,然後讓其基於祕鑰認證方式來管理節點

[root@node013ansible]# ssh-keygen -t rsa -P ''

Generatingpublic/private rsa key pair.

Enterfile in which to save the key (/root/.ssh/id_rsa):

/root/.ssh/id_rsaalready exists.

Overwrite(y/n)? y

Youridentification has been saved in /root/.ssh/id_rsa.

Yourpublic key has been saved in /root/.ssh/id_rsa.pub.

Thekey fingerprint is:

52:74:7e:88:0a:b1:4f:22:a0:12:d8:79:b3:84:15:63root@node013

Thekey's randomart image is:

+--[RSA 2048]----+

|+.+E.  . .      |

|oo+.++. + .     |

|o.o+o. o o .    |

|.  ..= o  .     |

|      + S       |

|       .        |

+-----------------+

傳送至被管理控節點上去

cd /root/                                          #注意.ssh目錄在ROOT家目錄中

#因爲本地也是被監控節點之一

[root@node013 ~]#ssh-copy-id -i .ssh/id_rsa.pub [email protected]

Warning: Permanentlyadded '10.10.18.184' (RSA) to the list of known hosts.

[email protected]'spassword:

Now try logging intothe machine, with "ssh '[email protected]'", and check in:

  .ssh/authorized_keys

to make sure we haven'tadded extra keys that you weren't expecting.


ssh-copy-id -i .ssh/id_rsa.pub [email protected]    
ssh-copy-id -i .ssh/id_rsa.pub [email protected]     

 

嘗試ansible是否可用

先來man一下幫助手冊

ANSIBLE(1)   System administration commands   ANSIBLE(1)
NAME
    ansible - run acommand somewhere else
SYNOPSIS   #使用格式

ansible <host-pattern> [-f forks] [-mmodule_name][-a args]

主機模式,可以使用通配

-f forks      表示我們可以明確指明啓動多少個子進程去連接這些主機默認不定義爲5個forks,如果節點比較多,可以將forks參數調大

-m           指定使用的模塊,每個模塊都需要接受特定參數

-a           來指定使用參數

比如我們想讓所有主機顯示當前系統時間

[root@node1~]#ansible all -m command -a 'date'

-mcommand 爲指定模塊,默認爲command,如果默認執行命令的話,可以不加這個模塊,如下所示

[root@node1~]#ansible all -a 'date'

可以看到,時間不同步,但是命令是同時進行的,因爲會發起N個子進程分別與每個被監控節點進行交互的

如果節點比較多,可以將forks參數調大

 

現在期望多個節點自動的去ntp服務器同步時間

[root@node1~]#ansible all -a 'crontab -l'
可以看到只有一個節點存在,那麼我們只要同步其餘兩臺機器即可

定義單獨組XK

[root@node1ansible]#cat hosts

[xk]

10.10.18.184

10.10.18.185

10.10.18.186

首先先來手動同步

[root@node1ansible]#ansible xk -a 'ntpdatetime.neusoft.com'

將時間同步加入計劃任務,我們可以使用命令追加的方式來寫入計劃任務

[root@node1ansible]#ansible crontab -a 'echo '00 * * * * * ' >>/var/spool/cron/root'

這樣一來可以實現,但是實在是太麻煩了,爲此,ansible有專門計劃任務管理的模塊,可以使用內置模塊來實現以最簡單的命令方式來輕鬆批量管理

定義crontab的參數有很多,可以在官方去查找幫助文檔,這裏不再一一介紹

 

明確指定xk組,-m指定cron模塊 -a指定參數

[root@node1ansible]# ansible xk -m cron -a 'name="sync time from time.neusoft.com" minute='*/3' job="/usr/sbin/ntpdate time.neusoft.com" '

[root@node1 ansible]# ansible all -a 'crontab -l'
參數說明:

name=synctime from time.windows" 這是任務的名稱註釋會寫到crontab裏面去,也可以不指定,效果如下所示

[root@node1ansible]#crontab -l

#Ansible:synctime from time.windows.com                       #因此上面多了一個註釋信息

*/3* * * */usr/sbin/ntpdate time.windows.com

minute='*/3 我們這裏只需要關注分鐘,只需要提供分鐘的定義,省去的統統默認爲*

job="/usr/sbin/ntpdatetime.windows.com"   此爲我們定義的計劃任務執行的命令

使用ansible批量安裝程序

我們工作場景中,如果想安裝程序的話,只要我們的yum源無問題,那麼則可以完全使用yum來安裝

那我們來man一下其模塊

man的使用方法比較獨特如果想man查看ansible模塊的話必須加符號.

[root@node1ansible]#man ansible.yum

安裝lrzsz

[root@node1ansible]#ansible xk -m yum -a 'name=lrzsz state=present'

state=present 狀態判斷其是安裝還是卸載的,這裏也可以使用install

[root@node1ansible]#ansible xk -m yum -a 'name=lrzsz state=present'

對於ansible來講的話,很難生成yum配置文件,但可以使用wget將其批量下載

#可以看到只有定義個沒用裝,因爲本來就沒有安裝。。

[root@node1ansible]#ansible all -a 'rpm -q lrzsz '


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