Discussion:
[ansible-project] dynamic inventory script doesnt run under ansible, but a script that cats its output works just fine.
pixel fairy
2015-07-15 03:39:59 UTC
Permalink
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
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/ff5555db-ccbb-4d22-90e8-9e2c71ef24d6%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-15 03:47:50 UTC
Permalink
so ansible uses python2 which would require yaml to work and seems to
be installed correctly, but your script uses python3, did you install
yaml for python 3?
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8kJC_%2BBvHYhRv8zScSAnCRmSatXGNmMPa%3DJpFOidy_-ow%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
pixel fairy
2015-07-15 14:14:08 UTC
Permalink
Yes, the rest the script (stripped out for brevity) works fine. checked for
all needed packages in both versions of python, but ansible insists on its
own (python2) version of yaml. is there a way around that? like telling
ansible not to use its own yaml?

ill probably just generate a static inventory file, but that seems silly
when the only issue a different version of the same language.
Post by Brian Coca
so ansible uses python2 which would require yaml to work and seems to
be installed correctly, but your script uses python3, did you install
yaml for python 3?
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/feb55909-079b-44e1-b05f-2bb6ce4a24aa%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-15 17:35:34 UTC
Permalink
ansible is not responsible for the yaml version in this case, that
depens on your script, ansible just executes invenory scripts, it does
not even know if it is python, ruby or C, just that it is executable.

are you setting PYTHONPATH for ansible? in that case the env variable
would get inherited by the subprocess.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8%3D-QcQ6m4sx5pwb6p-_ZQ64JYRLpSj%2BEt9N0Vf7NygHvA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
pixel fairy
2015-07-15 21:09:55 UTC
Permalink
turns out homebrew set that python path. running from git works as it
should.

pixel$ cat `which ansible`
#!/bin/bash
PYTHONPATH="/usr/local/Cellar/ansible/1.9.2/libexec/lib/python2.7/site-packages:/usr/local/Cellar/ansible/1.9.2/libexec/vendor/lib/python2.7/site-packages"
Post by Brian Coca
ansible is not responsible for the yaml version in this case, that
depens on your script, ansible just executes invenory scripts, it does
not even know if it is python, ruby or C, just that it is executable.
are you setting PYTHONPATH for ansible? in that case the env variable
would get inherited by the subprocess.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9976a12a-35ad-48fe-ab9b-978628120314%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
Brian Coca
2015-07-15 21:14:30 UTC
Permalink
This is one reason we removed homebrew from our instructions on how to
install ansible.
--
Brian Coca
--
You received this message because you are subscribed to the Google Groups "Ansible Project" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ansible-project+***@googlegroups.com.
To post to this group, send email to ansible-***@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/CAJ5XC8mkCCFsh7CwrrEoueME8D-n_xjoam2XiTXSqjUH%2BPkbZg%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.
Loading...