added ability to create vars from vbox properties
This commit is contained in:
parent
5cd0074831
commit
21da284f5d
1 changed files with 24 additions and 12 deletions
|
@ -38,7 +38,9 @@ EXAMPLES:
|
||||||
simple_config_file:
|
simple_config_file:
|
||||||
plugin: virtualbox
|
plugin: virtualbox
|
||||||
settings_password_file: /etc/virtulbox/secrets
|
settings_password_file: /etc/virtulbox/secrets
|
||||||
compose:
|
query: # create vars from virtualbox properties
|
||||||
|
logged_in_users: /VirtualBox/GuestInfo/OS/LoggedInUsersList
|
||||||
|
compose: # compose vars from jinja2 expressions
|
||||||
ansible_connection: ('indows' in vbox_Guest_OS)|ternary('winrm', 'ssh')
|
ansible_connection: ('indows' in vbox_Guest_OS)|ternary('winrm', 'ssh')
|
||||||
'''
|
'''
|
||||||
from __future__ import (absolute_import, division, print_function)
|
from __future__ import (absolute_import, division, print_function)
|
||||||
|
@ -57,6 +59,20 @@ class InventoryModule(BaseInventoryPlugin):
|
||||||
''' Host inventory parser for ansible using external inventory scripts. '''
|
''' Host inventory parser for ansible using external inventory scripts. '''
|
||||||
|
|
||||||
NAME = 'virtualbox'
|
NAME = 'virtualbox'
|
||||||
|
VBOX = "VBoxManage"
|
||||||
|
|
||||||
|
def query_vbox_data(self, host, property_path):
|
||||||
|
ret = None
|
||||||
|
try:
|
||||||
|
cmd = [self.VBOX, 'guestproperty', 'get', host, property_path]
|
||||||
|
x = Popen(cmd, stdout=PIPE)
|
||||||
|
ipinfo = x.stdout.read()
|
||||||
|
if 'Value' in ipinfo:
|
||||||
|
a, ip = ipinfo.split(':', 1)
|
||||||
|
ret = ip.strip()
|
||||||
|
except:
|
||||||
|
pass
|
||||||
|
return ret
|
||||||
|
|
||||||
def verify_file(self, path):
|
def verify_file(self, path):
|
||||||
|
|
||||||
|
@ -71,7 +87,6 @@ class InventoryModule(BaseInventoryPlugin):
|
||||||
|
|
||||||
super(InventoryModule, self).parse(inventory, loader, path)
|
super(InventoryModule, self).parse(inventory, loader, path)
|
||||||
|
|
||||||
VBOX = "VBoxManage"
|
|
||||||
cache_key = self.get_cache_prefix(path)
|
cache_key = self.get_cache_prefix(path)
|
||||||
|
|
||||||
# file is config file
|
# file is config file
|
||||||
|
@ -91,7 +106,7 @@ class InventoryModule(BaseInventoryPlugin):
|
||||||
running = data.get('running_only', False)
|
running = data.get('running_only', False)
|
||||||
|
|
||||||
# start getting data
|
# start getting data
|
||||||
cmd = [VBOX, 'list', '-l']
|
cmd = [self.VBOX, 'list', '-l']
|
||||||
if running:
|
if running:
|
||||||
cmd.append('runningvms')
|
cmd.append('runningvms')
|
||||||
else:
|
else:
|
||||||
|
@ -134,15 +149,7 @@ class InventoryModule(BaseInventoryPlugin):
|
||||||
hostvars[current_host] = {}
|
hostvars[current_host] = {}
|
||||||
self.inventory.add_host(current_host)
|
self.inventory.add_host(current_host)
|
||||||
# try to get network info
|
# try to get network info
|
||||||
try:
|
self.inventory.set_variable(current_host, 'ansible_host', self.query_vbox_data(current_host, netinfo))
|
||||||
cmd = [VBOX, 'guestproperty', 'get', current_host, netinfo]
|
|
||||||
x = Popen(cmd, stdout=PIPE)
|
|
||||||
ipinfo = x.stdout.read()
|
|
||||||
if 'Value' in ipinfo:
|
|
||||||
a, ip = ipinfo.split(':', 1)
|
|
||||||
self.inventory.set_variable(current_host, 'ansible_host', ip.strip())
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
# found groups
|
# found groups
|
||||||
elif k == 'Groups':
|
elif k == 'Groups':
|
||||||
|
@ -168,6 +175,11 @@ class InventoryModule(BaseInventoryPlugin):
|
||||||
# set vars in inventory from hostvars
|
# set vars in inventory from hostvars
|
||||||
for host in hostvars:
|
for host in hostvars:
|
||||||
|
|
||||||
|
# create vars from vbox properties
|
||||||
|
if data.get('query') and isinstance(data['query'], dict):
|
||||||
|
for varname in data['query']:
|
||||||
|
hostvars[host][varname] = self.query_vbox_data(host, data['query'][varname])
|
||||||
|
|
||||||
# create composite vars
|
# create composite vars
|
||||||
if data.get('compose') and isinstance(data['compose'], dict):
|
if data.get('compose') and isinstance(data['compose'], dict):
|
||||||
for varname in data['compose']:
|
for varname in data['compose']:
|
||||||
|
|
Loading…
Reference in a new issue