wrote a dynamic inventory for ansible, which seems to run fine on its own, but when run in ansible, it complains that it cant import yaml.
a script that just cats out the output (cut and paste) of the first script works just fine. the parts that actually make and configure the virtual machines and networks works fine, so thats stripped out.
im on os x 10.10.4, running ansible 1.9.2 from homebrew
pixel$ ./pransible-stripped.py --list
{
"fileserver": {
...
pixel$ ansible -i ./pransible-stripped.py all --list-hosts
ERROR: Inventory script (./pransible-stripped.py) had an execution error: Traceback (most recent call last):
File "/Users/pixel/pransible-stripped.py", line 4, in <module>
import yaml
File "/usr/local/Cellar/ansible/1.9.2/libexec/vendor/lib/python2.7/site-packages/yaml/__init__.py", line 2, in <module>
from error import *
ImportError: No module named 'error'
pixel$ cat pransible-stripped.sh
#!/bin/sh
cat<<EOF
{
"fileserver": {
...
pixel$ ansible -i ./pransible-stripped.sh all --list-hosts
erp
rndweb
rndrouter
fileserver
heres the script,
============
#!/usr/bin/env python3
import json
import yaml
class ProxSession:
"""Connection to a proxmox host or cluster for setting or getting kvm instances"""
def __init__(self,config_file):
with open(config_file) as fp:
self.siteconfig = yaml.load(fp.read())
def inventory(self):
i = {'_meta':{'hostvars':{}}}
for template,clones in self.siteconfig['clones'].items():
for clone,v in clones.items():
if 'groups' in v:
for group in v['groups']:
if group not in i:
i[group] = {'hosts':[clone]}
else:
i[group]['hosts'].append(clone)
if 'vars' in v:
i['_meta']['hostvars'][clone] = v['vars']
if 'group_vars' in self.siteconfig:
for k,v in self.siteconfig['group_vars'].items():
i[k]['vars'] = v
return json.dumps(i,indent=2)
if __name__ == '__main__':
p = ProxSession('test.yaml')
print (p.inventory())
exit
and the input, (those networks only exist in the test environment)
==========
dns:
domain:
example.com nameservers:
- 192.168.113.10
- 8.8.8.8
upstream_nameservers:
- 8.8.8.8
- 8.8.4.4
networks:
production:
bridge: vmbr0
network: 192.168.113.0
netmask: 255.255.255.0
gateway: 192.168.113.2
finance:
bridge: vmbr0
tag: 115
network: 192.168.115.0
netmask: 255.255.255.0
gateway: 192.168.115.1
rnd:
bridge: vmbr1
network: 10.10.10.0
netmask: 255.255.255.0
gateway: 10.10.10.1
templates:
ubuntu-14.04:
netconfig: debian
net:
model: virtio
clones:
ubuntu-14.04:
# production
fileserver:
net0:
network: production
address: 192.168.113.30
cores: 2
memory: 256
disk2:
size: 10G
groups:
- fileserver
vars:
quest: to find the holy grail
# RND
rndrouter:
net0:
address: 192.168.113.31
network: production
flags:
- primary
- default
net1:
network: rnd
address: 10.10.10.1
groups:
- dhcpserver
vars:
dnsmasq_interface: eth1
rndweb:
net0:
network: rnd
address: 10.10.10.5
groups:
- web
# finanace
erp:
net0:
network: finance
address: 192.168.115.10
groups:
- web
group_vars:
web:
favorite_color: yellow