上次介绍了如何使用ansible playbook安装mysql(http://dl528888.blog.51cto.com/2382721/1437882),这次介绍一下如何使用playbook安装nginx。
下面是安装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的版本
备注:此playbook仅能对centos或者redhat的6.x版本进行安装。
下面是安装nginx的playbook结构
09:33:15 # tree nginx_* 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 12 directories, 17 files
playbook的nginx安装是
09:34:04 # cat nginx_install.yml --- - hosts: "`host`" remote_user: "`user`" gather_facts: True roles: - common - pcre_install - nginx_install
playbook的nginx删除为
09:35:06 # cat nginx_delete.yml --- - hosts: "`host`" remote_user: "`user`" gather_facts: True roles: - nginx_delete - pcre_delete
1、安装nginx
09:26:58 # time ansible-playbook nginx_install.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem PLAY [192.168.240.17] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.240.17] TASK: [common | Install initializtion require software] *********************** changed: [192.168.240.17] TASK: [pcre_install | Copy Pcre Software To Redhat Client] ******************** changed: [192.168.240.17] TASK: [pcre_install | Uncompression Pcre Software In Redhat Client] *********** changed: [192.168.240.17] TASK: [pcre_install | Delete Pcre Software In Redhat Client] ****************** changed: [192.168.240.17] TASK: [nginx_install | Copy Nginx Software To Redhat Client] ****************** changed: [192.168.240.17] TASK: [nginx_install | Uncompression Nginx Software To Redhat Client] ********* changed: [192.168.240.17] TASK: [nginx_install | Copy Nginx Start Script To Redhat Client] ************** changed: [192.168.240.17] TASK: [nginx_install | Copy Nginx Config To Redhat Client] ******************** changed: [192.168.240.17] TASK: [nginx_install | Copy Nginx Vhost Config to RedHat Client] ************** changed: [192.168.240.17] TASK: [nginx_install | Create Nginx User In Redhat Client] ******************** changed: [192.168.240.17] TASK: [nginx_install | Create Nginx Dir] ************************************* changed: [192.168.240.17] => (item=vhost) changed: [192.168.240.17] => (item=logs) TASK: [nginx_install | Modify Nginx Dir Permission In Redhat Client] ********** changed: [192.168.240.17] => (item=/data/webroot/nginx) changed: [192.168.240.17] => (item=/usr/local/nginx-1.4.3) TASK: [nginx_install | Create Index Html To Redhat Client] ******************** changed: [192.168.240.17] TASK: [nginx_install | Install Check Script In Redhat Client] ***************** changed: [192.168.240.17] TASK: [nginx_install | Start Nginx Service In Redhat Client] ****************** changed: [192.168.240.17] TASK: [nginx_install | Add Boot Start Nginx Service In Redhat Client] ********* changed: [192.168.240.17] TASK: [nginx_install | Delete Nginx compression Software In Redhat Client] **** changed: [192.168.240.17] PLAY RECAP ******************************************************************** 192.168.240.17 : ok=18 changed=17 unreachable=0 failed=0 real 0m53.357s user 0m2.433s sys 0m0.341s
2、安装后测试
[root@ip-10-10-240-21 local]# ps -ef|grep nginx root 27791 1 0 21:27 ? 00:00:00 nginx: master process /usr/local/nginx-1.4.3/sbin/nginx -c /usr/local/nginx-1.4.3/conf/nginx.conf www 27792 27791 0 21:27 ? 00:00:00 nginx: worker process www 27794 27791 0 21:27 ? 00:00:00 nginx: worker process www 27795 27791 0 21:27 ? 00:00:00 nginx: worker process www 27796 27791 0 21:27 ? 00:00:00 nginx: worker process www 27797 27791 0 21:27 ? 00:00:00 nginx: worker process www 27798 27791 0 21:27 ? 00:00:00 nginx: worker process www 27799 27791 0 21:27 ? 00:00:00 nginx: worker process www 27800 27791 0 21:27 ? 00:00:00 nginx: worker process root 27905 26349 0 21:28 pts/0 00:00:00 grep nginx [root@ip-10-10-240-21 local]# chkconfig --list|grep nginx nginx 0:off 1:off 2:off 3:on 4:on 5:on 6:off [root@ip-10-10-240-21 local]# /etc/init.d/nginx status Usage: /etc/init.d/nginx {start|restart|reload|stop|test|show} [root@ip-10-10-240-21 local]# curl 10.10.240.21 Nginx 1.4.3 in 10.10.240.21 is success! [root@ip-10-10-240-21 local]# curl -I 10.10.240.21 HTTP/1.1 200 OK Server: Tengine/1.5.2 Date: Wed, 16 Jul 2014 01:35:59 GMT Content-Type: text/html Content-Length: 41 Last-Modified: Wed, 16 Jul 2014 01:27:57 GMT Connection: keep-alive ETag: "53c5d51d-29" Accept-Ranges: bytes
可与看到安装成功,并且软件名与版本都隐藏了
3、删除nginx
09:28:02 # time ansible-playbook nginx_delete.yml --extra-vars "host=192.168.240.17 user=root" --private-key=/root/test.pem PLAY [192.168.240.17] ********************************************************* GATHERING FACTS *************************************************************** ok: [192.168.240.17] TASK: [nginx_delete | stop nginx service] ************************************* changed: [192.168.240.17] TASK: [nginx_delete | Delete Nginx Boot Start Script] ************************* changed: [192.168.240.17] TASK: [nginx_delete | Delete Nginx Dir] *************************************** changed: [192.168.240.17] TASK: [nginx_delete | Delete Nginx User] ************************************** changed: [192.168.240.17] TASK: [nginx_delete | Delete Nginx Service Start Script] ********************** changed: [192.168.240.17] TASK: [pcre_delete | Delete Pcre] ********************************************* changed: [192.168.240.17] PLAY RECAP ******************************************************************** 192.168.240.17 : ok=7 changed=6 unreachable=0 failed=0 real 0m30.222s user 0m0.774s sys 0m0.126s
4、删除后测试
[root@ip-10-10-240-21 local]# ps -ef|grep nginx root 28417 26349 0 21:37 pts/0 00:00:00 grep nginx [root@ip-10-10-240-21 local]# chkconfig --list|grep nginx [root@ip-10-10-240-21 local]# ll /etc/init.d/nginx ls: cannot access /etc/init.d/nginx: No such file or directory [root@ip-10-10-240-21 local]# ll /usr/local/ total 44 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 Jul 10 22:43 lib 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
可与看到都删除了。
如果大家想使用我的例子,可以从github里下载(地址是https://github.com/dl528888/ansible-examples/tree/master/nginx_install),然后放到/etc/ansible目录里,下面是内容
09:39:13 # unzip -v nginx_install.zip Archive: nginx_install.zip Length Method Size Cmpr Date Time CRC-32 Name -------- ------ ------- ---- ---------- ----- -------- ---- 132 Defl:N 95 28% 07-16-2014 09:34 d75c7ef9 nginx_install.yml 117 Defl:N 89 24% 07-16-2014 09:35 27eb35d2 nginx_delete.yml 0 Stored 0 0% 07-03-2014 17:29 00000000 roles/common/ 0 Stored 0 0% 06-30-2014 15:58 00000000 roles/common/meta/ 267 Defl:N 178 33% 07-16-2014 09:25 31ee20ec roles/common/meta/main.yml 0 Stored 0 0% 07-14-2014 13:40 00000000 roles/common/tasks/ 586 Defl:N 268 54% 07-16-2014 09:25 1c0af2a1 roles/common/tasks/main.yml 0 Stored 0 0% 06-19-2014 13:43 00000000 roles/common/handlers/ 58 Defl:N 48 17% 07-16-2014 09:25 8d058053 roles/common/handlers/main.yml 0 Stored 0 0% 06-19-2014 13:30 00000000 roles/common/vars/ 0 Stored 0 0% 06-19-2014 13:30 00000000 roles/common/templates/ 0 Stored 0 0% 06-19-2014 13:30 00000000 roles/common/files/ 0 Stored 0 0% 06-24-2014 11:27 00000000 roles/nginx_install/ 0 Stored 0 0% 07-08-2014 14:12 00000000 roles/nginx_install/meta/ 198 Defl:N 149 25% 07-16-2014 09:25 e527ea95 roles/nginx_install/meta/main.yml 0 Stored 0 0% 07-03-2014 17:46 00000000 roles/nginx_install/tasks/ 198 Defl:N 153 23% 07-16-2014 09:25 eb70a996 roles/nginx_install/tasks/delete.yml 65 Defl:N 41 37% 07-16-2014 09:25 a3197ca7 roles/nginx_install/tasks/main.yml 1142 Defl:N 300 74% 07-16-2014 09:25 34ddc722 roles/nginx_install/tasks/copy.yml 1574 Defl:N 431 73% 07-16-2014 09:25 13c2a951 roles/nginx_install/tasks/install.yml 0 Stored 0 0% 06-19-2014 13:30 00000000 roles/nginx_install/handlers/ 0 Stored 0 0% 06-24-2014 10:38 00000000 roles/nginx_install/vars/ 155 Defl:N 105 32% 07-16-2014 09:25 70e58924 roles/nginx_install/vars/main.yml 0 Stored 0 0% 07-16-2014 09:25 00000000 roles/nginx_install/templates/ 738 Defl:N 337 54% 07-16-2014 09:25 bd02415e roles/nginx_install/templates/vhost.conf 784 Defl:N 346 56% 06-24-2014 11:37 6d09dba5 roles/nginx_install/templates/install_nginx.sh 1642 Defl:N 698 58% 07-16-2014 09:25 60b4ed9a roles/nginx_install/templates/nginx.conf 196 Defl:N 147 25% 07-16-2014 09:25 fa0f7181 roles/nginx_install/templates/index.php 77 Defl:N 71 8% 07-16-2014 09:25 1fdd8439 roles/nginx_install/templates/index.html 926 Defl:N 459 50% 07-16-2014 09:25 4aa2e8fa roles/nginx_install/templates/nginx 0 Stored 0 0% 07-03-2014 14:24 00000000 roles/nginx_install/files/ 2346820 Defl:N 2283881 3% 07-16-2014 09:25 8a501ba6 roles/nginx_install/files/nginx-1.4.3.tar.gz 0 Stored 0 0% 06-23-2014 14:03 00000000 roles/nginx_delete/ 0 Stored 0 0% 07-08-2014 14:12 00000000 roles/nginx_delete/meta/ 197 Defl:N 147 25% 07-16-2014 09:25 ab72e486 roles/nginx_delete/meta/main.yml 0 Stored 0 0% 07-01-2014 17:05 00000000 roles/nginx_delete/tasks/ 893 Defl:N 289 68% 07-16-2014 09:25 68681d99 roles/nginx_delete/tasks/delete.yml 22 Stored 22 0% 07-16-2014 09:25 728ce4aa roles/nginx_delete/tasks/main.yml 0 Stored 0 0% 06-24-2014 14:14 00000000 roles/nginx_delete/handlers/ 0 Stored 0 0% 06-24-2014 12:48 00000000 roles/nginx_delete/vars/ 155 Defl:N 105 32% 07-16-2014 09:25 70e58924 roles/nginx_delete/vars/main.yml 0 Stored 0 0% 06-19-2014 17:32 00000000 roles/nginx_delete/templates/ 0 Stored 0 0% 06-24-2014 13:53 00000000 roles/nginx_delete/files/ -------- ------- --- ------- 2356942 2288359 3% 43 files
如果觉得好,给个赞并多评论,谢谢。