說明:mysql
系統爲CentOS6.5 web
manager 192.168.10.1sql
web1 192.168.10.2shell
web2 192.168.10.3vim
安裝ansible
bash
wget http://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm rpm -ivh epel-release-6-8.noarch.rpm #雙機互信 [root@manager ~]# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P '' for i in web1 web2;do ssh-copy-id -i root@$i;done #安裝ansible # yum install ansible # ansible --version ansible 1.8.2
ansible幫助文檔:https://galaxy.ansible.com/explore#/ http://www.ansible.cn/docs/ 服務器
#ansible 怎麼用?測試幾個經常使用的命令
ansible ansible-galaxy ansible-pull ansible-vault
ansible-doc ansible-playbook ansible-shell
ssh
# ansible -h #ansible的用法,後面可加的參數,好比connection、sudo、module-name、timeout
ide
# ansible-doc -l #ansible的工具書,查看支持的modules,能夠說是命令的目錄工具
# ansible-doc ping #查看ping模塊的使用方法,同理查看file的用法# ansible-doc file
> PING
A trivial test module, this module always returns `pong' on
successful contact. It does not make sense in playbooks, but it is
useful from `/usr/bin/ansible'
# Test 'webservers' status
cd /etc/ansible/ #修改ansible的hosts文件
vim hosts #註釋全部,添加如下
[web1]
192.168.10.2
[web2]
192.168.10.3
ansible web1 -a uptime #等同於ansible web1 -m command -a uptime
ansible web1 -m yum -a "name=dstat state=latest" #執行yum install dstat
ansible web1 -m raw -a "rpm -qa|grep dstat" #執行rpm -qa |grep dstat
ansible web1 -m shell -a "service mysqld restart" #執行service mysqld restart
ansible web1 -m service -a "name=mysqld state=stopped" #執行service mysqld stop
ansible web1 -m setup #查看web1的綜合信息
#小測試一下,看看結果
[root@manager ~]# ansible web1 -m ping [WARNING]: The version of gmp you have installed has a known issue regarding timing vulnerabilities when used with pycrypto. If possible, you should update it (i.e. yum update gmp). 192.168.10.2 | success >> { "changed": false, "ping": "pong" }
#那麼ansible到底怎麼用呢,仍是上面已經寫了的過程(綠色標記的),ansible默認執行的是command,能夠省略,若是不是command,那就ansible-doc -l裏面查找要用的模塊命令,好比file。
那麼ansible web1 -m file 後面怎麼寫?
# ansible-doc file 查看file模塊的幫助,裏面有詳細的介紹,例:
file: path=/etc/foo.conf owner=foo group=foo mode=0644
#下面舉個栗子,ansible file的用法,前提是web1的/tmp下有file這個文件,而且web1有mysql用戶和組
# ansible web1 -m file -a "path=/tmp/file owner=mysql group=mysql mode=0755" 192.168.10.2 | success >> { "changed": true, "gid": 500, "group": "mysql", "mode": "0755", "owner": "mysql", "path": "/tmp/path", "size": 20, "state": "file", "uid": 500 }
#ansible可用的命令不少,全部的命令均可以這樣查看具體的用法
#下面只是一個簡單的發佈例子,或者說是思路,具體的根據公司本身狀況作改動
若是要作的比較完善,那麼需加入對輸入的路徑和文件作檢測,而且對線上正式機的備份作更詳細些,這個不支持回滾,若是更新出錯,須要到web服務器備份的文件夾backup再次覆蓋線上文件
[root@manager ~]# cat publish.sh #!/bin/bash # echo "please choose server web1 or web2: " echo "1:web1 2:web2 3:QUIT" read -p "\>" SERVER case $SERVER in 1) read -p "PATH:" DIR NEWDIR=`echo $DIR|sed 's/var/home/'` #發佈機的目錄是/home/www而不是/var/www,根據本身環境 ansible web1 -a "cp $NEWDIR /backup" #把web1上文件作一個備份,複製到/backup ansible web1 -m copy -a "src=$DIR dest=$NEWDIR" #把當前路徑的文件copy到web1上 ;; 2) read -p "PATH:" DIR ansible web2 -a "cp $DIR /backup" ansible web2 -m copy -a "src=$DIR dest=$DIR" ;; 3) exit ;; *) echo "you are wrong" esac