Ansible: AWS dynamic inventory Python script fails to run

Created on 19 Aug 2016  ·  2Comments  ·  Source: ansible/ansible

ISSUE TYPE
  • Bug Report
    COMPONENT NAME

Dynamic Inventory: ec2.py

ANSIBLE VERSION
ansible 2.1.0.0
Python 2.7.10
OS / ENVIRONMENT

Mac OS X 10.11.6

SUMMARY

Running ec2.py standalone causes import error.

STEPS TO REPRODUCE
  1. Download the ec2.py dynamic inventory script and ec2.ini file as instructed in the docs.
  2. Run the script with Python 2.7
python ec2.py --list
Traceback (most recent call last):
  File "ec2.py", line 134, in <module>
    from ansible.module_utils import ec2 as ec2_utils
ImportError: No module named ansible.module_utils
EXPECTED RESULTS

A list of instances

ACTUAL RESULTS

Error above.

Workaround

If I comment out the line from ansible.module_utils import ec2 as ec2_utils the script runs fine.

The line was introduced in #16606.

aws bug cloud

Most helpful comment

Great to hear it's not a bug. Is this described somewhere in the documentation?

I figured out what caused it at my end: installing Ansible with brew made ansible unavailable to my Python interpreter.

To fix this I uninstalled Ansible and installed it again using pip:

brew uninstall ansible
pip install ansible

Now ansible is available:

python -c "import ansible;print(ansible.__version__)"
2.1.1.0

and the script works as expected.

It may be worth mentioning this somewhere in the documentation, or have the script throw an informative error if ansible is not available in the python path.

All 2 comments

@jonatanblue that import was added by https://github.com/ansible/ansible/commit/59e499f8f00e4068b68c147b8f3002535ad706e0 which appears to be an attempt to normalize the boto client setup code.

The implication is that in order to use the inventory script, you need to have ansible installed on the machine such that "ansible" is available in the python path for your default python interpreter. We plan to make more use of the ansible libraries in the dynamic inventory scripts over time, so I am not going to revert this change.

Great to hear it's not a bug. Is this described somewhere in the documentation?

I figured out what caused it at my end: installing Ansible with brew made ansible unavailable to my Python interpreter.

To fix this I uninstalled Ansible and installed it again using pip:

brew uninstall ansible
pip install ansible

Now ansible is available:

python -c "import ansible;print(ansible.__version__)"
2.1.1.0

and the script works as expected.

It may be worth mentioning this somewhere in the documentation, or have the script throw an informative error if ansible is not available in the python path.

Was this page helpful?
0 / 5 - 0 ratings