dynamic inventory script doesnt run under ansible, but a script that cats its output works just fine.

268 views
Skip to first unread message

pixel fairy

unread,
Jul 14, 2015, 11:40:00 PM7/14/15
to ansible...@googlegroups.com
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


Brian Coca

unread,
Jul 14, 2015, 11:47:58 PM7/14/15
to ansible...@googlegroups.com
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

pixel fairy

unread,
Jul 15, 2015, 10:14:09 AM7/15/15
to ansible...@googlegroups.com
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.

Brian Coca

unread,
Jul 15, 2015, 1:35:42 PM7/15/15
to ansible...@googlegroups.com
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

pixel fairy

unread,
Jul 15, 2015, 5:09:56 PM7/15/15
to ansible...@googlegroups.com
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" exec "/usr/local/Cellar/ansible/1.9.2/libexec/bin/ansible" "$@"

Brian Coca

unread,
Jul 15, 2015, 5:14:35 PM7/15/15
to ansible...@googlegroups.com
This is one reason we removed homebrew from our instructions on how to
install ansible.

--
Brian Coca
Reply all
Reply to author
Forward
0 new messages