Actually... I'd been meaning to create dynamic inventory script myself, so I had a go based on your code above (many thanks).
You might need a #! at the top of the script (looking at some of the other examples) #!/usr/bin/env python # -*- coding: utf-8 -*- Only other thing I had to do was remove all my debug output so the response only contained json, otherwise I got the error you describe above. Hope this helps, Jon On Monday, April 25, 2016 at 2:14:26 PM UTC+1, J Hawkesworth wrote: > > Looks to me like its not running your script, just trying to parse it as a > static inventory file. > > Did you set your non-essential-servers.py script to be executable? > > Looking here: > http://docs.ansible.com/ansible/intro_dynamic_inventory.html#using-inventory-directories-and-multiple-inventory-sources > > it says > > "In an inventory directory, executable files will be treated as dynamic > inventory sources and most other files as static sources." > > So I think you might just need to > > chmod u+x non-essential-servers.py > > > > On Monday, April 25, 2016 at 1:11:12 PM UTC+1, Jared Jenkins wrote: >> >> Hello all, I'm new to python and ansible in general. I feel like I can't >> use this unless I plug into AD. I spent a great deal of time trying to >> learn python for a dynamic inventory and I'm completely stumped. The json >> seems to check out with the doc almost exact. >> >> I'm running ansible from os x using python 2.7.11 and ansible 2.0.1.0 >> >> Here's my python script >> >> non-essential-servers.py >> ============================== >> # import class and constants >> from ldap3 import Server, Connection, ALL, NTLM, Tls >> import json >> import ssl >> >> #prompt for ldapusername and password >> #ldapusr = ("playnetwork.com\\") >> #ldapusr += raw_input("Username (use a read-only account):") >> #ldappwd = getpass.getpass("Password for " + ldapusr + ":") >> >> >> # define the server and the connection >> >> server = Server('reddc2.playnetwork.com', use_ssl=True) >> conn = Connection(server, user="domain\\username", password="password", >> authentication=NTLM, auto_bind=True) >> #conn = Connection(server, user="playnetwork.com\\ldapusr", >> password=ldappwd, authentication=NTLM, auto_bind=True) >> >> conn.open() >> conn.bind() >> conn.entries >> conn.search('OU=Servers, OU=PlayNetwork, DC=playnetwork, DC=com', >> '(objectclass=computer)', attributes=["name"]) >> >> names = [] >> for x in conn.response: >> names.append(x['attributes']['name'][0]) >> >> conn.unbind() >> >> hosts = {} >> hosts['hosts'] = names >> var = {'a':True} >> >> export = {} >> export['servers'] = hosts >> export['vars'] = var >> print json.dumps(export, sort_keys=True, indent=4, separators=(',', ': ')) >> ==================== >> Simplied the json looks like this >> { >> "servers": { >> "hosts": [ >> "SERVER1", >> "SERVER2", >> "SERVER3", >> ] >> }, >> "vars": { >> "a": true >> } >> } >> >> when I run >> ansible -i non-essential-servers.py all -m win_ping >> >> I get the error >> ERROR! non-essential-servers.py:2: Expected key=value host variable >> assignment, got: ldap3 >> >> I feel like I'm not "printing" like I should or not closing my ldap >> connection properly. >> Thank you! >> > -- 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 [email protected]. To post to this group, send email to [email protected]. To view this discussion on the web visit https://groups.google.com/d/msgid/ansible-project/9416a044-3396-49b5-aaa0-6de901cced37%40googlegroups.com. For more options, visit https://groups.google.com/d/optout.
