$ grep SBX list.csv
SBX,FET,1.fet.sbx,03 04 05 13 14 15
SBX,BET,2.bet.sbx,03 04 06 07 08 09 13 14 15 16 17 18 19 22
SBX,BET,3.bet.sbx,00 01 10 23 24 25 26 28 31 32 34 35 36 37,add 31 as AQUA_11
SBX,REST,3.fet.sbx,11 12
SBX,REST,4.fet.sbx,11 12
SBX,REST_SB,4.fet.sbx,31 32
如上所示,需要拷贝2个文件到对于host上各个instance下,e.g 2.bet.stg:/home/zapp/apps/11/webapps/apps/WEB-INF/scripts/clone_tenant/ #11是ID,当机器是REST的时候目录应该是 /home/zapp/apps/11/webapps/rest/WEB-INF/scripts/clone_tenant/
环境分STG/STG2/SBX/PROD. etc.
awk -F \, '/SBX/{
if($NF~/[a-zA-Z]+/) NF=NF-1 # add 31 as AQUA_11 为注释,需要去掉.
for(i=1;i<=split($NF,m," ");i++)if($2!="REST")print "sudo scp /tmp/utils.py /tmp/clone_tenant.py "$3":/home/zapp/apps/"""m[i]"/webapps/apps/WEB-INF/scripts/clone_tenant/ ; ssh -tt """$3" \042sudo chown -R zapp:zapp /home/zapp/apps/"""m[i]"/webapps/apps/WEB-INF;sudo chmod -R 755 /home/zapp/apps/"""m[i]"/webapps/apps/WEB-INF\042 /dev/null"
else print "sudo scp /tmp/utils.py /tmp/clone_tenant.py "$3":/home/zapp/apps/"""m[i]"/webapps/rest/WEB-INF/scripts/clone_tenant/ ;ssh -tt """$3" \042sudo chown -R zapp:zapp /home/zapp/apps/"""m[i]"/webapps/rest/WEB-INF;sudo chmod -R 755 /home/zapp/apps/"""m[i]"/webapps/rest/WEB-INF\042 /dev/null"
}' list.csv |bash
# |bash之前的结果:
sudo scp /tmp/utils.py /tmp/clone_tenant.py 2.fet.stg:/home/zapp/apps/11/webapps/rest/WEB-INF/scripts/clone_tenant/ ;ssh -tt 2.fet.stg "sudo chown -R zapp:zapp /home/zapp/apps/11/webapps/rest/WEB-INF;sudo chmod -R 755 /home/zapp/apps/11/webapps/rest/WEB-INF" /dev/null
若list.csv的维护标准不变,类似问题可以更改匹配/STG2/来处理STG2, 其他环境类似。
如果用Ansible来实现,整个代码会稍微好看点,:)
---
- hosts: all
gather_facts: False
vars:
owner: zapp
group: zapp
tasks:
- name: check ......
shell: ls /home/zapp/apps/*/webapps/*/WEB-INF/scripts/clone_tenant |grep -E '[apps|rest]/WEB-INF/scripts'
register: stat_result
- debug: var=stat_result.stdout
- name: scp files...
action: copy src=/tmp/{{ item[1]}} dest={{ item[0] }}/ owner={{ owner }} group={{ group }} mode=0755
with_nested:
- "{{ stat_result.stdout_lines |replace(':', '') }}" # 因为ls 文件用了通配符,结果是 /XXX/scripts/clone_tenant: ,多了个冒号,所以替换一下。
- [ 'utils.py', 'clone_tenant.py' ]
Ansible官方文档写的太差了,难怪火不起来,语法晦涩,社区寥寥无几。也就满足一下ad-hoc之类的任务。多数情况还能for ... ;ssh 来替换。建议了解一下即可不要深究,跟Puppet比差太远。
阅读(4634) | 评论(0) | 转发(0) |