mirror of https://github.com/ansible/ansible.git
You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
78 lines
2.6 KiB
Python
78 lines
2.6 KiB
Python
10 years ago
|
#!/usr/bin/env python
|
||
|
# -*- coding: utf-8 -*-
|
||
|
#
|
||
|
# openvz.py
|
||
|
#
|
||
|
# Copyright 2014 jordonr <jordon@beamsyn.net>
|
||
|
#
|
||
10 years ago
|
# This file is part of Ansible.
|
||
|
#
|
||
|
# Ansible is free software: you can redistribute it and/or modify
|
||
10 years ago
|
# it under the terms of the GNU General Public License as published by
|
||
10 years ago
|
# the Free Software Foundation, either version 3 of the License, or
|
||
10 years ago
|
# (at your option) any later version.
|
||
|
#
|
||
10 years ago
|
# Ansible is distributed in the hope that it will be useful,
|
||
10 years ago
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||
10 years ago
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||
10 years ago
|
# GNU General Public License for more details.
|
||
|
#
|
||
|
# You should have received a copy of the GNU General Public License
|
||
10 years ago
|
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
|
||
10 years ago
|
#
|
||
|
# Inspired by libvirt_lxc.py inventory script
|
||
|
# https://github.com/ansible/ansible/blob/e5ef0eca03cbb6c8950c06dc50d0ca22aa8902f4/plugins/inventory/libvirt_lxc.py
|
||
|
#
|
||
|
# Groups are determined by the description field of openvz guests
|
||
|
# multiple groups can be seperated by commas: webserver,dbserver
|
||
|
|
||
|
from subprocess import Popen,PIPE
|
||
|
import sys
|
||
|
import json
|
||
|
|
||
|
|
||
|
#List openvz hosts
|
||
10 years ago
|
vzhosts = ['vzhost1','vzhost2','vzhost3']
|
||
|
#Add openvz hosts to the inventory and Add "_meta" trick
|
||
|
inventory = {'vzhosts': {'hosts': vzhosts}, '_meta': {'hostvars': {}}}
|
||
10 years ago
|
#default group, when description not defined
|
||
|
default_group = ['vzguest']
|
||
|
|
||
10 years ago
|
def get_guests():
|
||
|
#Loop through vzhosts
|
||
|
for h in vzhosts:
|
||
|
#SSH to vzhost and get the list of guests in json
|
||
|
pipe = Popen(['ssh', h,'vzlist','-j'], stdout=PIPE, universal_newlines=True)
|
||
|
|
||
|
#Load Json info of guests
|
||
|
json_data = json.loads(pipe.stdout.read())
|
||
|
|
||
|
#loop through guests
|
||
|
for j in json_data:
|
||
|
#Add information to host vars
|
||
|
inventory['_meta']['hostvars'][j['hostname']] = {'ctid': j['ctid'], 'veid': j['veid'], 'vpsid': j['vpsid'], 'private_path': j['private'], 'root_path': j['root'], 'ip': j['ip']}
|
||
10 years ago
|
|
||
10 years ago
|
#determine group from guest description
|
||
|
if j['description'] is not None:
|
||
|
groups = j['description'].split(",")
|
||
|
else:
|
||
|
groups = default_group
|
||
10 years ago
|
|
||
10 years ago
|
#add guest to inventory
|
||
|
for g in groups:
|
||
|
if g not in inventory:
|
||
|
inventory[g] = {'hosts': []}
|
||
10 years ago
|
|
||
10 years ago
|
inventory[g]['hosts'].append(j['hostname'])
|
||
10 years ago
|
|
||
10 years ago
|
return inventory
|
||
10 years ago
|
|
||
|
|
||
|
if len(sys.argv) == 2 and sys.argv[1] == '--list':
|
||
10 years ago
|
inv_json = get_guests()
|
||
|
print json.dumps(inv_json, sort_keys=True)
|
||
10 years ago
|
elif len(sys.argv) == 3 and sys.argv[1] == '--host':
|
||
10 years ago
|
print json.dumps({});
|
||
10 years ago
|
else:
|
||
10 years ago
|
print "Need an argument, either --list or --host <host>"
|