巨大的ansible库存文件的目录结构

Nag*_*gri 0 ansible ansible-playbook

我们的ansible库存文件日益变得越来越大.所以我们想用目录和文件模块化它.比如说.

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com
Run Code Online (Sandbox Code Playgroud)

这可以转换成

|--production
|  |--WEBSERVERS
|  |  |--webservers
|  |--DBSERVERS
|  |  |--dbservers
Run Code Online (Sandbox Code Playgroud)

Web服务器是一个文件;

[webservers]
foo.example.com
bar.example.com
Run Code Online (Sandbox Code Playgroud)

dbservers是一个文件;

[dbservers]
one.example.com
two.example.com
three.example.com
Run Code Online (Sandbox Code Playgroud)

for simple inventory file it works fine. Problem comes when I create group of groups.

like

[webservers]
foo.example.com
bar.example.com

[dbservers]
one.example.com
two.example.com
three.example.com

[master:children]
webservers
dbservers
Run Code Online (Sandbox Code Playgroud)

I cant imagine a directory structure for this and it. Can someone please guide me to the right tutorial. Thanks

Vor*_*Vor 7

Ansible支持"动态库存",你可以在这里阅读更多相关内容:http://docs.ansible.com/ansible/developing_inventory.html

它是什么:

以特定格式生成JSON的简单脚本(python,ruby,shell等).

我怎样才能从中受益:

创建最能反映您需求的文件夹结构,并将服务器配置放在那里.然后创建一个简单的可执行文件来读取这些文件并输出结果.

例:

inventory.py:

#!/usr/bin/python

import yaml
import json

web = yaml.load(open('web.yml', 'r'))

inventory = { '_meta': { 'hostvars': {} } }

# Individual server configuration
for server, properties in web['servers'].iteritems():
  inventory['_meta']['hostvars'][server] = {}
  inventory['_meta']['hostvars'][server]['ansible_ssh_host'] = properties['ip']

# Magic group for all servers
inventory['all'] = {}
inventory['all']['hosts'] = web['servers'].keys()

# Groups of servers
if 'groups' in web:
  for group, properties in web['groups'].iteritems():
    inventory[group] = {}
    inventory[group]['hosts'] = web['groups'][group]

print json.dumps(inventory, indent=2)
Run Code Online (Sandbox Code Playgroud) web.yml
---
servers:
  foo:
    ip: 192.168.42.10
  bar:
    ip: 192.168.42.20

groups:
  webservers:
  - foo
  dbservers:
  - bar
Run Code Online (Sandbox Code Playgroud)

然后将你的剧本称为usuall,你会得到与使用标准ini文件相同的结果.