假如你要在一台机器安装多个tomcat或者安装多台tomcat,你可以选择
假设现在tomcat有三个,分别为cxx,wxx,sxx
多建立对应的yml文件,一个项目一个
copy.yml,install.yml,delete.yml 分别一个
我选择第二种方法,变量可以定义在一个文件中
借用ansible生成动态的hosts优势
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
|
#!/usr/bin/python
# coding:utf-8
import
sqlite3
import
sys
try
:
import
json
except
ImportError:
import
simplejson as json
def
grouplist():
inventory
=
{}
inventory[
'local'
]
=
[
'127.0.0.1'
]
sfile
=
'/etc/ansible/books.txt'
with
open
(sfile,
'rb'
) as f:
for
i
in
f.readlines():
group
=
i.strip().split()[
0
]
name
=
i.strip().split()[
1
]
if
not
group
in
inventory:
inventory[group]
=
{
'hosts'
: []
}
inventory[group][
'hosts'
].append(name)
print
json.dumps(inventory, indent
=
4
)
def
hostinfo(name):
vars
=
{}
vars
=
{
'admin'
:
'Jane Jolie'
,
'datacenter'
:
1
}
print
json.dumps(
vars
, indent
=
4
)
if
__name__
=
=
'__main__'
:
if
len
(sys.argv)
=
=
2
and
(sys.argv[
1
]
=
=
'--list'
):
grouplist()
elif
len
(sys.argv)
=
=
3
and
(sys.argv[
1
]
=
=
'--host'
):
hostinfo(sys.argv[
2
])
else
:
print
"Usage: %s --list or --host <hostname>"
%
sys.argv[
0
]
sys.exit(
1
)
|
这个配置文件,指明端口和分组。cxx就是分组,后面的端口,你懂的。
1
2
3
4
|
cat books.txt
cxx
192.168
.
1.3
sc_tomcat
8005
8080
wxx
192.168
.
1.3
sc_tomcat
8006
8081
sxx
192.168
.
1.3
sc_tomcat
8007
8082
|
先来说ansible部分吧
目录结构
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
|
[root@localhost roles]
# tree
.
├── copy
│ ├── default
│ ├── files
│ │ └── ROOT
│ │ └── test.html
│ ├── meta
│ │ └── test
│ │ └── test.html
│ └── tasks
│ ├── copy.yml
│ ├── delete.yml
│ ├── install.yml
│ ├── main.bak
│ ├── main.yml
│ ├── main.yml.v1
│ └── main.yml.v2
├── delete
│ ├── default
│ ├── files
│ │ └── ROOT
│ │ └── test.html
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └── test
│ │ └── test.html
│ ├── tasks
│ │ ├── copy.yml
│ │ ├── delete.yml
│ │ ├── install.yml
│ │ ├── main.bak
│ │ ├── main.yml
│ │ ├── main.yml.v1
│ │ └── main.yml.v2
│ ├── templates
│ │ ├── server.xml
│ │ └── tomcat.sh
│ └──
vars
│ └── main.yml
└── install
├── default
├── files
│ └── ROOT
│ └── test.html
├── handlers
│ └── main.yml
├── meta
│ └── test
│ └── test.html
├── tasks
│ ├── copy.yml
│ ├── delete.yml
│ ├── install.yml
│ ├── main.bak
│ ├── main.yml
│ ├── main.yml.v1
│ └── main.yml.v2
├── templates
│ ├── server.xml
│ └── tomcat.sh
└──
vars
└── main.yml
|
对比以前的变化是
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
|
cd
/
etc
/
ansible
/
tomcat
/
roles
install中的install.yml 文件改变了
cd
/
etc
/
ansible
/
tomcat
/
roles
/
install
/
tasks
[root@localhost tasks]
# cat install.yml
-
name: pro
file
: path
=
/
opt
/
apps
/
`cxx_pro_dir` state
=
directory
-
name: tar
shell: chdir
=
/
opt
/
ea rm
-
rf `cxx_dir` && cp
-
r tomcat `cxx_dir`
-
name: copy server.xml
template: src
=
server.xml dest
=
/
opt
/
ea
/
`cxx_dir`
/
conf
/
force
=
yes mode
=
0644
-
name: rm webapps
file
: dest
=
/
opt
/
ea
/
`cxx_dir`
/
webapps
/
state
=
absent
-
name: copy tomcat.sh
copy: src
=
/
opt
/
src
/
tomcat.sh dest
=
/
opt
/
ea
/
`cxx_pro_dir`.sh owner
=
tomcat group
=
tomcat
-
name: modify tomcat.sh
shell: chdir
=
/
opt
/
ea sed
-
i
's%/ea/tomcat%/ea/`cxx_dir`%g'
`cxx_pro_dir`.sh
-
name: modify
file
: path
=
/
opt
/
ea
/
`cxx_dir`
/
owner
=
tomcat group
=
tomcat mode
=
0755
-
name: chown
file
: path
=
/
opt
/
ea
/
`cxx_dir`
/
state
=
directory recurse
=
yes owner
=
tomcat group
=
tomcat
-
name: 首次启动tomcat
shell: cd
/
opt
/
ea && source
/
etc
/
profile && sudo
-
i
-
u tomcat nohup sh `cxx_pro_dir`.sh start &
-
name: copy test
copy: src
=
/
etc
/
ansible
/
tomcat
/
roles
/
install
/
meta
/
test dest
=
/
opt
/
apps
/
`cxx_pro_dir`
/
|
1
2
3
4
5
6
7
|
cd
/
etc
/
ansible
/
tomcat
/
roles
/
install
/
vars
变量文件有大改变
说明cxx_pro_dir 也可以指明wxx_pro_dir,你可以把它当做变量,上面的install.yml引用这个变量而已。
[root@localhost
vars
]
# cat main.yml
cxx_pro_dir:
"{{ cxx_pro_dir }}"
cxx_dir:
"{{ cxx_dir }}"
cxx_port1:
"{{ cxx_port1 }}"
cxx_port2:
"{{ cxx_port2 }}"
|
ansible部分完结。
因为有好多主机,不想手动书写ansible执行文件,可以用python脚本自动生成
主要想自动生成这样的脚本
ansible-playbook -i /etc/ansible/test.py copy.yml --extra-vars "host=192.168.1.3"
ansible-playbook -i /etc/ansible/test.py install.yml --extra-vars "host=192.168.1.3 cxx_pro_dir=cxx cxx_dir=cxx_tomcat cxx_port1=8005 cxx_port2=8080"
以下python脚本,读取生成的
声明 什么cxx都是变量,你可以随便取名字
1
2
3
4
5
6
7
8
9
10
11
|
# !/usr/bin/env python
# coding:utf-8
#import sqlite3
import
sys
try
:
import
json
except
ImportError:
import
simplejson as json
dfile
=
'
/
etc
/
ansible
/
ansible.sh
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
|
'''
cxx 172.29.1.3 sc_tomcat 8005 8080
'''
def
ansiblec(host,cxx_pro_dir
=
None
,cxx_dir
=
None
,cxx_port1
=
None
,cxx_port2
=
None
):
if
cxx_port1
=
=
None
:
stringa
=
'ansible-playbook -i /etc/ansible/test.py copy.yml --extra-vars "host=%s"'
%
(host)
return
stringa
else
:
stringb
=
'ansible-playbook -i /etc/ansible/test.py install.yml --extra-vars "host=%s cxx_pro_dir=%s cxx_dir=%s cxx_port1=%s cxx_port2=%s"'
\
%
(host,cxx_pro_dir,cxx_dir,cxx_port1,cxx_port2)
return
stringb
def
grouplist():
inventory
=
{}
sfile
=
'/etc/ansible/books.txt'
with
open
(sfile,
'rb'
) as f:
iplist
=
[]
glist
=
[]
for
i
in
f.readlines():
group
=
i.strip().split()[
0
]
grouptomcat
=
group
+
"_"
+
"tomcat"
name
=
i.strip().split()[
1
]
cxx_port1
=
i.strip().split()[
3
]
cxx_port2
=
i.strip().split()[
4
]
iplist.append(ansiblec(name))
glist.append(ansiblec(name,group,grouptomcat,cxx_port1,cxx_port2))
# if not group in inventory:
# inventory[group] = {
# 'hosts': []
# }
# inventory[group]['hosts'].append(name)
# for ip in inventory.iteritems():
# ipt= ip[1]['hosts'][0]
ipset
=
set
(iplist)
gset
=
set
(glist)
# print json.dumps(inventory, indent=4)
with
open
(dfile,
'wb'
) as f1:
for
i
in
ipset:
line
=
str
(i)
+
"\n"
f1.write(line)
for
i
in
gset:
line
=
str
(i)
+
"\n"
f1.write(line)
f1.close()
def
hostinfo(name):
vars
=
{}
vars
=
{
'admin'
:
'Jane Jolie'
,
'datacenter'
:
1
}
print
json.dumps(
vars
, indent
=
4
)
if
__name__
=
=
'__main__'
:
grouplist()
|
直接执行这个脚本后,自动生成
1
|
/
etc
/
ansible
/
ansible.sh
|
里面包含就是
ansible-playbook -i /etc/ansible/test.py copy.yml --extra-vars "host=192.168.1.3"
ansible-playbook -i /etc/ansible/test.py install.yml --extra-vars "host=192.168.1.3 cxx_pro_dir=cxx cxx_dir=cxx_tomcat cxx_port1=8005 cxx_port2=8080"
这样,通过两个python脚本,只需要一个配置文件,写明端口和应用名称,自动生成创建脚本。
剩余没有解决的问题
1.root权限,ansible最好不要用root权限
2.context.xml没有加入到模板里面去,主要做集群,可以自己添加
3.lib包,比如集群包没有,主要做session共享的集群包
4.没有自动启动脚本,这个需要写下tomcat的启动脚本
这是自己个人的想法,比以前快点。
本文转自 liqius 51CTO博客,原文链接:http://blog.51cto.com/szgb17/1925198,如需转载请自行联系原作者