FS#54978 - [ansible] No module named 'ansible' in custom python script

Attached to Project: Community Packages
Opened by Anton Latukha (AntonLatukha) - Sunday, 30 July 2017, 16:47 GMT
Last edited by Eli Schwartz (eschwartz) - Sunday, 30 July 2017, 16:52 GMT
Task Type Bug Report
Category Packages
Status Closed
Assigned To No-one
Architecture All
Severity High
Priority Normal
Reported Version
Due in Version Undecided
Due Date Undecided
Percent Complete 100%
Votes 1
Private No

Details

Description:

I got caught between two bugs with package and I can't find a proper way out.

I am doing Dynamic Inventory for AWS: http://docs.ansible.com/ansible/latest/intro_dynamic_inventory.html#example-aws-ec2-external-inventory-script

In one way. While I try to use downloaded script (as thet bluntly describe in documentation)(script https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py). - I am getting:

```
ansible -i ec2.py -u ... ... -m ping
ERROR! Attempted to execute "ec2.py" as inventory script: Inventory script (ec2.py) had an execution error: Traceback (most recent call last):
File "/path/ec2.py", line 138, in <module>
from ansible.module_utils import ec2 as ec2_utils
ModuleNotFoundError: No module named 'ansible'
```

I am inclined to use already provided ansible and modules in /usr/lib/python2.7/site-packages/.
So I modify PYTHONPATH:

```
export PYTHONPATH='/usr/lib/python2.7/site-packages:'
```

And now importing works:
```
$ python -c "import ansible;print(ansible.__version__)"
2.3.1.0
```

And now I am getting:
```
ERROR! Attempted to execute "ec2.py" as inventory script: Inventory script (ec2.py) had an execution error: Traceback (most recent call last):
File "/path/ec2.py", line 127, in <module>
import argparse
File "/usr/lib/python3.6/argparse.py", line 89, in <module>
import re as _re
File "/usr/lib/python3.6/re.py", line 142, in <module>
class RegexFlag(enum.IntFlag):
AttributeError: module 'enum' has no attribute 'IntFlag'
```

Answers to that - is to unset PYTHONPATH.

Ansible seems to use a mix of Python 2.7 and 3.6

I also tried:

```
ansible -i ec2.py -u ... ... -m ping -e 'ansible_python_interpreter=/usr/bin/python2'
```
and also
```
ansible -i ec2.py -u ... ... -m ping -e 'ansible_python_interpreter=/usr/bin/python3'
```

And both results in exactly as above: `AttributeError: module 'enum' has no attribute 'IntFlag'` error.

Additional info:
* package version(s)

Recent ones:
ansible 2.3.1.0-1
Python 2.7.13
Python 3.6.2

* config and/or log files etc.


Steps to reproduce:
As described in Ansible doc: download
$ curl -L https://raw.githubusercontent.com/ansible/ansible/devel/contrib/inventory/ec2.py -o ./ec2.py

Provide with proper AWS access credentials:
$ export AWS_ACCESS_KEY_ID=
$ export AWS_SECRET_ACCESS_KEY=

$ chmod u+x ./ec2.py
$ ./ec2.py --list
Traceback (most recent call last):
File "./ec2.py", line 138, in <module>
from ansible.module_utils import ec2 as ec2_utils
ModuleNotFoundError: No module named 'ansible'

$ ansible -i ec2.py -u ... ... -m ping
ERROR! Attempted to execute "ec2.py" as inventory script: Inventory script (ec2.py) had an execution error: Traceback (most recent call last):
File "/path/ec2.py", line 138, in <module>
from ansible.module_utils import ec2 as ec2_utils
ModuleNotFoundError: No module named 'ansible'

Also as you know Ansible from 2.2 transitions to using Python3.
I could not get it working not with Python2, nor 3.
I sadly think to mimic Mac devs with their virtualenv world now.


P.S.
Thank you.
Wish I shared helpful info. Also happy to understand this problem.

Anton Latukha
This task depends upon

Closed by  Eli Schwartz (eschwartz)
Sunday, 30 July 2017, 16:52 GMT
Reason for closing:  Not a bug
Additional comments about closing:  That script does not appear to be PEP 394 compliant, fix the shebang

Loading...