1、首先安装好Ansible环境,具体步骤请见Ansible安装

2、先创建hosts文件(为后面编写脚本安装JDK做铺垫)

[root@localhost /]# vi hosts

[jdktest]

192.168.186.133 ansible_ssh_user=guxiong ansible_ssh_pass=private
192.168.186.134 ansible_ssh_user=guxiong ansible_ssh_pass=private

注:记得/etc/ansible/hosts也要加入

3、创建文件夹

[root@localhost /]#

makedir roles

nginx-install

default

files

ngnix-install

nginx-install.tar.gz

handler

meta

tasks    main.yml

templates  install-nginx.sh   nginx.conf

vars   main.yml

4、编辑tasks下面的main.yml

 - name: create direc
shell: mkdir -p {{ nginx_path }}
- name: copy nginx file to remote client
copy: src=nginx-install.tar.gz dest=/home/{{ user }}
- name: untar nginx
shell: tar -zxf nginx-install.tar.gz
- name: configure install-nginx.sh user template
template: src=install-nginx.sh dest=/home/{{ user }}/nginx-install/install-nginx.sh
- name: cd install direc and excu install
shell: cd /home/{{ user }}/nginx-install && sh install-nginx.sh
- name: configure nginx.conf use template
template: src=nginx.conf dest={{ nginx_path }}/conf/nginx.conf
- name: rm nginxfiles
shell: rm -rf /home/{{ user }}/nginx-install*

5、编辑templates下的 install-nginx.sh

#!/bin/bash

mkdir -p {{ nginx_path }}
cd nginx-1.9.3-hide-version
./configure --prefix={{ nginx_path }} --with-http_ssl_module --with-http_gunzip_module --with-http_stub_status_module --with-http_gzip_static_module --with-zlib=../zlib-1.2.8 --with-pcre=../pcre-8.38 --add-module=../nginx_upstream_check_module-master --add-module=../ngx_cache_purge-2.3 && make && make install
sleep 6
exit 0

nginx.conf

#user  nobody;
worker_processes 4; error_log logs/error.log;
#error_log logs/error.log notice;
#error_log logs/error.log info; pid logs/nginx.pid; events {
worker_connections 1024;
} 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"'; log_format main '$remote_addr - $remote_user [$time_local] "$request" '
'$status $body_bytes_sent "$http_referer" '
'"$http_user_agent" "$http_x_forwarded_for"'
'"$upstream_cache_status"';
access_log logs/access.log main; sendfile on;
#tcp_nopush on;
server_tokens off;
#keepalive_timeout 0;
keepalive_timeout 65; gzip on; proxy_connect_timeout 60;
proxy_read_timeout 30;
proxy_send_timeout 60;
proxy_buffer_size 4k;
proxy_buffers 32 4k;
proxy_busy_buffers_size 8k;
proxy_temp_file_write_size 512k;
proxy_next_upstream http_500 http_502 http_503 error invalid_header; proxy_temp_path {{ nginx_path }}/proxy_temp;
proxy_cache_path {{ nginx_path }}/proxy_cache
levels=1:2 keys_zone=cache_one:100m inactive=2d max_size=2g; upstream test {
server 192.168.186.133:8080;
server 192.168.186.134:8080;
check interval=3000 rise=2 fall=5 timeout=1000 type=tcp;
ip_hash;
}
server {
listen {{ nginx_port }};
server_name {{ nginx_host }};
check_status;
#charset koi8-r; #access_log logs/host.access.log main; location / {
# root html;
# index index.html index.htm;
proxy_pass http://test/baidu/;
}
location / {
proxy_pass http://test;
} location ~ /purge(/.*) { # allow 192.168.100.112;
# allow 192.168.100.64;
allow all;
# deny all;
proxy_cache_purge cache_one $host$1$is_args$args;
error_page 405 =200 /purge$1;
} location ^~ /jenkins { proxy_pass http://test/jenkins/; proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; access_log logs/jenkins_access.log main; } location /status {
check_status;
access_log off;
# allow 192.168.100.64;
allow all;
deny all;
}
#error_page 404 /404.html;
location ~ .*\.(gif|jpg|png|html|css|ico|pdf) {
proxy_pass http://test;
proxy_redirect off;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for; proxy_cache cache_one;
add_header Nginx-Cache $upstream_cache_status;
proxy_cache_valid 200 304 301 302 8h;
proxy_cache_valid 404 1m;
proxy_cache_valid any 2d;
proxy_cache_key $host$uri$is_args$args;
expires 30d; access_log logs/host.access.log main;
# access_log logs/access.log main;
} # 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 html;
# fastcgi_pass 127.0.0.1:9000;
# fastcgi_index index.php;
# fastcgi_param SCRIPT_FILENAME /scripts$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;
#}
} # another virtual host using mix of IP-, name-, and port-based configuration
#
#server {
# listen 8000;
# listen somename:8080;
# server_name somename alias another.alias; # location / {
# root html;
# index index.html index.htm;
# }
#} # HTTPS server
#
#server {
# listen 443 ssl;
# server_name localhost; # ssl_certificate cert.pem;
# ssl_certificate_key cert.key; # ssl_session_cache shared:SSL:1m;
# ssl_session_timeout 5m; # ssl_ciphers HIGH:!aNULL:!MD5;
# ssl_prefer_server_ciphers on; # location / {
# root html;
# index index.html index.htm;
# }
#} }

6、将使用到的变量编辑vars下的main.yml

user: guxiong
nginx_host: 192.168.186.134
nginx_port: 3080
nginx_path: /home/guxiong/nginx1.9.0

7、ansible-playbook安装  

[root@localhost /]# ansible-playbook nginx-install.yml --extra-var "host_cluster=jdktest user=guxiong nginx_host=192.168.186.134 nginx_port=3080 nginx_path=/home/guxiong/nginx1.9.0"

PLAY [jdktest] **************************************************************** 

TASK: [nginx-install | create direc] ******************************************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | copy nginx file to remote client] **********************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | untar nginx] *******************************************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | configure install-nginx.sh user template] **************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | cd install direc and excu install] *********************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | configure nginx.conf use template] *********************
changed: [192.168.186.134]
changed: [192.168.186.133] TASK: [nginx-install | rm nginxfiles] *****************************************
changed: [192.168.186.134]
changed: [192.168.186.133] PLAY RECAP ********************************************************************
192.168.186.133 : ok=7 changed=7 unreachable=0 failed=0
192.168.186.134 : ok=7 changed=7 unreachable=0 failed=0

  

使用Ansible自动配置Nginx服务的更多相关文章

  1. Ansible安装配置Nginx

    一.思路 现在一台机器上编译安装好nginx.打包,然后在用ansible去下发 cd /etc/ansible 进入ansible配置文件目录 mkdir roles/{common,install ...

  2. 自动分割nginx服务的日志文件

    nginx服务每天都会产生大量的日志信息,时间一长导致日志文件容量很大,会影响系统性能.通过以下shell代码,配合crontab定时执行可实现nginx日志定时分割的功能. #!/bin/bash ...

  3. 【Nginx】Windows平台下配置Nginx服务实现负载均衡

    前言:废话不多说了,直接上步骤. 系统环境:win10 测试用的开发环境和服务类型:VS2022 + DotNet 6 + WebApi 1.本地先创建一个webapi项目,用于测试使用. 2.新建一 ...

  4. 配置Nginx服务

    一,安装之前准备1.nginx依赖: gcc openssl-devel pcre-devel zlib-devel    安装依赖:yum install gcc openssl-devel pcr ...

  5. Centos 6.5 配置nginx服务

    官方网站:http://nginx.org最新版本:1.7.11官方文档:http://nginx.org/en/docs/ 一.安装NGINX查看当前centos版本: #cat /etc/redh ...

  6. 使用Ansible自动配置JDK环境

    1.首先安装好Ansible环境,具体步骤请见Ansible安装 2.先创建hosts文件(为后面编写脚本安装JDK做铺垫) [root@localhost /]# vi hosts [jdktest ...

  7. 【CNMP系列】CentOS7.0下安装Nginx服务

    话步前言,CNMP之路,系统起步:http://www.cnblogs.com/riverdubu/p/6425028.html 这回我来讲解下CentOS7.0下如何安装和配置Nginx服务 Ngi ...

  8. nodejs配置nginx 以后链接mongodb数据库

    服务器 :windows server2008 R2 反向代理 :nginx 1.15.1 for window 64位 数据库:mongodb 4 64位 使用框架express 首先下载nodej ...

  9. Nginx优化之基本安全优化 (隐藏Nginx软件版本号信息,更改源码隐藏Nginx软件名及版本号,更改Nginx服务的默认用户)

    一,隐藏Nginx软件版本号信息 查看版本号 curl -I 192.168.0.220 HTTP/1.1 200 OK Server: nginx/1.6.2 #这里清晰的暴露了Web版本号(1.6 ...

随机推荐

  1. Python中的strip()的理解

    在看到Python中strip的时候产生了疑问 strip() 用于移除字符串头尾指定的字符(默认为空格) 开始测试: >>> s = 'ncy_123.python' >&g ...

  2. MultipartEntity 乱码

    MultipartEntity multipartEntity = new MultipartEntity(HttpMultipartMode.BROWSER_COMPATIBLE, null, Ch ...

  3. 跟着百度学PHP[13]-文件处理 文件后缀验证、设置随机文件名....

    目录: 文件的处理:00x1 判断错误 文件的处理:00x2 判断类型 文件的处理:00x3 文件大小 ++++++++++++++++++++++++++++++++++++++++++++++++ ...

  4. headfirst设计模式swift版01

    headfirst设计模式这本书真好,准备用一个月学完.书里讲得很清楚了. 设计原则: 1.找出应用中可能需要变化之处,把它们独立出来,不要和那些不需要变化的代码混在一起. 2.针对接口编程,而不是针 ...

  5. EF调用存储过程、函数

    一.ef4.1 codeFirst 修改表结构 增加字段等 EF code first需要重新生成库导致数据丢失的问题 说这个问题前 首先先说下 我使用ef4.1 codefirst的目的. 是因为可 ...

  6. haproxy+keepalived实现高可用负载均衡(转)

      软件负载均衡一般通过两种方式来实现:基于操作系统的软负载实现和基于第三方应用的软负载实现.LVS就是基于Linux操作系统实现的一种软负载,HAProxy就是开源的并且基于第三应用实现的软负载. ...

  7. Spider Studio 新版本 (20140109) - 修复浏览器对部分网页不支持的BUG

    SS对部分网页中引用的jquery.js有冲突, 会造成网页部分JS效果无法正常执行. 本次版本对其进行了修正, 优化了浏览器的脚本引用机制, 修正了这个BUG.

  8. 01 awk工具的使用

    一:登录mysql后查看mysql的连接状态:show status ; 回车 如图所示: |Threads_connected    | 1| Threads_running       | 1   ...

  9. javascript弹出层-DEMO001

    首先上一张图 看下弹出层的效果 从图中可以看到二部分 一是弹出层 二是遮照层 弹出层:即弹出你要操作的内容 遮照层:遮照住不要操作的内空 实际技术原理主要是 CSS +JS  (z-index是核心) ...

  10. python XlsxWriter Example: Hello World

    http://xlsxwriter.readthedocs.io/example_hello_world.html The simplest possible spreadsheet. This is ...