目前公司网站前台有16台,每次想更新个文件,手动更新真的很让人抓狂。现在就借助ansible的copy模块轻松实现批量分发,并自动备份,效果还不错,拿来分享给大家。

1.前提要安装好ansible且要与前台16台服务器,做好ssh信任,并归类到一组如v2_51auto_www。

# cat /etc/ansible/hosts

[v1_51auto_www]
172.31.2.68 ansible_ssh_port=22
172.31.2.69 ansible_ssh_port=22
172.31.2.70 ansible_ssh_port=22
172.31.2.71 ansible_ssh_port=22
172.31.2.72 ansible_ssh_port=22
172.16.1.20
172.16.1.21
172.16.1.22
172.16.1.23
172.16.1.24
[v2_51auto_www]
172.16.1.11
172.16.1.12
172.16.1.13
172.16.1.14
172.16.1.19
172.16.1.34

2.把要更新的文件上传到ansible服务器,我的目录是/home/maser/upload

[master@localhost upload]$ pwd
/home/master/upload
[master@localhost upload]$ ls
list.jsp  ParamUtils.class  TempUtil.class

3.ansible命令分发

[master@localhost upload]$ ansible v2_51auto_www -s -m copy -a 'src=/home/master/upload/list.jsp dest=/opt/websuite/resin/data/webapps/ROOT/views/car/promotion owner=websuite group=websuite backup=yes'

4.验证效果

[root@s01 promotion]# ifconfig
eth0      Link encap:Ethernet  HWaddr AE:FC:BB:33:18:41  
          inet addr:172.16.1.11  Bcast:172.16.1.255  Mask:255.255.255.0
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:54593130 errors:0 dropped:0 overruns:0 frame:0
          TX packets:42947200 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000
          RX bytes:141150153477 (131.4 GiB)  TX bytes:114151578105 (106.3 GiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:13401 errors:0 dropped:0 overruns:0 frame:0
          TX packets:13401 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0
          RX bytes:826668 (807.2 KiB)  TX bytes:826668 (807.2 KiB)

[root@s01 promotion]# pwd
/opt/websuite/resin/data/webapps/ROOT/views/car/promotion
[root@s01 promotion]# ll
总用量 112
-rw-r--r-- 1 websuite websuite 22203 5月  13 14:27 activity.jsp
-rw-r--r-- 1 websuite websuite 42599 5月  20 17:34 list.jsp
-rw-r--r-- 1 root     root     42598 5月  19 17:25 list.jsp.2015-05-20@17:34:12~