您的位置:首页 > 其它

基于ansible的roles实现agnix源码安装部署

2017-11-06 22:49 609 查看
基于ansible的roles实现agnix源码安装部署 ansible是一款比较热门的自动化部署工具。具有比较好兼用性,而且使用比较简单,而今天我们通过roles来实现我们的自动化部署;先讲讲通过roles实现部署的好处吧,通过roles我们便很好的管理我们要部署的内容,而且通过roles来管理,使得我们使用ansible变得更方便。 环境主机:node1 172.25.0.29 ansible node2 172.25.0.30 测试主机先看一下role角色定义: ---以特定的层级目录结构进行组织的tasks、variables、handlers、templates、files(依赖的文件)等;角色目录的定义:role_name/(以角色名命名的目录)files/: 存储由copy或script等模块调用的文件; 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的公私钥认证,我这里做了就不在演示了。1、创建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’
2、定义角色路径:
[root@node1 nginx]# cat   /etc/ansible/nginx.yaml
- hosts: 172.25.0.30
remote_user: root
roles:
- nginx           ###这个表示roles目录下的nginx目录
3、在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模块
4、往file添加我们的nginx压缩包
[root@node1 files]# wget [root@node1' target='_blank'>http://nginx.org/download/nginx-1.12.0.tar.gz
5、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`6、接下来在vars目录下添加自定义变量
[root@node1 nginx]# cat  vars/main.yml
ngxport: "8080"
7、接下来再到handlers目录下添加自定义触发器模块:
[root@node1 handlers]# cat main.yml
- name: reload nginx service
shell: /usr/local/nginx/sbin/nginx
8、我们查看一下目录结构:
[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
9、开始部署,先检查一下
[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
10、查看测试结果,在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提供给我们的只是一个框架。所以我们在应用时要懂得转变,灵活运用。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  roles ansible agnix