1、本文尝试着使用ansible部署tomcat,test为测试工程
roles如下:
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
|
[lansgg@node1 ansible_playbook]$
pwd
/opt/lansgg/ansible_playbook
[lansgg@node1 ansible_playbook]$
ls
hosts roles site.yml
[lansgg@node1 ansible_playbook]$ tree
.
├── hosts
├── roles
│ └── http
│ ├── default
│ ├── files
│ │ ├── apache-tomcat-7.0.68.
tar
.gz
│ │ ├── jdk-7u72-linux-x64.
tar
.gz
│ │ └── ROOT
│ │ └──
test
.html
│ ├── handlers
│ │ └── main.yml
│ ├── meta
│ │ └──
test
│ │ └──
test
.html
│ ├── tasks
│ │ ├── bak.yml
│ │ └── main.yml
│ ├── templates
│ │ └──
test
.yml
│ └── vars
└── site.yml
11 directories, 10 files
[lansgg@node1 ansible_playbook]$
|
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
25
26
|
[lansgg@node1 tasks]$
cat
main.yml
- name: 将jdk压缩包copy到远程主机 jdk-7u72-linux-x64.
tar
.gz
copy: src=jdk-7u72-linux-x64.
tar
.gz dest=
/opt/lansgg/
- name: 解压jdk压缩包 jdk-7u72-linux-x64.
tar
.gz
shell: chdir=
/opt/lansgg
tar
zxvf jdk-7u72-linux-x64.
tar
.gz
- name: 将tomcat压缩包copy到远程主机 apache-tomcat-7.0.68.
tar
.gz
copy: src=apache-tomcat-7.0.68.
tar
.gz dest=
/opt/lansgg
- name: 解压tomcat压缩包
shell: chdir=
/opt/lansgg
tar
zxvf apache-tomcat-7.0.68.
tar
.gz
- name: 将tomcat压缩包 重新命名为 tomcat
shell: chdir=
/opt/lansgg
mv
apache-tomcat-7.0.68 tomcat
- name: 将用户的 .bashrc 模板文件 copy 到远程主机
copy: src=.bashrc dest=`ansible_user_dir`/
- name: 将用户的 .bash_profile 模板文件 copy 到远程主机
copy: src=.bash_profile dest=`ansible_user_dir`/
- name: 首次启动tomcat
shell: chdir=`ansible_user_dir`
/tomcat/bin
nohup
.
/startup
.sh &
- name: 将
test
工程 copy 到远程主机
copy: src=
/opt/lansgg/ansible_playbook/roles/http/meta/test
dest=`ansible_user_dir`
/tomcat/webapps/
tags:
- updateconf
notify:
- stop tomcat service
- start tomcat service
- name: 将某些模板文件 copy 到远程主机
template: src=
test
.yml dest=`ansible_user_dir`
/tomcat/webapps/test/test
.html
|
handlers:
1
2
3
4
5
|
[lansgg@node1 handlers]$
more
main.yml
- name: stop tomcat service
shell:
"ps -ef |grep `ansible_user_dir`/tomcat |grep -v grep |awk '{print $2}' |xargs kill -9"
- name: start tomcat service
shell: chdir=`ansible_user_dir`
/tomcat/bin
nohup
.
/startup
.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
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts site.yml
PLAY [http] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.131]
TASK: [http | 将jdk压缩包copy到远程主机 jdk-7u72-linux-x64.
tar
.gz] ***
changed: [192.168.100.131]
TASK: [http | 解压jdk压缩包 jdk-7u72-linux-x64.
tar
.gz] *******************
changed: [192.168.100.131]
TASK: [http | 将tomcat压缩包copy到远程主机 apache-tomcat-7.0.68.
tar
.gz] ***
changed: [192.168.100.131]
TASK: [http | 解压tomcat压缩包] ******************************************
changed: [192.168.100.131]
TASK: [http | 将tomcat压缩包 重新命名为 tomcat] **********************
changed: [192.168.100.131]
TASK: [http | 将用户的 .bashrc 模板文件 copy 到远程主机] *********
changed: [192.168.100.131]
TASK: [http | 将用户的 .bash_profile 模板文件 copy 到远程主机] ***
changed: [192.168.100.131]
TASK: [http | 首次启动tomcat] *********************************************
changed: [192.168.100.131]
TASK: [http | 将
test
工程 copy 到远程主机] ***************************
changed: [192.168.100.131]
TASK: [http | 将某些模板文件 copy 到远程主机] *********************
changed: [192.168.100.131]
NOTIFIED: [http | stop tomcat service] ****************************************
changed: [192.168.100.131]
NOTIFIED: [http | start tomcat service] ***************************************
changed: [192.168.100.131]
PLAY RECAP ********************************************************************
192.168.100.131 : ok=13 changed=12 unreachable=0 failed=0
[lansgg@node1 ansible_playbook]$
|
核实测试主机:
1
2
3
4
5
|
[lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a
'ps -ef|grep tomcat |grep -v grep'
192.168.100.131 | success | rc=0 >>
lansgg 54034 1 6 13:09 ? 00:00:06
/opt/lansgg/jdk1
.7.0_72
/jre/bin/java
-Djava.util.logging.config.
file
=
/opt/lansgg/tomcat/conf/logging
.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.
dirs
=
/opt/lansgg/tomcat/endorsed
-classpath
/opt/lansgg/tomcat/bin/bootstrap
.jar:
/opt/lansgg/tomcat/bin/tomcat-juli
.jar -Dcatalina.base=
/opt/lansgg/tomcat
-Dcatalina.home=
/opt/lansgg/tomcat
-Djava.io.tmpdir=
/opt/lansgg/tomcat/temp
org.apache.catalina.startup.Bootstrap start
[lansgg@node1 ansible_playbook]$
|
1
2
3
4
5
|
[lansgg@node1 ansible_playbook]$ curl http:
//192
.168.100.131:8080
/test/test
.html
this is a
test
hostname
:v2.lansgg.com
abcdefg
[lansgg@node1 ansible_playbook]$
|
测试更新操作:
1.1、修改测试文件:
1
|
[lansgg@node1 ansible_playbook]$
echo
"abcdefg"
>> roles
/http/meta/test/test
.html
|
执行:
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
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts site.yml --tags=updateconf
PLAY [http] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.131]
TASK: [http | 将
test
工程 copy 到远程主机] ***************************
changed: [192.168.100.131]
NOTIFIED: [http | stop tomcat service] ****************************************
changed: [192.168.100.131]
NOTIFIED: [http | start tomcat service] ***************************************
changed: [192.168.100.131]
PLAY RECAP ********************************************************************
192.168.100.131 : ok=4 changed=3 unreachable=0 failed=0
[lansgg@node1 ansible_playbook]$
[lansgg@node1 ansible_playbook]$ curl http:
//192
.168.100.131:8080
/test/test
.html
this is a
test
hostname
:
hostname
1111111111111111111111111111
[lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a
'ps -ef|grep tomcat |grep -v grep'
192.168.100.131 | success | rc=0 >>
lansgg 54236 1 24 13:11 ? 00:00:05
/opt/lansgg/jdk1
.7.0_72
/jre/bin/java
-Djava.util.logging.config.
file
=
/opt/lansgg/tomcat/conf/logging
.properties -Djava.util.logging.manager=org.apache.juli.ClassLoaderLogManager -Djava.endorsed.
dirs
=
/opt/lansgg/tomcat/endorsed
-classpath
/opt/lansgg/tomcat/bin/bootstrap
.jar:
/opt/lansgg/tomcat/bin/tomcat-juli
.jar -Dcatalina.base=
/opt/lansgg/tomcat
-Dcatalina.home=
/opt/lansgg/tomcat
-Djava.io.tmpdir=
/opt/lansgg/tomcat/temp
org.apache.catalina.startup.Bootstrap start
[lansgg@node1 ansible_playbook]$
|
2、ansible 中的include 机制
测试目的,可以随意指定要操作的主机组 和 要操作的状态
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[lansgg@node1 ansible_playbook]$
pwd
/opt/lansgg/ansible_playbook
[lansgg@node1 ansible_playbook]$ tree
.
├── hosts
├── roles
│ └──
test
│ └── tasks
│ ├── del.yml
│ ├── main.yml
│ └──
touch
.yml
└──
test
.yml
3 directories, 5 files
[lansgg@node1 ansible_playbook]$
|
tasks:
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
|
[lansgg@node1 ansible_playbook]$
cat
roles
/test/tasks/touch
.yml
- name:
touch
file
shell:
touch
/tmp/123
[lansgg@node1 ansible_playbook]$
cat
roles
/test/tasks/del
.yml
- name: del
file
shell:
rm
/tmp/123
[lansgg@node1 ansible_playbook]$
cat
roles
/test/tasks/main
.yml
- name: include
touch
yml
include:
touch
.yml
when:
"state=='touch'"
- name: include del yml
include: del.yml
when:
"state=='del'"
[lansgg@node1 ansible_playbook]$
[lansgg@node1 ansible_playbook]$
cat
test
.yml
- hosts:
'`testhost`'
remote_user: lansgg
roles:
-
test
[lansgg@node1 ansible_playbook]$
cat
hosts
[http]
192.168.100.131
[db]
192.168.100.132
[lansgg@node1 ansible_playbook]$
|
执行并查看结果:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts
test
.yml --extra-vars
"testhost=http state=touch"
PLAY [http] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.131]
TASK: [
test
|
touch
file
] *****************************************************
changed: [192.168.100.131]
TASK: [
test
| del
file
] *******************************************************
skipping: [192.168.100.131]
PLAY RECAP ********************************************************************
192.168.100.131 : ok=2 changed=1 unreachable=0 failed=0
|
上面是创建文件,我们也可以删除操作
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts
test
.yml --extra-vars
"testhost=http state=del"
PLAY [http] *******************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.131]
TASK: [
test
|
touch
file
] *****************************************************
skipping: [192.168.100.131]
TASK: [
test
| del
file
] *******************************************************
changed: [192.168.100.131]
PLAY RECAP ********************************************************************
192.168.100.131 : ok=2 changed=1 unreachable=0 failed=0
[lansgg@node1 ansible_playbook]$ ansible -i hosts http -m shell -a
'ls -l /tmp/'
192.168.100.131 | success | rc=0 >>
total 0
[lansgg@node1 ansible_playbook]$
|
可以以命令式的方式控制要操作的主机及状态 (对 db 主机组操作)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts
test
.yml --extra-vars
"testhost=db state=touch"
PLAY [db] *********************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.132]
TASK: [
test
|
touch
file
] *****************************************************
changed: [192.168.100.132]
TASK: [
test
| del
file
] *******************************************************
skipping: [192.168.100.132]
PLAY RECAP ********************************************************************
192.168.100.132 : ok=2 changed=1 unreachable=0 failed=0
[lansgg@node1 ansible_playbook]$ ansible -i hosts db -m shell -a
'ls -l /tmp/'
192.168.100.132 | success | rc=0 >>
total 0
-rw-rw-r-- 1 lansgg lansgg 0 Mar 9 16:20 123
[lansgg@node1 ansible_playbook]$
|
删除操作:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
[lansgg@node1 ansible_playbook]$ ansible-playbook -i hosts
test
.yml --extra-vars
"testhost=db state=del"
PLAY [db] *********************************************************************
GATHERING FACTS ***************************************************************
ok: [192.168.100.132]
TASK: [
test
|
touch
file
] *****************************************************
skipping: [192.168.100.132]
TASK: [
test
| del
file
] *******************************************************
changed: [192.168.100.132]
PLAY RECAP ********************************************************************
192.168.100.132 : ok=2 changed=1 unreachable=0 failed=0
[lansgg@node1 ansible_playbook]$ ansible -i hosts db -m shell -a
'ls -l /tmp/'
192.168.100.132 | success | rc=0 >>
total 0
[lansgg@node1 ansible_playbook]$
|
本文转自 西索oO 51CTO博客,原文链接:http://blog.51cto.com/lansgg/1749179