(1)、輕量級,無需在客戶端安裝agent,更新時,只需在操做機上進行一次更新便可;php
(2)、批量任務執行能夠寫成腳本,並且不用分發到遠程就能夠執行;python
(3)、使用python編寫,維護更簡單;linux
(4)、基於SSH工做;sql
(5)、支持文件同步而且對修改以前的文件進行備份,支持回滾;vim
好了,下面咱們開始安裝ansible:首先得準備好安裝環境bash
我這裏準備了兩臺機器: server1: 192.168.1.231 server2: 192.168.1.232
#在server1上部署 #關閉防火牆iptables service iptables stop chkconfig iptables off #而且修改SELINUX=disabled sed -i's#SELINUX=enforcing#SELINUX=disabled#g' /etc/selinux/config #使用yum安裝python等軟件 yum install PyYAML.x86_64 python-paramiko.noarch python-jinja2.x86_64 python-devel –y
#下載ansible和setuptools安裝包 wget https://pypi.python.org/packages/source/a/ansible/ansible-1.7.2.tar.gz wget https://pypi.python.org/packages/source/s/setuptools/setuptools-7.0.tar.gz
#解壓安裝setuptools tar zfxv setuptools-7.0.tar.gz cd setuptools-7.0 python setup.py install cd .. #解壓安裝ansible tar fzvx ansible-1.7.2.tar.gz cd ansible-1.7.2 python setup.py build python setup.py install mkdir /etc/ansible cp examples/ansible.cfg /etc/ansible/ cp examples/hosts /etc/ansible/ cd ..
#配置ansible,我這裏默認是不須要修改的 vi /etc/ansible/ansible.cfg hostfile = /etc/ansible/hosts library = /usr/share/ansible remote_tmp = $HOME/.ansible/tmp pattern = * forks = 5 poll_interval = 15 sudo_user = ansible #ask_sudo_pass = True #ask_pass = True transport = smart remote_port = 22 module_lang = C #修改/etc/ansible/hosts文件 #local_Server [localhost] 127.0.0.1 #client [client] 192.168.1.232 #server2的IP
#ssh互信 [root@ansibleserver ~]#ssh-keygen –b 1024 -t rsa #不斷的回車便可 The key's randomart p_w_picpath is: +--[ RSA 2048]----+ | o o. | | +=o . | | .=+* o | | o* OE. | | .S.= | | +.. | | . + | | . | | | +-----------------+
#而後進入.ssh/目錄下 [root@ansibleserver ~]#cd .ssh/ cat *.pub > authorized_keys chmod –R 700 . #再經過scp命令將authorized_keys拷貝到客戶端192.168.1.232 scp authorized_keys root@192.168.1.232:/root/.ssh/authorized_keys #執行scp出現的錯誤以及解決方法: scp: /root/.ssh/authorized_keys: No such file or directory #解決方法:在客戶端也直接生成一個ssh-keygen –b 124 –t rsa文件便可,這樣的作法主要是生成.ssh/的目錄,服務端纔可以將公鑰拷貝過去
#測試互信是否成功 [root@ansibleserver ~]# ssh 192.168.1.232 Last login: Tue Mar 17 22:56:26 2015 from 192.168.1.231 #無需密碼,直接登陸成功!說明互信已經成功了! [root@localhost ~]# ifconfig eth1 Link encap:Ethernet HWaddr 08:00:27:41:28:38 inetaddr:192.168.1.232 Bcast:192.168.1.255 Mask:255.255.255.0
-i 設備列表路徑,能夠指定一些動態路徑 -f 併發任務數 -private-key 私鑰路徑 -m 模塊名稱 -M 模塊夾的路徑 -a 參數 -k 登錄密碼 -K sudo密碼 -t 輸出結果保存路徑 -B 後臺運行超時時間 -P 調查後臺程序時間 -u 執行用戶 -U sudo用戶 -l 限制設備範圍 -s 是此用戶sudo無需輸入密碼
#使用ansible的ping模塊測試client是否可以通訊! #注意:all 表明全部client的意思 [root@ansibleserver ~]# ansible all -m ping 192.168.1.232 | success >> { "changed": false, "ping":"pong" } 127.0.0.1 | success >> { "changed": false, "ping":"pong" } #查看時間 [root@ansibleserver ~]# ansible all -m command -a "date" 192.168.1.232 | success | rc=0 >> Tue Mar 17 23:06:43 EDT 2015 127.0.0.1 | success | rc=0 >> Tue Mar 17 23:06:44 EDT 2015
[root@ansibleserver ~]# ansible all -m command -a "yum install unzip -y" 192.168.1.232 | success | rc=0 >> Loaded plugins: fastestmirror Setting up Install Process Determining fastest mirrors ………… Installed: unzip.x86_64 0:6.0-1.el6 Complete!
#拷貝文件到遠程主機 相關選項以下: backup:在覆蓋以前,將源文件備份,備份文件包含時間信息。有兩個選項:yes|no content:用於替代「src」,能夠直接設定指定文件的值 dest:必選項。要將源文件複製到的遠程主機的絕對路徑,若是源文件是一個目錄,那麼該路徑也必須是個目錄 directory_mode:遞歸設定目錄的權限,默認爲系統默認權限 force:若是目標主機包含該文件,但內容不一樣,若是設置爲yes,則強制覆蓋,若是爲no,則只有當目標主機的目標位置不存在該文件時,才複製。默認爲yes others:全部的file模塊裏的選項均可以在這裏使用 src:被複制到遠程主機的本地文件,能夠是絕對路徑,也能夠是相對路徑。若是路徑是一個目錄,它將遞歸複製。在這種狀況下,若是路徑使用「/」來結尾,則只複製目錄裏的內容,若是沒有使用「/」來結尾,則包含目錄在內的整個內容所有複製,相似於rsync。
實例:拷貝本地的/root/script目錄全部內容到192.168.1.232的/tmp目錄下 注意:由於script後面沒有加/ ,因此拷貝的是整個目錄 ansible 192.168.1.232 -m copy -a "src=/root/script dest=/tmp/owner=root group=root mode=0644" #拷貝成功的返回信息 192.168.1.232 | success >> { "changed": true, "dest": "/tmp/", "src": "/root/script" } #切換到192.168.1.232機器中查看 [root@localhost ~]# ls /tmp/script/ a.txt b.txt #拷貝script目錄的文件實例: 注意:這裏的script後面是加了/ ,因此只拷貝script目錄下的文件 [root@ansibleserver script]# ansible 192.168.1.232 -m copy -a"src=/root/script/ dest=/tmp/script/ owner=root group=root mode=0644" 192.168.1.232 | success >> { "changed": true, "dest":"/tmp/script/", "src":"/root/script" }
在192.168.1.232的script目錄下查看內容 [root@localhost script]# ll total 0 -rw-r--r-- 1 root root 0 Mar 17 23:38 a.txt -rw-r--r-- 1 root root 0 Mar 17 23:38 b.txt -rw-r--r-- 1 root root 0 Mar 1723:38 c.txt -rw-r--r-- 1 root root 0 Mar 1723:38 d.txt #backup參數:有yes|no兩個選項 ansible 192.168.1.232 -m copy -a "src=/root/script/ dest=/tmp/script/owner=root group=root mode=0644 backup=yes" 提示:例如,src和dest同時有個a.txt文件,若是在src修改了a.txt 再執行copy的時候,dest就會生成一個備份 [root@localhost script]# ll total 4 -rw-r--r-- 1 root root 12 Mar 18 03:09 a.txt -rw-r--r-- 1 root root 0 Mar 18 03:08 a.txt.2015-03-18@03:09~ #由於a.txt被修改過了,因此生成了一個備份 [root@localhost script]# cat a.txt #這裏被修改過,而後copy過來的 hello world [root@localhost script]# cat a.txt.2015-03-18\@03\:09~ #備份的a.txt默認沒有內容
#使用file模塊,更改文件的用戶和權限 [root@ansibleserver ~]# ansible 192.168.1.232 -m file -a "dest=/tmp/a.txtmode=600" #查看更改狀況 [root@localhost tmp]# ll total 8 -rw------- 1 root root 0 Mar 17 23:38 a.txt #建立目錄,相似mkdir –p [root@ansibleserver ~]# ansible 192.168.1.232 -m file -a"dest=/tmp/to/c mode=755 owner=root group=root state=directory" #查看建立狀況 [root@localhost c]# pwd /tmp/to/c #刪除文件或者目錄 [root@ansibleserver ~]# ansible 192.168.1.232 -m file -a"dest=/tmp/a.txt state=absent" 192.168.1.232 | success >> { "changed": true, "path":"/tmp/a.txt", "state":"absent" } #查看刪除狀況 [root@localhost tmp]# ll total 8 -rw-r--r-- 1 root root 0 Mar 17 23:38 b.txt drwxr-xr-x 2 root root 4096 Mar 1723:38 script drwxr-xr-x 3 root root 4096 Mar 1723:53 to -rw-------. 1 root root 0 Dec 2819:45 yum.log
#注意:cron是爲遠程主機定義任務計劃的 #批量定義遠程主機上的定時任務 #首先咱們在本地的/etc/ansible/目錄下定義一個cron.yml文件 - hosts: 192.168.1.232 #遠程主機IP remote_user: root #指定執行的用戶 tasks: #任務 - name: cron #任務名稱 cron: name='cp file' minute=1job='/usr/bin/tmp/script/test.sh' 提示:name 爲註釋名稱,minute爲執行任務的時間間隔,job爲執行的腳本 #定義好以後,咱們執行下ansible-playbook命令 [root@ansibleserver ansible]# ansible-playbook cron.yml PLAY RECAP******************************************************************** 192.168.1.232 :ok=2 changed=1 unreachable=0 failed=0 #出現ok=2 change=1,表明已經在遠程機子上作好定時任務了 #在遠程主機上查看: [root@ansible-client script]# crontab -l #Ansible: cp file 1 * * * * /usr/bin/tmp/script/test.sh
實例2: 目的:在指定節點上定義一個計劃任務,每隔3分鐘到主控端更新一次時間 命令:ansible all-m cron -a 'name="custom job" minute=*/3 hour=* day=* month=*weekday=* job="/usr/sbin/ntpdate 172.16.254.139"'
#先聲明下,使用rsync 模塊,遠程主機系統必須安裝rsync 包,不然沒法使用這個模塊 #先給遠程機裝個rsync吧 [root@ansibleserver ~]# ansible 192.168.1.232 -m yum -a 'name=rsyncstate=latest' #再次驗證下rsync是否安裝成功 [root@ansibleserver ~]# ansible 192.168.1.232 -a "which rsync" 192.168.1.232 | success | rc=0 >> /usr/bin/rsync #看來沒問題了!
#看下使用的參數 [root@ansibleserver ~]# ansible-doc -s synchronize - name: Uses rsync to make synchronizing file paths in your playbooks quickand easy. action: synchronize archive # 是否採用歸檔模式同步,即以源文件相同屬性同步到目標地址 checksum # 是否效驗 compress # 是否壓縮 copy_links # 同步的時候是否複製鏈接 delete # 刪除源中沒有而目標存在的文件 dest= # 目標地址 dest_port # 目標接受的端口 dirs # 以非遞歸的方式傳輸目錄 existing_only # Skipcreating new files on receiver. group # Preservegroup links # Copysymlinks as symlinks. mode # 模式,rsync 同步的方式 PUSH\PULL recursive # 是否遞歸 yes/no rsync_opts # 使用rsync 的參數 rsync_path # 服務的路徑(源碼編譯時需指定) rsync_timeout # Specify a--timeout for the rsync command in seconds. set_remote_user # put user@for the remote paths. If you have a custom ssh config to define the remote userfor src= # 源,同步的數據源 times
實例:將ansible端/tmp/目錄下的script同步到232機子的/tmp/目錄下面 [root@ansibleserver ~]# ansible 192.168.1.232 -m synchronize -a 'src=/tmp/scriptdest=/tmp/' 192.168.1.232 | success >> { "changed": true, "cmd": "rsync--delay-updates -FF --compress --archive --rsh 'ssh -o StrictHostKeyChecking=no'--out-format='<<CHANGED>>%i %n%L' \"/tmp/script\"\"root@192.168.1.232:/tmp/\"", "msg":"cd+++++++++ script/\n<f+++++++++ script/a.txt\n", "rc": 0, "stdout_lines": [ "cd+++++++++script/", "<f+++++++++script/a.txt" ] } #注意:要想ansible端於遠程端的文件保持一致,最好用delete=yes參數 由於,有時候在ansible的目錄下刪除了某個文件,若不加delete=yes參數的話,遠程端的目錄下仍然保留有舊的文件!
#啓動client的httpd服務 [root@ansibleserver ~]# ansible 192.168.1.232 -m service -a"name=httpd state=started" 192.168.1.232 | success >> { "changed": true, "name":"httpd", "state":"started" } #注意:state的狀態有:started restarted stoped #client端查看狀況 [root@localhost ~]# netstat -lntup|grep httpd tcp 0 0 :::80 :::* LISTEN 1565/httpd
#收集主機的全部系統信息 [root@ansibleserver ~]# ansible 192.168.1.232 -m setup #收集系統信息並以主機名爲文件名分別保存在/tmp/facts目錄 [root@ansibleserver facts]# ansible 192.168.1.232 -m setup --tree/tmp/facts [root@ansibleserver facts]# ll total 12 -rw-r--r-- 1 root root 8656 Mar 18 00:25 192.168.1.232 #收集系統內存相關信息 [root@ansibleserver ~]# ansible 192.168.1.232 -m setup -a'filter=ansible_*_mb' 192.168.1.232 | success >> { "ansible_facts": { "ansible_memfree_mb": 299, "ansible_memtotal_mb": 490, "ansible_swapfree_mb": 2047, "ansible_swaptotal_mb": 2047 }, "changed": false } #收集網卡信息 [root@ansibleserver ~]# ansible 192.168.1.232 -m setup -a'filter=ansible_eth[0-2]'
對於需反覆執行的、較爲複雜的任務,咱們能夠經過定義 Playbook 來搞定。Playbook 是 Ansible 真正強大的地方,它容許使用變量、條件、循環、以及模板,也能經過角色及包含指令來重用既有內容。下面咱們來看看一些具體的實例。併發
#首先在/etc/ansible目錄下創建一個php.yaml的文件 [root@ansibleserver ansible]# vim php.yaml - hosts: 192.168.1.232 #主機名,若是是所有主機,能夠用all remote_user: root #指定執行操做的用戶 tasks: #任務 - name: php installing #起個任務的名字 yum: name=php state=present #利用yum模塊,安裝軟件的包名爲php 參數:present爲安裝 absent爲卸載 提示:注意對齊的格式,否則會出錯 #用ansible-playbook 參數調用php.yaml [root@ansibleserver ansible]# ansible-playbook php.yaml PLAY [192.168.1.232]********************************************************** GATHERING FACTS*************************************************************** ok: [192.168.1.232] TASK: [php installing]******************************************************** changed: [192.168.1.232] PLAY RECAP ******************************************************************** 192.168.1.232 : ok=2 changed=1 unreachable=0 failed=0 #看到結果,ok=2 changed=1 說明客戶機(232)上的php安裝成功了!
#創建一個cron.yaml文件,而後每個月10號來運行/root/dd.sql腳本 #cron定時任務參數 # Ensure a job that runs at 2 and 5 exists. # Creates an entry like "* 5,2 * * ls -alh > /dev/null" - cron: name="check dirs" hour="5,2" job="ls -alh> /dev/null" # Ensure an old job is no longer present. Removes any job that is prefixed # by "#Ansible: an old job" from the crontab - cron: name="an old job" state=absent # Creates an entry like "@reboot /some/job.sh" - cron: name="a job for reboot" special_time=rebootjob="/some/job.sh" # Creates a cron file under /etc/cron.d - cron: name="yum autoupdate" weekday="2" minute=0hour=12 user="root"job="YUMINTERACTIVE=0 /usr/sbin/yum-autoupdate" cron_file=ansible_yum-autoupdate # Removes a cron file from under /etc/cron.d - cron:cron_file=ansible_yum-autoupdate state=absent
#關於ansible的介紹就到此,有不足之處,但願你們多多指教!
運維