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

ansible 渲染nginx配置文件(菜鸟版)

时间:2017-07-14 20:22:09      阅读:160      评论:0      收藏:0      [点我收藏+]

标签:ansible

标题有点吓人,其实做的很基础,简单。没什么技术含量。

1.首先在ansible上形成vhost.fact
cat books.txt
w ip1 dev 8021 8688 api
w ip2 dev 8021 8688 api

w是tomcat应用名称,api是域名名称


nginx.py
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import ConfigParser
import string, os, sys

def ynameconf():
    sfile = ‘books.txt‘
    inventory = {}
    with open(sfile, ‘rb‘) as f:
        for i in f.readlines():
            ip = i.strip().split()[1]
            yname = i.strip().split()[5]  # 域名
            if not yname in inventory:
                inventory[yname] = []
            inventory[yname].append(ip)
    return inventory

def ynameParser():
    cf = ConfigParser.ConfigParser()
    ynameyname=ynameconf()
    for k,v in ynameyname.iteritems():
        cf.add_section(‘general‘)
        cf.set("general", "pro_dir", k)
        cf.set("general", "ip1", v[0])
        cf.set("general", "ip2", v[1])
        # cf.set(k, ‘,‘.join(v))
        cf.write(open("vhost.fact", "w"))

ynameP=ynameParser()

生成的vhost.fact的样子
[general]
pro_dir = api
ip1 = ip1
ip2 = ip2

还有很多问题
1.假如有三台以上机器怎么处理
2.多个域名怎么同时处理,搞到我想到了协程

2.写yml文件,就几行

mkdir -p /etc/ansible/nginx

├── hosts
├── roles
│   └── vhost
│       ├── default
│       ├── files
│       ├── handlers
│       │   └── main.yml
│       ├── meta
│       ├── tasks
│       │   ├── install.yml
│       │   └── main.yml
│       ├── templates
│       │   ├── set.conf
│       │   └── vhost.conf
│       └── vars
└── vhost.yml

主要tasks/install.yml
- name: mkdir
  shell: mkdir -p /etc/ansible/facts.d
- name: copy vhost.fact
  copy: src=/etc/ansible/vhost.fact dest=/etc/ansible/facts.d/vhost.fact 
- name: flush
  setup: 
- name: copy
  template: src=set.conf dest=/usr/local/tengine/conf/SET/{{ ansible_local.vhost.general.pro_dir }}.conf
- name: copy
  template: src=vhost.conf dest=/usr/local/tengine/conf/conf.d/{{ ansible_local.vhost.general.pro_dir }}.conf

3.手动执行命令
#nginx域名渲染
cd /etc/ansible && python nginx.py
cd /etc/ansible/nginx/
#‘host=nginx‘ 这个是nginx服务器名称
ansible-playbook -i /etc/ansible/hosts vhost.yml --extra-vars ‘host=nginx‘
ansible -i /etc/ansible/hosts nginx -m shell -a ‘/usr/local/tengine/sbin/nginx -c /usr/local/tengine/conf/nginx.conf -t‘ 
ansible -i /etc/ansible/hosts nginx -m shell -a ‘/usr/local/tengine/sbin/nginx -c /usr/local/tengine/conf/nginx.conf -s reload‘

达到的效果,利用ansible使两台机器立刻部署完tomcat后,然后加入到nginx中。
土。



本文出自 “要有梦想,万一实现了呢” 博客,请务必保留此出处http://szgb17.blog.51cto.com/340201/1947640

ansible 渲染nginx配置文件(菜鸟版)

标签:ansible

原文地址:http://szgb17.blog.51cto.com/340201/1947640

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