ansible/contrib/inventory/openvz.py

86 lines
2.8 KiB
Python
Raw Normal View History

2014-07-30 17:08:22 +00:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
#
# openvz.py
#
# Copyright 2014 jordonr <jordon@beamsyn.net>
#
2014-08-13 17:28:43 +00:00
# This file is part of Ansible.
#
# Ansible is free software: you can redistribute it and/or modify
2014-07-30 17:08:22 +00:00
# it under the terms of the GNU General Public License as published by
2014-08-13 17:28:43 +00:00
# the Free Software Foundation, either version 3 of the License, or
2014-07-30 17:08:22 +00:00
# (at your option) any later version.
#
2014-08-13 17:28:43 +00:00
# Ansible is distributed in the hope that it will be useful,
2014-07-30 17:08:22 +00:00
# but WITHOUT ANY WARRANTY; without even the implied warranty of
2014-08-13 17:28:43 +00:00
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
2014-07-30 17:08:22 +00:00
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
2014-08-13 17:28:43 +00:00
# along with Ansible. If not, see <http://www.gnu.org/licenses/>.
2014-07-30 17:08:22 +00:00
#
# 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
2015-10-26 21:13:00 +00:00
# multiple groups can be separated by commas: webserver,dbserver
2014-07-30 17:08:22 +00:00
2017-05-09 21:38:08 +00:00
from subprocess import Popen, PIPE
2014-07-30 17:08:22 +00:00
import sys
import json
2017-05-09 21:38:08 +00:00
# List openvz hosts
vzhosts = ['vzhost1', 'vzhost2', 'vzhost3']
# Add openvz hosts to the inventory and Add "_meta" trick
2014-08-13 17:28:43 +00:00
inventory = {'vzhosts': {'hosts': vzhosts}, '_meta': {'hostvars': {}}}
2017-05-09 21:38:08 +00:00
# default group, when description not defined
2014-07-30 17:08:22 +00:00
default_group = ['vzguest']
2017-05-09 21:38:08 +00:00
2014-08-13 17:28:43 +00:00
def get_guests():
2017-05-09 21:38:08 +00:00
# Loop through vzhosts
2014-08-13 17:28:43 +00:00
for h in vzhosts:
2017-05-09 21:38:08 +00:00
# SSH to vzhost and get the list of guests in json
pipe = Popen(['ssh', h, 'vzlist', '-j'], stdout=PIPE, universal_newlines=True)
2014-08-13 17:28:43 +00:00
2017-05-09 21:38:08 +00:00
# Load Json info of guests
2014-08-13 17:28:43 +00:00
json_data = json.loads(pipe.stdout.read())
2017-05-09 21:38:08 +00:00
# loop through guests
2014-08-13 17:28:43 +00:00
for j in json_data:
2017-05-09 21:38:08 +00:00
# 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']
}
2014-07-30 17:08:22 +00:00
2017-05-09 21:38:08 +00:00
# determine group from guest description
2014-08-13 17:28:43 +00:00
if j['description'] is not None:
groups = j['description'].split(",")
else:
groups = default_group
2014-07-30 17:08:22 +00:00
2017-05-09 21:38:08 +00:00
# add guest to inventory
2014-08-13 17:28:43 +00:00
for g in groups:
if g not in inventory:
inventory[g] = {'hosts': []}
2014-07-30 17:08:22 +00:00
2014-08-13 17:28:43 +00:00
inventory[g]['hosts'].append(j['hostname'])
2014-07-30 17:08:22 +00:00
2014-08-13 17:28:43 +00:00
return inventory
2014-07-30 17:08:22 +00:00
if len(sys.argv) == 2 and sys.argv[1] == '--list':
2014-08-13 17:28:43 +00:00
inv_json = get_guests()
print(json.dumps(inv_json, sort_keys=True))
2014-07-30 17:08:22 +00:00
elif len(sys.argv) == 3 and sys.argv[1] == '--host':
print(json.dumps({}))
2014-07-30 17:08:22 +00:00
else:
print("Need an argument, either --list or --host <host>")