# python Dynamic Inventory won't work: it is interpreted as a .ini file

**URL:** <https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445>\
**Category:** Ansible Project\
**Created:** [June 11, 2014, 6:13pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445 "2014-06-11T18:13:11Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Pierre\_Masci](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@Pierre\_Masci](https://forum.ansible.com/u/Pierre_Masci)\
**Post date:** [June 11, 2014, 6:13pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445/1 "2014-06-11T18:13:11Z")

</div>

Hi,

I’m trying the simplest possible example of a [Dynamic Inventory](http://docs.ansible.com/developing_inventory.html) but it won’t work: my hosts.py file gets interpreted as a .ini file:

$ ansible-playbook -i **hosts.py** play.yml  
ERROR: **Invalid ini entry** : sys - need more than 1 value to unpack

Here is the dynamic inventory **hosts.py** :

import sys  
import json

if len(sys.argv) == 2 and (sys.argv[1] == ‘–list’):  
inventory={}  
inventory[‘local’] = [‘127.0.0.1’]  
print json.dumps(inventory, indent=4)  
elif len(sys.argv) == 3 and (sys.argv[1] == ‘–host’):  
print ‘{}’  
else:  
print “Usage: %s --list or --host ” % sys.argv[0]  
sys.exit(1)

I’m pretty sure that it returns what it should. It’s a much simpler example of the one shown [there](http://jpmens.net/2013/06/18/adapting-inventory-for-ansible/).

And the playbook **play.yml** could not be simpler:

- hosts: all  
connection: local  
tasks:
- name: Can I run this playbook?  
debug: msg=“Yes!”

The playbook works when I run it playbook locally with this command:

$ ansible-playbook -i 127.0.0.1, play.yml

[output not copied here - it’s all fine]

I am stuck there.  
**What am I doing wrong** with my dynamic inventory?  
How can I get it to be evaluated as a python file, rather than a .ini file?

---

<div class="post-metadata">

**Author:** ![Serge\_van\_Ginderacht](https://avatars.discourse-cdn.com/v4/letter/s/b782af/32.png) [@Serge\_van\_Ginderacht](https://forum.ansible.com/u/Serge_van_Ginderacht)\
**Post date:** [June 11, 2014, 6:45pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445/2 "2014-06-11T18:45:34Z")

</div>

Sounds like you forgot to

chmod +x hosts.py

---

<div class="post-metadata">

**Author:** ![Scott\_Sturdivant](https://avatars.discourse-cdn.com/v4/letter/s/f14d63/32.png) [@Scott\_Sturdivant](https://forum.ansible.com/u/Scott_Sturdivant)\
**Post date:** [June 11, 2014, 6:50pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445/3 "2014-06-11T18:50:04Z")

</div>

Similarly, don’t forget the shebang line at the top of your inventory script. e.g. '#!/usr/bin/env python`

---

<div class="post-metadata">

**Author:** ![Pierre\_Masci](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@Pierre\_Masci](https://forum.ansible.com/u/Pierre_Masci)\
**Post date:** [June 11, 2014, 6:55pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445/4 "2014-06-11T18:55:02Z")

</div>

Oh so silly of me, thank you Serge and Scott ! Now it works.

Perhaps these info could be added to this documentation page:  
[http://docs.ansible.com/developing\_inventory.html](http://docs.ansible.com/developing_inventory.html)  
I could submit a pull request for that.

---

<div class="post-metadata">

**Author:** ![Pierre\_Masci](https://avatars.discourse-cdn.com/v4/letter/p/7c8e57/32.png) [@Pierre\_Masci](https://forum.ansible.com/u/Pierre_Masci)\
**Post date:** [June 11, 2014, 7:01pm UTC](https://forum.ansible.com/t/python-dynamic-inventory-wont-work-it-is-interpreted-as-a-ini-file/15445/5 "2014-06-11T19:01:27Z")

</div>

And now my more complex dynamic inventory works. Easy peasy, cheers! I’m loving Ansible 🙂
