基于ansible的roles实现nginx源码安装部署php
ansible是一款比较热门的自动化部署工具。具备比较好兼用性,并且使用比较简单,而今天咱们经过roles来实现咱们的自动化部署;先讲讲经过roles实现部署的好处吧,经过roles咱们便很好的管理咱们要部署的内容,并且经过roles来管理,使得咱们使用ansible变得更方便。html
环境主机:node1 172.25.0.29 ansiblenode
node2 172.25.0.30 测试主机nginx
先看一下role角色定义:web
---以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files(依赖的文件)等;shell
角色目录的定义:bash
role_name/(以角色名命名的目录)app
files/:框架
存储由copy或script等模块调用的文件; ssh
tasks/:
此目录中至少应该有一个名为main.yml的文件,用于定义各task;其它的文件须要由main.yml进行“包含”调用;
handlers/:
此目录中至少应该有一个名为main.yml的文件,用于定义各handler;其它的文件须要由main.yml进行“包含”调用;
vars/:
此目录中至少应该有一个名为main.yml的文件,用于定义各variable;其它的文件须要由main.yml进行“包含”调用;
templates/:
存储由template模块调用的模板文本;
meta/:
此目录中至少应该有一个名为main.yml的文件,定义当前角色的特殊设定及其依赖关系;其它的文件须要由main.yml进行“包含”调用;
default/:
此目录中至少应该有一个名为main.yml的文件,用于设定默认变量;
接下来开始实现nginx快速安装
主意小事项:
作ansible要先作ssh的公私钥认证,我这里作了就不在演示了。
一、建立roles的所须要的目录
[root@node1 ansible]# mkdir -pv roles/nginx/{files,templates,vars,handlers,meta,default,tasks} mkdir: created directory ‘roles/nginx’ mkdir: created directory ‘roles/nginx/files’ mkdir: created directory ‘roles/nginx/templates’ mkdir: created directory ‘roles/nginx/vars’ mkdir: created directory ‘roles/nginx/handlers’ mkdir: created directory ‘roles/nginx/meta’ mkdir: created directory ‘roles/nginx/default’ mkdir: created directory ‘roles/nginx/tasks’
二、定义角色路径:
[root@node1 nginx]# cat /etc/ansible/nginx.yaml - hosts: 172.25.0.30 remote_user: root roles: - nginx ###这个表示roles目录下的nginx目录
三、在tasks文件夹里面定义咱们的task。
[root@node1 nginx]# cat tasks/main.yml - name: copy nginx package to remote host copy: src=nginx-1.12.0.tar.gz dest=/tmp/nginx-1.12.0.tar.gz #### 这里是调用files模块 tags: cppkg - name: tar nginx shell: cd /tmp;tar -xf nginx-1.12.0.tar.gz - name: install pakger yum: name={{ item }} state=latest with_items: - openssl-devel - pcre-devel - gcc - name: install nginx shell: cd /tmp/nginx-1.12.0;useradd nginx;./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module --with-pcre;make && make install - name: copy conf file nginx.conf ### 这里调用的是templates模块 template: src=nginx.conf dest=/usr/local/nginx/conf/nginx.conf tags: ngxconf notify: reload nginx service ###这里调用的是handlers模块
四、往file添加咱们的nginx压缩包
[root@node1 files]# wget http://nginx.org/download/nginx-1.12.0.tar.gz
五、template这一行也要添加对应的template这个目录和主服务端定义的变量:
[root@node1 nginx]# cat templates/nginx.conf ###这里文件用来自定义咱们所须要的,我这里只做演示,就定了两个参数。 user nginx; worker_processes {{ ansible_processor_vcpus }}; #修改CPU进程数量 #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen {{ ngxport }}; server_name www.xiaozhang.com; access_log logs/www.xiaozhang.com ; #location / { # proxy_pass http://172.25.0.29; #} #error_page 404 /404.html; # redirect server error pages to the static page /50x.html # error_page 500 502 503 504 /50x.html; location = /50x.html { root html; } # proxy the PHP scripts to Apache listening on 127.0.0.1:80 # #location ~ \.php$ { # proxy_pass http://127.0.0.1; #} # pass the PHP scripts to FastCGI server listening on 127.0.0.1:9000 # location ~ \.php$ { root /web; fastcgi_pass 127.0.0.1:9000; fastcgi_index index.php; fastcgi_param SCRIPT_FILENAME $document_root$fastcgi_script_name; include fastcgi_params; } # deny access to .htaccess files, if Apache's document root # concurs with nginx's one # #location ~ /\.ht { # deny all; #} } include vhosts/*.conf; }
##这里须要注意的就是模板变量(客户端自动采集)、和在服务端定义的变量`ngx_port`
六、接下来在vars目录下添加自定义变量
[root@node1 nginx]# cat vars/main.yml ngxport: "8080"
七、接下来再到handlers目录下添加自定义触发器模块:
[root@node1 handlers]# cat main.yml - name: reload nginx service shell: /usr/local/nginx/sbin/nginx
八、咱们查看一下目录结构:
[root@node1 roles]# tree . └── nginx ├── default ├── files │ └── nginx-1.12.0.tar.gz ├── handlers │ └── main.yml ├── meta ├── tasks │ └── main.yml ├── templates │ └── nginx.conf └── vars └── main.yml
九、开始部署,先检查一下
[root@node1 ~]# ansible-playbook -C /etc/ansible/nginx.yaml PLAY [172.25.0.30] ****************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [172.25.0.30] TASK [nginx : copy nginx package to remote host] ************************************************************************************************************ changed: [172.25.0.30] TASK [nginx : tar nginx] ************************************************************************************************************************************ skipping: [172.25.0.30] TASK [nginx : install pakger] ******************************************************************************************************************************* ok: [172.25.0.30] => (item=[u'openssl-devel', u'pcre-devel', u'gcc']) TASK [nginx : install nginx] ******************************************************************************************************************************** skipping: [172.25.0.30] TASK [nginx : copy conf file nginx.conf] ******************************************************************************************************************** changed: [172.25.0.30] RUNNING HANDLER [nginx : reload nginx service] ************************************************************************************************************** skipping: [172.25.0.30] PLAY RECAP ************************************************************************************************************************************************** 172.25.0.30 : ok=4 changed=2 unreachable=0 failed=0 [root@node1 ~]#
##咱们发现检测没问题,开始部署:
[root@node1 ~]# ansible-playbook /etc/ansible/nginx.yaml PLAY [172.25.0.30] ****************************************************************************************************************************************** TASK [Gathering Facts] ************************************************************************************************************************************** ok: [172.25.0.30] TASK [nginx : copy nginx package to remote host] ************************************************************************************************************ changed: [172.25.0.30] TASK [nginx : tar nginx] ************************************************************************************************************************************ changed: [172.25.0.30] TASK [nginx : install pakger] ******************************************************************************************************************************* ok: [172.25.0.30] => (item=[u'openssl-devel', u'pcre-devel', u'gcc']) TASK [nginx : install nginx] ******************************************************************************************************************************** changed: [172.25.0.30] TASK [nginx : copy conf file nginx.conf] ******************************************************************************************************************** changed: [172.25.0.30] NOTIFIED: [nginx | reload nginx service] ******************************************************************************************************************** changed: [172.25.0.30] PLAY RECAP ************************************************************************************************************************************************** 172.25.0.30 : ok=6 changed=2 unreachable=0 failed=0
十、查看测试结果,在node2上:
[root@node2 conf]# cat /usr/local/nginx/conf/nginx.conf user nginx; worker_processes 2; #error_log logs/error.log; #error_log logs/error.log notice; #error_log logs/error.log info; #pid logs/nginx.pid; events { worker_connections 65535; } http { include mime.types; default_type application/octet-stream; #log_format main '$remote_addr - $remote_user [$time_local] "$request" ' # '$status $body_bytes_sent "$http_referer" ' # '"$http_user_agent" "$http_x_forwarded_for"'; #access_log logs/access.log main; sendfile on; #tcp_nopush on; #keepalive_timeout 0; keepalive_timeout 65; #gzip on; server { listen 8080; server_name www.xiaozhang.com; access_log logs/www.xiaozhang.com ; #location / { # proxy_pass http://172.25.0.29; ###咱们能够看到 worker_processes 和listen 这两个参数已经获取到。
[root@node2 conf]# netstat -ntpl Active Internet connections (only servers) Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name tcp 0 0 0.0.0.0:111 0.0.0.0:* LISTEN 1/systemd tcp 0 0 0.0.0.0:8080 0.0.0.0:* LISTEN 10896/nginx: master
咱们也发现咱们的服务也起来了。证实咱们的roles部署是成功的。
总结
该实验是ansible的roles一个方面应用,主要是为了可以快速实现一个服务的部署,固然啦,咱们利用这样的方法还能够部署更多的服务,咱们要认识到ansible提供给咱们的只是一个框架。因此咱们在应用时要懂得转变,灵活运用。