码迷,mamicode.com
首页 > 其他好文 > 详细

使用Ansible的Playbook修改nginx配置文件

时间:2018-02-06 15:29:00      阅读:1816      评论:0      收藏:0      [点我收藏+]

标签:web   poc   cat   set   location   direct   入口   rem   var   

目录结构:

技术分享图片

文件内容

tasks 目录下的“file.yml”文件,内容如下:

技术分享图片

tasks目录下的“main.yml”
技术分享图片

templates目录下的“nginx.conf.j2”

{% if nginx_use_proxy %}
{% for proxy in nginx_proxies %}
upstream {{ proxy.name }} {
    server {{ ansible_eth0.ipv4.address }}:{{ proxy.port }} 
}
{% endfor %}
{% endif %}
server {
    listen 80;
    server_name {{ nginx_server_name }};
    access_log off;
    error_log /dev/null crit;
    rewrite ^ https://$server_name$request_uri? permanent;
}
server {
    listen 443 ssl;
    server_name {{ nginx_server_name }};
    ssl_certificate /etc/nginx/ssl/{{ nginx_ssl_cert_name }};
    ssl_certificate_key /etc/nginx/ssl/{{ nginx_ssl_cert_key }};

    root {{ nginx_web_root }};
    index index.html index.html;

    {% if nginx_use_auth %}
        auth_basic  "Restricted";
        auth_basic_user_file /etc/nginx/{{ project_name }}.htpasswd;
    {% endif %}

    {% if nginx_use_proxy %}
    {% for proxy in nginx_proxies %}

        location {{ proxy.location }} {
            proxy_set_header X-Real-IP $remote_addr;
            proxy_set_header X-Forwarded-Proto http;
            proxy_set_header X-Url-Scheme $scheme;
            proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
            proxy_set_header Host $http_host;
            proxy_set_header X-NginX-Proxy true;
            proxy_redirect off;
            proxy_pass http://{{ proxy.name }};
        }
    {% endfor %}
    {% endif %}

    {% if nginx_server_static %}
        location / {
            try_file $uri $uri/ =404
    {% endif %}

}

var目录下的“main.yml”

---

nginx_server_name: www.test.com
nginx_web_root: /opt/test/
nginx_proxies:
    - name: suspicious
      location: /
      port: 2368
    - name: suspocoous-api
      location: /api
      port: 3000

执行入口文件“nginxconf.yml”

- name: Nginx Proxy Server‘s Conf Dynamic Create
    hosts: your IP
    vars:
        nginx_use_proxy: true
        nginx_ssl_cert_name: test.crt
        nginx_ssl_cert_key: test.key
        nginx_use_auth: true
        project_name: suspicious
        nginx_server_static: true
    gather_facts: true    //收集主机配置信息

    roles:
        - { role: nginxconf }

- name: Nginx WebServer‘s Conf Dynamic Create
    hosts: your IP
    vars:
        nginx_use_proxy: false
        nginx_ssl_cert_name: test.crt
        nginx_ssl_cert_key: test.key
        nginx_use_auth: false
        project_name: suspicious
        nginx_server_static: false
    gather_facts: no

    roles:
        - { role: nginxconf }

使用Ansible的Playbook修改nginx配置文件

标签:web   poc   cat   set   location   direct   入口   rem   var   

原文地址:http://blog.51cto.com/wymanlee/2069417

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!