243123221 发表于 2014-7-29 11:41:20

运维自动化之ansible playbook安装apache

现在介绍如何使用ansible安装apache。下面是安装apache的信息:

1
2
3
4
5
6
7
8
9
10
apr_version: 1.5.0
apr_util_version: 1.5.3
libiconv_version: 1.14
apache_version: 2.4.7
apache_web_dir: /data/webroot/apache
apache_log: /data/webroot/apache/logs
apache_vhost: /data/webroot/apache/vhost
apache_port: 80
apache_user: www
serveradmin: denglei@chukong-inc.com




可以看到apache的版本是2.4.7
备注:此playbook仅能对centos或者redhat的6.x版本进行安装。下面是安装apache的playbook结构

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
apache_delete
├── files
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── delete.yml
│   └── main.yml
├── templates
└── vars
    └── main.yml
apache_install
├── files
│   ├── httpd-2.4.7.tar.gz
│   └── libiconv.tar.gz
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── copy.yml
│   ├── delete.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   ├── httpd
│   ├── httpd.conf
│   ├── index.html
│   ├── index.php
│   └── vhost.conf
└── vars
    └── main.yml

12 directories, 17 files




playbook安装apache的是:


1
2
3
4
5
6
7
8
9
09:09:35 # cat apache_install.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - common
    - pcre_install
    - apache_install




playbook删除apache的是:

1
2
3
4
5
6
7
09:09:56 # cat apache_delete.yml
---
- hosts: "{{host}}"
remote_user: "{{user}}"
gather_facts: True
roles:
    - apache_delete




1、安装apache


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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
09:05:59 # time ansible-playbook apache_install.yml --extra-vars "host=192.168.240.13 user=root" --private-key=/root/test.pem

PLAY *********************************************************

GATHERING FACTS ***************************************************************
ok:

TASK: ***********************
changed:

TASK: ********************
changed:

TASK: ***********
changed:

TASK: ******************
changed:

TASK: ****************
changed: => (item=httpd-2.4.7.tar.gz)
changed: => (item=libiconv.tar.gz)

TASK: ******************
changed:

TASK: *******
changed:

TASK: ******************
changed:

TASK: ************
changed:

TASK: ***
changed:

TASK: *******************************
ok:

TASK: ******************
changed:

TASK: ****************
changed:

TASK: *****************
failed: => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf ", "delta": "0:00:00.005372", "end": "2014-07-27 21:07:58.416717", "item": "", "rc": 1, "start": "2014-07-27 21:07:58.411345"}
stdout: 0
...ignoring

TASK: ****************
changed:

TASK: ************************************
changed: => (item=/data/webroot/apache)
changed: => (item=/data/webroot/apache/logs)
changed: => (item=/data/webroot/apache/vhost)

TASK: ****************
changed:

TASK: *******************
changed:

TASK: ****************
changed:

TASK: *******
changed:

TASK: ***
changed:

PLAY RECAP ********************************************************************
192.168.240.13             : ok=22   changed=20   unreachable=0    failed=0   


real    0m47.364s
user    0m3.823s
sys 0m0.402s




可以看到47秒就安装完成(默认的common模块是安装基础的yum依赖库,这个我一般初始化安装完成,所以这个地方没有浪费时间,建议大家也都在系统默认安装完成后,初始化依赖库)
2、安装后测试

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
# ps -ef|grep httpd
root      8375   10 21:08 ?      00:00:00 /usr/local/httpd-2.4.7/bin/httpd -k start
www       837783750 21:08 ?      00:00:00 /usr/local/httpd-2.4.7/bin/httpd -k start
www       837883750 21:08 ?      00:00:00 /usr/local/httpd-2.4.7/bin/httpd -k start
www       837983750 21:08 ?      00:00:00 /usr/local/httpd-2.4.7/bin/httpd -k start
www       838083750 21:08 ?      00:00:00 /usr/local/httpd-2.4.7/bin/httpd -k start
root      856966660 21:11 pts/1    00:00:00 grep httpd
# ll /usr/local/
total 56
drwxr-xr-x.2 root root 4096 Sep 232011 bin
drwxr-xr-x.2 root root 4096 Sep 232011 etc
drwxr-xr-x.2 root root 4096 Sep 232011 games
drwxr-xr-x15 root root 4096 Jul1 05:18 httpd-2.4.7
drwxr-xr-x.2 root root 4096 Sep 232011 include
drwxr-xr-x.2 root root 4096 Jul 27 21:07 lib
drwxr-xr-x.2 root root 4096 Sep 232011 lib64
drwxr-xr-x.2 root root 4096 Sep 232011 libexec
drwxr-xr-x   6 root root 4096 Jun 23 05:38 pcre-8.33
drwxr-xr-x10 root root 4096 Jul 22 23:33 proftpd-1.3.4d
drwxr-xr-x.2 root root 4096 Sep 232011 sbin
drwxr-xr-x.5 root root 4096 May 122013 share
drwxr-xr-x.3 root root 4096 May 122013 src
drwxr-xr-x   6 root root 4096 Jul 24 05:41 vpsmate
# curl 10.10.240.20
Apache 2.4.7 in 10.10.240.20is success!
# curl 10.10.240.20/index.php
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>503 Service Unavailable</title>
</head><body>
<h1>Service Unavailable</h1>
<p>The server is temporarily unable to service your
request due to maintenance downtime or capacity
problems. Please try again later.</p>
</body></html>
# curl -I 10.10.240.20
HTTP/1.1 200 OK
Date: Mon, 28 Jul 2014 01:13:13 GMT
Server: Tengine/1.5.2
Last-Modified: Mon, 28 Jul 2014 01:08:01 GMT
ETag: "2a-4ff368df22a0b"
Accept-Ranges: bytes
Content-Length: 42
Content-Type: text/html




如果打开php界面的原因是没有安装php,安装的话都成功了并运行正常。
3、删除apache

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
09:16:38 # time ansible-playbook apache_delete.yml --extra-vars "host=192.168.240.13 user=root" --private-key=/root/test.pem

PLAY *********************************************************

GATHERING FACTS ***************************************************************
ok:

TASK: *******************
changed:

TASK: ********************
changed:

TASK: ********************
changed:

TASK: *********
changed:

TASK: ************************************
changed:

PLAY RECAP ********************************************************************
192.168.240.13             : ok=6    changed=5    unreachable=0    failed=0   


real    0m19.803s
user    0m0.665s
sys 0m0.104s




4、删除后测试

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# ps -ef|grep http
root   1135366660 21:17 pts/1    00:00:00 grep http
# ll /usr/local/
total 52
drwxr-xr-x.2 root root 4096 Sep 232011 bin
drwxr-xr-x.2 root root 4096 Sep 232011 etc
drwxr-xr-x.2 root root 4096 Sep 232011 games
drwxr-xr-x.2 root root 4096 Sep 232011 include
drwxr-xr-x.2 root root 4096 Jul 27 21:16 lib
drwxr-xr-x.2 root root 4096 Sep 232011 lib64
drwxr-xr-x.2 root root 4096 Sep 232011 libexec
drwxr-xr-x   6 root root 4096 Jun 23 05:38 pcre-8.33
drwxr-xr-x10 root root 4096 Jul 22 23:33 proftpd-1.3.4d
drwxr-xr-x.2 root root 4096 Sep 232011 sbin
drwxr-xr-x.5 root root 4096 May 122013 share
drwxr-xr-x.3 root root 4096 May 122013 src
drwxr-xr-x   6 root root 4096 Jul 24 05:41 vpsmate
# id www
id: www: No such user
# curl 10.10.240.20 -I
curl: (7) couldn't connect to host




如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/apache_install),然后放到/etc/ansible目录里,下面是内容
如果觉得好,给个赞并多评论,谢谢。
页: [1]
查看完整版本: 运维自动化之ansible playbook安装apache