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

说明:

系统为CentOS6.5 

manager 192.168.10.1

web1 192.168.10.2

web2 192.168.10.3

安装ansible

1
2
3
4
5
6
7
8
9
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  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

# ansible -h    #ansible的用法,后面可加的参数,比如connection、sudo、module-name、timeout

# 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的综合信息

#小测试一下,看看结果

1
2
3
4
5
6
7
8
[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用户和组

1
2
3
4
5
6
7
8
9
10
11
12
# 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再次覆盖线上文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
[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


wKioL1S3tfnDKb-hAALxeq1txk4408.jpg










本文转自 bbotte 51CTO博客,原文链接:http://blog.51cto.com/bbotte/1604541,如需转载请自行联系原作者
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值