之前介绍了使用ansible playbok安装zabbix客户端、mysql服务端、nginx,现在介绍一下安装lnmp环境。
下面是安装nginx的信息:
nginx_user: www nginx用户 nginx_port: 80 nginx监听端口 nginx_web_dir: /data/webroot/nginx nginx的目录 nginx_version: 1.4.3 nginx的版本 pcre_version: 8.33 pcre的版本 pretended_name: Tengine 为了安全起见,把nginx名字改为tengine pretended_version: 1.5.2 隐藏版本,换个tengine的版本
下面是安装php的信息:
php_user: www php fpm模式的启动用户 php_port: 9000 php fpm使用tcp的端口 php_dir: /usr/local php的安装目录 php_version: 5.3.10 php的版本 mysql_basedir: /data/mysql/basedir mysql的目录
下面是安装mysql的信息:
mysql_basedir: /data/mysql/basedir 源码目录 mysql_datadir: /data/mysql/datadir 数据目录 mysql_user: mysql mysql用户 mysql_database_user: root 数据库用户 mysql_passwd: 'E4yR3WnoluSFTCBAI' 数据库密码 mysql_port: 3306 mysql监听端口 mysql_sock: /data/mysql/datadir/mysql.sock mysql的sock mysql_charset: utf8 mysql字符集 mysql_collation: utf8_general_ci mysql排序方式 mysql_version: Percona-Server-5.5.21-rel25.1.tar.gz mysql版本
备注:此playbook仅能对centos或者redhat的6.x版本进行安装。
下面是安装lnmp的playbook结构
09:28:50 # tree nginx* mysql* php* nginx_delete ├── files ├── handlers ├── meta │ └── main.yml ├── tasks │ ├── delete.yml │ └── main.yml ├── templates └── vars └── main.yml nginx_install ├── files │ └── nginx-1.4.3.tar.gz ├── handlers ├── meta │ └── main.yml ├── tasks │ ├── copy.yml │ ├── delete.yml │ ├── install.yml │ └── main.yml ├── templates │ ├── index.html │ ├── index.php │ ├── install_nginx.sh │ ├── nginx │ ├── nginx.conf │ └── 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的lnmp安装是
09:29:59 # cat lnmp_install.yml --- - hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - common - pcre_install - nginx_install - mysql_install - php_install
playbook的lnmp删除是
09:30:33 # cat lnmp_delete.yml --- - hosts: "{{host}}" remote_user: "{{user}}" gather_facts: True roles: - pcre_delete - php_delete - nginx_delete - mysql_delete
1、安装lnmp
09:35:55 # time ansible-playbook lnmp_install.yml --extra-vars "host=192.168.240.13 user=root" --private-key=/root/test.pem PLAY [192.168.240.13] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.240.13] TASK: [common | Install initializtion require software] *********************** changed: [192.168.240.13] TASK: [pcre_install | Copy Pcre Software To Redhat Client] ******************** changed: [192.168.240.13] TASK: [pcre_install | Uncompression Pcre Software In Redhat Client] *********** changed: [192.168.240.13] TASK: [pcre_install | Delete Pcre Software In Redhat Client] ****************** changed: [192.168.240.13] TASK: [nginx_install | Copy Nginx Software To Redhat Client] ****************** changed: [192.168.240.13] TASK: [nginx_install | Uncompression Nginx Software To Redhat Client] ********* changed: [192.168.240.13] TASK: [nginx_install | Copy Nginx Start Script To Redhat Client] ************** changed: [192.168.240.13] TASK: [nginx_install | Copy Nginx Config To Redhat Client] ******************** changed: [192.168.240.13] TASK: [nginx_install | Copy Nginx Vhost Config to RedHat Client] ************** changed: [192.168.240.13] TASK: [nginx_install | Create Nginx User In Redhat Client] ******************** changed: [192.168.240.13] TASK: [nginx_install | Create Nginx Dir] ************************************* changed: [192.168.240.13] => (item=vhost) changed: [192.168.240.13] => (item=logs) TASK: [nginx_install | Modify Nginx Dir Permission In Redhat Client] ********** changed: [192.168.240.13] => (item=/data/webroot/nginx) changed: [192.168.240.13] => (item=/usr/local/nginx-1.4.3) TASK: [nginx_install | Create Index Html To Redhat Client] ******************** changed: [192.168.240.13] TASK: [nginx_install | Install Check Script In Redhat Client] ***************** changed: [192.168.240.13] TASK: [nginx_install | Start Nginx Service In Redhat Client] ****************** changed: [192.168.240.13] TASK: [nginx_install | Add Boot Start Nginx Service In Redhat Client] ********* changed: [192.168.240.13] TASK: [nginx_install | Delete Nginx compression Software In Redhat Client] **** changed: [192.168.240.13] TASK: [mysql_install | Copy Mysql Software To Redhat Client] ****************** changed: [192.168.240.13] TASK: [mysql_install | Create Mysql User In Redhat Client] ******************** changed: [192.168.240.13] TASK: [mysql_install | Copy Mysql Start Script To Redhat Client] ************** changed: [192.168.240.13] TASK: [mysql_install | Copy Install Mysql Script To Redhat Client] *********** changed: [192.168.240.13] TASK: [mysql_install | Copy Mysql Config To Redhat Client] ******************** changed: [192.168.240.13] TASK: [mysql_install | Copy Mysql Security Script To Redhat Client] *********** changed: [192.168.240.13] TASK: [mysql_install | Create Mysql Install Dir] ****************************** ok: [192.168.240.13] TASK: [mysql_install | Uncompression Mysql Software To Redhat Client] ********* changed: [192.168.240.13] TASK: [mysql_install | Modify Mysql Dir Permission In Redhat Client] ********** ok: [192.168.240.13] => (item=/data/mysql/datadir) ok: [192.168.240.13] => (item=/data/mysql/basedir) TASK: [mysql_install | Install Mysql Script In Redhat Client] ***************** changed: [192.168.240.13] TASK: [mysql_install | Start Myql Security Script In Redhat Client] *********** changed: [192.168.240.13] TASK: [mysql_install | Add Boot Start Mysql Service In Redhat Client] ********* changed: [192.168.240.13] TASK: [mysql_install | Delete Mysql compression Software In Redhat Client] **** changed: [192.168.240.13] TASK: [php_install | Copy Php Software To Redhat Client] ********************** changed: [192.168.240.13] TASK: [php_install | Uncompression Php Software To Redhat Client] ************* changed: [192.168.240.13] TASK: [php_install | Copy Php Start Script To Redhat Client] ****************** changed: [192.168.240.13] TASK: [php_install | Copy Php Config To Redhat Client] ************************ changed: [192.168.240.13] TASK: [php_install | Copy Php Install Script To Redhat Client] **************** changed: [192.168.240.13] TASK: [php_install | Create Php User In Redhat Client] ************************ ok: [192.168.240.13] TASK: [php_install | Install Php Script In Redhat Client] ********************* changed: [192.168.240.13] TASK: [php_install | Start Php Service In Redhat Client] ********************** changed: [192.168.240.13] TASK: [php_install | Add Boot Start Php Service In Redhat Client] ************* changed: [192.168.240.13] TASK: [php_install | Delete Php compression Software In Redhat Client] ******** changed: [192.168.240.13] PLAY RECAP ******************************************************************** 192.168.240.13 : ok=41 changed=37 unreachable=0 failed=0 real 2m16.803s user 0m14.753s sys 0m1.468s
可以看到2分16秒就安装成功,一般比较费时的是使用yum安装软件与从本地负责包到客户端,所以yum源最后使用自己做的yum源或者找个速度快的yum源。
2、安装后测试
[root@ip-10-10-240-20 tmp]# ps -ef|grep nginx|wc -l 10 [root@ip-10-10-240-20 tmp]# ps -ef|grep mysql|wc -l 3 [root@ip-10-10-240-20 tmp]# ps -ef|grep php|wc -l 130 [root@ip-10-10-240-20 tmp]# cd /data/webroot/nginx/ logs/ vhost/ [root@ip-10-10-240-20 tmp]# cd /data/webroot/nginx/vhost/ [root@ip-10-10-240-20 vhost]# cat index.html Nginx 1.4.3 in 10.10.240.20 is success! [root@ip-10-10-240-20 vhost]# cat index.php <?php $link = mysql_connect('10.10.240.20','zabbix','zabbix'); if ($link) echo "Successs!!!Php install success!\n"; else echo "Fail!!!Php install fail!\n"; mysql_close(); ?> [root@ip-10-10-240-20 vhost]# curl 10.10.240.20/index.html Nginx 1.4.3 in 10.10.240.20 is success! [root@ip-10-10-240-20 vhost]# curl 10.10.240.20/index.php Successs!!!Php install success! [root@ip-10-10-240-20 vhost]# curl -I 10.10.240.20/index.php HTTP/1.1 200 OK Server: Tengine/1.5.2 Date: Mon, 21 Jul 2014 02:12:24 GMT Content-Type: text/html Connection: keep-alive Vary: Accept-Encoding
3、删除lnmp
09:38:36 # time ansible-playbook lnmp_delete.yml --extra-vars "host=192.168.240.13 user=root" --private-key=/root/test.pem PLAY [192.168.240.13] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.240.13] TASK: [pcre_delete | Delete Pcre] ********************************************* changed: [192.168.240.13] TASK: [php_delete | Copy Delete Php Script To RedHat Client] ****************** changed: [192.168.240.13] TASK: [php_delete | Run Delete Php Script In RedHat Client] ******************* changed: [192.168.240.13] TASK: [php_delete | Delete Php User In Redhat Client] ************************* failed: [192.168.240.13] => {"failed": true, "item": "", "name": "www", "rc": 8} msg: userdel: user www is currently logged in ...ignoring TASK: [php_delete | Delete Php Script] **************************************** changed: [192.168.240.13] TASK: [nginx_delete | stop nginx service] ************************************* changed: [192.168.240.13] TASK: [nginx_delete | Delete Nginx Boot Start Script] ************************* changed: [192.168.240.13] TASK: [nginx_delete | Delete Nginx Dir] *************************************** changed: [192.168.240.13] TASK: [nginx_delete | Delete Nginx User] ************************************** changed: [192.168.240.13] TASK: [nginx_delete | Delete Nginx Service Start Script] ********************** changed: [192.168.240.13] TASK: [mysql_delete | Stop Mysql Service] ************************************* changed: [192.168.240.13] TASK: [mysql_delete | Delete Mysql Boot Start Script] ************************* changed: [192.168.240.13] TASK: [mysql_delete | Delete Mysql Dir And Socket] **************************** changed: [192.168.240.13] TASK: [mysql_delete | Delete Mysql User] ************************************** changed: [192.168.240.13] TASK: [mysql_delete | Delete Mysql Service Start Script] ********************** changed: [192.168.240.13] PLAY RECAP ******************************************************************** 192.168.240.13 : ok=16 changed=14 unreachable=0 failed=0 real 0m31.511s user 0m1.470s sys 0m0.232s
4、删除后测试
[root@ip-10-10-240-20 vhost]# ll /usr/local/ total 40 drwxr-xr-x. 2 root root 4096 Sep 23 2011 bin drwxr-xr-x. 2 root root 4096 Sep 23 2011 etc drwxr-xr-x. 2 root root 4096 Sep 23 2011 games drwxr-xr-x. 2 root root 4096 Sep 23 2011 include drwxr-xr-x. 2 root root 4096 Sep 23 2011 lib64 drwxr-xr-x. 2 root root 4096 Sep 23 2011 libexec drwxr-xr-x. 2 root root 4096 Sep 23 2011 sbin drwxr-xr-x. 5 root root 4096 May 12 2013 share drwxr-xr-x. 3 root root 4096 May 12 2013 src drwxr-xr-x 5 zabbix zabbix 4096 Jul 8 02:17 zabbix [root@ip-10-10-240-20 vhost]# ps -ef|grep php root 10016 3527 0 22:13 pts/0 00:00:00 grep php [root@ip-10-10-240-20 vhost]# ps -ef|grep mysql root 10018 3527 0 22:13 pts/0 00:00:00 grep mysql [root@ip-10-10-240-20 vhost]# ps -ef|grep nginx root 10020 3527 0 22:13 pts/0 00:00:00 grep nginx [root@ip-10-10-240-20 vhost]# id www id: www: No such user [root@ip-10-10-240-20 vhost]# id mysql id: mysql: No such user
可与看到都删除了。
如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/lnmp_install),然后放到/etc/ansible目录里