kyjh 发表于 2015-1-16 09:12:03

利用ansible做线上的简单更新发布

说明:
系统为CentOS6.5
manager 192.168.10.1
web1 192.168.10.2
web2 192.168.10.3
安装ansible

wget http://mirrors.zju.edu.cn/epel/6/x86_64/epel-release-6-8.noarch.rpm
rpm -ivh epel-release-6-8.noarch.rpm
#双机互信
# ssh-keygen -t rsa -f ~/.ssh/id_rsa -P ''
for i in web1 web2;do ssh-copy-id -i root@$i;done

yum install ansible
# ansible --version
ansible 1.8.2


ansible命令
ansible         ansible-galaxy    ansible-pull      ansible-vault   
ansible-doc       ansible-playbookansible-shell
# ansible-doc -l查看modules
# ansible-doc ping 查看ping模块的使用方法
> 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
#测试


1
2
3
4
5
6
7
8
# ansible web1 -m ping
: 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"
}




#下面开始
cd /etc/ansible/
vim hosts #注释所有,添加以下

192.168.10.2

192.168.10.3
#测试几个常用的命令

ansible web1 -a uptime
ansible web1 -m yum -a "name=dstat state=latest"
ansible web1 -m raw -a "rpm -qa|grep dstat"
ansible web1 -m shell -a "service mysqld restart"
ansible web1 -m service -a "name=mysqld state=stopped"

#下面只是一个简单的例子,或者说是思路,具体的根据公司自己情况做改动


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# 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"   #做一个备份
ansible web1 -m copy -a "src=$DIR dest=$NEWDIR"
;;
2)
read -p "PATH:" DIR
ansible web2 -a "cp $NEWDIR /backup"
ansible web2 -m copy -a "src=$DIR dest=$NEWDIR"
;;
3)
exit
;;
*)
echo "you are wrong"
esac



页: [1]
查看完整版本: 利用ansible做线上的简单更新发布