xiaoxue85 发表于 2018-7-29 11:16:37

ansible 用变量简单渲染nginx配置文件

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()
  yname = i.strip().split()# 域名
  if not yname in inventory:
  inventory = []
  inventory.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)
  cf.set("general", "ip2", v)
  # cf.set(k, ','.join(v))
  cf.write(open("vhost.fact", "w"))
  ynameP=ynameParser()
  生成的vhost.fact的样子
  
  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中。
  土。
页: [1]
查看完整版本: ansible 用变量简单渲染nginx配置文件