下面介绍使用ansible playbook安装lamp环境

下面是apache的安装信息:

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: [email protected]

下面是php的安装信息:

php_user: www
php_port: 9000
php_dir: /usr/local
php_version: 5.3.10
mysql_basedir: /data/mysql/basedir

下面是mysql的安装信息:

mysql_basedir: /data/mysql/basedir
mysql_datadir: /data/mysql/datadir
mysql_user: mysql
mysql_database_user: root
mysql_passwd: 'E4yR3WnoluSFTCBAI'
mysql_port: 3306
mysql_sock: /data/mysql/datadir/mysql.sock
mysql_charset: utf8
mysql_collation: utf8_general_ci
mysql_version: Percona-Server-5.5.21-rel25.1.tar.gz

下面是lamp的playbook的结构

09:09:17 # tree apache_* mysql_* php_*
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
mysql_delete
├── files
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── delete.yml
│   └── main.yml
├── templates
└── vars
    └── main.yml
mysql_install
├── files
│   └── mysql.tar.gz
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── copy.yml
│   ├── delete.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   ├── install_mysql.sh
│   ├── my.cnf
│   ├── mysqld
│   └── mysql_security.sh
└── vars
    └── main.yml
php_delete
├── files
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── delete.yml
│   └── main.yml
├── templates
│   └── delete_php.sh
└── vars
    └── main.yml
php_install
├── files
│   └── php.tar.gz
├── handlers
├── meta
│   └── main.yml
├── tasks
│   ├── copy.yml
│   ├── delete.yml
│   ├── install.yml
│   └── main.yml
├── templates
│   ├── install_php.sh
│   ├── php-fpm
│   └── php-fpm.conf
└── vars
    └── main.yml

36 directories, 47 files

playbook的lamp安装内容是:

09:10:15 # cat lamp_install.yml 
---
- hosts: "`host`"
  remote_user: "`user`"
  gather_facts: True
  roles:
    - common
    - pcre_install
    - apache_install
    - mysql_install
    - php_install

playbook的lamp删除的内容是:

09:10:58 # cat lamp_delete.yml 
---
- hosts: "`host`"
  remote_user: "`user`"
  gather_facts: True
  roles:
    - mysql_delete
    - apache_delete
    - php_delete
    - pcre_delete

1、安装lamp

09:27:09 # time ansible-playbook lamp_install.yml --extra-vars "host=192.168.240.11 user=root" --private-key=/root/test.pem

PLAY [192.168.240.11] ********************************************************* 

GATHERING FACTS *************************************************************** 
ok: [192.168.240.11]

TASK: [common | Install initializtion require software] *********************** 
changed: [192.168.240.11]

TASK: [pcre_install | Copy Pcre Software To Redhat Client] ******************** 
changed: [192.168.240.11]

TASK: [pcre_install | Uncompression Pcre Software In Redhat Client] *********** 
changed: [192.168.240.11]

TASK: [pcre_install | Delete Pcre Software In Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [apache_install | Copy Apache Software To Redhat Client] **************** 
changed: [192.168.240.11] => (item=httpd-2.4.7.tar.gz)
changed: [192.168.240.11] => (item=libiconv.tar.gz)

TASK: [apache_install | Create Apache User In Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [apache_install | Uncompression Apache Software To Redhat Client] ******* 
changed: [192.168.240.11]

TASK: [apache_install | Copy Apache Config To Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [apache_install | Copy Apache Vhost Config To Redhat Client] ************ 
changed: [192.168.240.11]

TASK: [apache_install | Copy Apache Start Service Script  To Redhat Client] *** 
changed: [192.168.240.11]

TASK: [apache_install | Create Lib Install Dir] ******************************* 
ok: [192.168.240.11]

TASK: [apache_install | Check Apache Iconv In Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [apache_install | Install Apache Iconv In Redhat Client] **************** 
changed: [192.168.240.11]

TASK: [apache_install | Check Lib In Config In Redhat Client] ***************** 
failed: [192.168.240.11] => {"changed": true, "cmd": "grep -c /usr/local/lib/ /etc/ld.so.conf ", "delta": "0:00:00.006510", "end": "2014-07-30 21:45:19.036594", "item": "", "rc": 1, "start": "2014-07-30 21:45:19.030084"}
stdout: 0
...ignoring

TASK: [apache_install | Install Apache Iconv In Redhat Client] **************** 
changed: [192.168.240.11]

TASK: [apache_install | Create Apache Dir] ************************************ 
changed: [192.168.240.11] => (item=/data/webroot/apache)
changed: [192.168.240.11] => (item=/data/webroot/apache/logs)
changed: [192.168.240.11] => (item=/data/webroot/apache/vhost)

TASK: [apache_install | Install Check Script In Redhat Client] **************** 
changed: [192.168.240.11]

TASK: [apache_install | Create Index Html To Redhat Client] ******************* 
changed: [192.168.240.11]

TASK: [apache_install | Start Apache Service In Redhat Client] **************** 
changed: [192.168.240.11]

TASK: [apache_install | Add Boot Start Apache Service In Redhat Client] ******* 
changed: [192.168.240.11]

TASK: [apache_install | Delete Apache compression Software In Redhat Client] *** 
changed: [192.168.240.11]

TASK: [mysql_install | Copy Mysql Software To Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [mysql_install | Create Mysql User In Redhat Client] ******************** 
changed: [192.168.240.11]

TASK: [mysql_install | Copy Mysql Start Script To Redhat Client] ************** 
changed: [192.168.240.11]

TASK: [mysql_install | Copy Install Mysql Script  To Redhat Client] *********** 
changed: [192.168.240.11]

TASK: [mysql_install | Copy Mysql Config To Redhat Client] ******************** 
changed: [192.168.240.11]

TASK: [mysql_install | Copy Mysql Security Script To Redhat Client] *********** 
changed: [192.168.240.11]

TASK: [mysql_install | Create Mysql Install Dir] ****************************** 
ok: [192.168.240.11]

TASK: [mysql_install | Uncompression Mysql Software To Redhat Client] ********* 
changed: [192.168.240.11]

TASK: [mysql_install | Modify Mysql Dir Permission In Redhat Client] ********** 
ok: [192.168.240.11] => (item=/data/mysql/datadir)
ok: [192.168.240.11] => (item=/data/mysql/basedir)

TASK: [mysql_install | Install Mysql Script In Redhat Client] ***************** 
changed: [192.168.240.11]

TASK: [mysql_install | Start Myql Security Script In Redhat Client] *********** 
changed: [192.168.240.11]

TASK: [mysql_install | Add Boot Start Mysql Service In Redhat Client] ********* 
changed: [192.168.240.11]

TASK: [mysql_install | Delete Mysql compression Software In Redhat Client] **** 
changed: [192.168.240.11]

TASK: [php_install | Copy Php Software To Redhat Client] ********************** 
changed: [192.168.240.11]

TASK: [php_install | Uncompression Php Software To Redhat Client] ************* 
changed: [192.168.240.11]

TASK: [php_install | Copy Php Start Script To Redhat Client] ****************** 
changed: [192.168.240.11]

TASK: [php_install | Copy Php Config To Redhat Client] ************************ 
changed: [192.168.240.11]

TASK: [php_install | Copy Php Install Script To Redhat Client] **************** 
changed: [192.168.240.11]

TASK: [php_install | Create Php User In Redhat Client] ************************ 
ok: [192.168.240.11]

TASK: [php_install | Install Php Script In Redhat Client] ********************* 
changed: [192.168.240.11]

TASK: [php_install | Start Php Service In Redhat Client] ********************** 
changed: [192.168.240.11]

TASK: [php_install | Add Boot Start Php Service In Redhat Client] ************* 
changed: [192.168.240.11]

TASK: [php_install | Delete Php compression Software In Redhat Client] ******** 
changed: [192.168.240.11]

PLAY RECAP ******************************************************************** 
192.168.240.11             : ok=45   changed=40   unreachable=0    failed=0   


real	4m42.365s
user	0m39.984s
sys	0m2.872s

2、安装后测试

[root@ip-10-10-240-14 tmp]# ps -ef|grep mysql|wc -l
3
[root@ip-10-10-240-14 tmp]# ps -ef|grep http|wc -l
6
[root@ip-10-10-240-14 tmp]# ps -ef|grep php|wc -l
130
[root@ip-10-10-240-14 tmp]# ll /data/webroot/apache/
logs/  vhost/ 
[root@ip-10-10-240-14 tmp]# ll /data/webroot/apache/vhost/
total 8
-rw-r--r-- 1 www www  42 Jul 30 21:45 index.html
-rw-r--r-- 1 www www 174 Jul 30 21:45 index.php
[root@ip-10-10-240-14 tmp]# cat /data/webroot/apache/
logs/  vhost/ 
[root@ip-10-10-240-14 tmp]# cat /data/webroot/apache/
logs/  vhost/ 
[root@ip-10-10-240-14 tmp]# cat /data/webroot/apache/vhost/index.html 
Apache 2.4.7 in 10.10.240.14  is success!
[root@ip-10-10-240-14 tmp]# cat /data/webroot/apache/vhost/index.php 

[root@ip-10-10-240-14 tmp]# curl 10.10.240.14/index.html
Apache 2.4.7 in 10.10.240.14  is success!
[root@ip-10-10-240-14 tmp]# curl 10.10.240.14/index.php
Successs!!!Php install success!

可以看到安装没问题

3、删除

09:49:03 # time ansible-playbook lamp_delete.yml --extra-vars "host=192.168.240.11 user=root" --private-key=/root/test.pem

PLAY [192.168.240.11] ********************************************************* 

GATHERING FACTS *************************************************************** 
ok: [192.168.240.11]

TASK: [mysql_delete | Stop Mysql Service] ************************************* 
changed: [192.168.240.11]

TASK: [mysql_delete | Delete Mysql Boot Start Script] ************************* 
changed: [192.168.240.11]

TASK: [mysql_delete | Delete Mysql Dir And Socket] **************************** 
changed: [192.168.240.11]

TASK: [mysql_delete | Delete Mysql User] ************************************** 
changed: [192.168.240.11]

TASK: [mysql_delete | Delete Mysql Service Start Script] ********************** 
changed: [192.168.240.11]

TASK: [apache_delete | Stop Httpd Service In RedHat Client] ******************* 
changed: [192.168.240.11]

TASK: [apache_delete | Delete Boot Start In RedHat Client] ******************** 
changed: [192.168.240.11]

TASK: [apache_delete | Delete Apache Dir In RedHat Client] ******************** 
changed: [192.168.240.11]

TASK: [apache_delete | Delete Apache Service Script In RedHat Client] ********* 
changed: [192.168.240.11]

TASK: [apache_delete | Delete Apache User] ************************************ 
failed: [192.168.240.11] => {"changed": true, "cmd": "userdel www ", "delta": "0:00:00.013968", "end": "2014-07-30 22:00:24.245309", "item": "", "rc": 8, "start": "2014-07-30 22:00:24.231341"}
stderr: userdel: user www is currently logged in
...ignoring

TASK: [php_delete | Copy Delete Php Script To RedHat Client] ****************** 
changed: [192.168.240.11]

TASK: [php_delete | Run Delete Php Script In RedHat Client] ******************* 
changed: [192.168.240.11]

TASK: [php_delete | Delete Php User In Redhat Client] ************************* 
changed: [192.168.240.11]

TASK: [php_delete | Delete Php Script] **************************************** 
changed: [192.168.240.11]

TASK: [pcre_delete | Delete Pcre] ********************************************* 
changed: [192.168.240.11]

PLAY RECAP ******************************************************************** 
192.168.240.11             : ok=16   changed=15   unreachable=0    failed=0   


real	0m23.096s
user	0m3.943s
sys	0m0.516s

4、删除后测试

[root@ip-10-10-240-14 tmp]# ps -ef|grep http
root      7491  1259  0 22:02 pts/0    00:00:00 grep http
[root@ip-10-10-240-14 tmp]# ps -ef|grep mysql
root      7493  1259  0 22:03 pts/0    00:00:00 grep mysql
[root@ip-10-10-240-14 tmp]# ps -ef|grep php
root      7495  1259  0 22:03 pts/0    00:00:00 grep php
[root@ip-10-10-240-14 tmp]# curl 10.10.240.14
curl: (7) couldn't connect to host

可与看到都删除了。

如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/lamp_install),然后放到/etc/ansible目录里

运维自动化之ansible playbook安装lamp环境_第1张图片