Directory structure for huge ansible inventory file -
our ansible inventory file getting bigger , bigger day day. wanted modularize directories , file. example.
[webservers] foo.example.com bar.example.com [dbservers] one.example.com two.example.com three.example.com
this can converted
|--production | |--webservers | | |--webservers | |--dbservers | | |--dbservers
where webservers file;
[webservers] foo.example.com bar.example.com
and dbservers file;
[dbservers] one.example.com two.example.com three.example.com
for simple inventory file works fine. problem comes when 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
i cant imagine directory structure , it. can please guide me right tutorial. thanks
ansible supports "dynamic inventories" can read more in here: http://docs.ansible.com/ansible/developing_inventory.html
what it:
simple script (python, ruby, shell etc) produces json in specific format.
how can benefit it:
create folder structure best reflects needs, , place servers config in there. create simple executable file read files , output result.
example:
inventory.py:
#!/usr/bin/python import yaml import json web = yaml.load(open('web.yml', 'r')) inventory = { '_meta': { 'hostvars': {} } } # individual server configuration server, properties in web['servers'].iteritems(): inventory['_meta']['hostvars'][server] = {} inventory['_meta']['hostvars'][server]['ansible_ssh_host'] = properties['ip'] # magic group servers inventory['all'] = {} inventory['all']['hosts'] = web['servers'].keys() # groups of servers if 'groups' in web: group, properties in web['groups'].iteritems(): inventory[group] = {} inventory[group]['hosts'] = web['groups'][group] print json.dumps(inventory, indent=2)
web.yml --- servers: foo: ip: 192.168.42.10 bar: ip: 192.168.42.20 groups: webservers: - foo dbservers: - bar
then call playbook usuall , same result if use standart ini
file.
Comments
Post a Comment