Pasha
Pasha

Reputation: 190

Ansible dynamic inventory python interpreter issue

I am trying to run a playbook from virtualenv and I am using ansible-dynamic inventory script. Problem is: The inventory script is using default system interpreter not the one I have passed using "ansible_python_interpreter" variable.

The playbook works fine if activate virtualenv and run from the command line. Also, the inventory script works fine when I run using /opt/myproj/.ve/bin/python3.6 inv.py. But the playbook is invoked remotely by a different python script, not from the command line.

/opt/myproj/.ve/bin/ansible-playbook playbooks/rotate_passwords.yml -i inv.py -e ansible_python_interpreter=/opt/myproj/.ve/bin/python3.6 -vvv
ansible-playbook 2.7.10
  config file = /opt/myproj/ansible.cfg
  configured module search path = ['/opt/myproj/library']
  ansible python module location = /opt/myproj/.ve/lib64/python3.6/site-packages/ansible
  executable location = /opt/myproj/.ve/bin/ansible-playbook
  python version = 3.6.8 (default, Apr 25 2019, 21:02:35) [GCC 4.8.5 20150623 (Red Hat 4.8.5-36)]
Using /opt/myproj/ansible.cfg as config file
setting up inventory plugins
/opt/myproj/inv.py did not meet host_list requirements, check plugin documentation if this is unexpected
/opt/myproj/inv.py did not meet yaml requirements, check plugin documentation if this is unexpected
/opt/myproj/inv.py did not meet auto requirements, check plugin documentation if this is unexpected
 [WARNING]:  * Failed to parse /opt/myproj/inv.py with script plugin: Inventory script (/opt/myproj/inv.py) had an execution error: Traceback (most recent call last):
File "/opt/myproj/inv.py", line 11, in <module>     import pymysql.cursors ModuleNotFoundError: No module named 'pymysql'

I expect the inv.py should get invoked with given ansible_python_interpreter where I have installed all pip modules.

Upvotes: 1

Views: 2486

Answers (1)

nitzmahone
nitzmahone

Reputation: 13940

Ansible inventory scripts don't receive any special Python substitution behavior like modules do. Since they can be written in any language, the only requirement is that the execute bit is set and a valid shebang is in place. So it's going to literally run whatever the shebang says.

A couple of options:

  • Make the shebang on the inventory script #!/usr/bin/env python - that should inherit the venv Python from the controller.
  • Rewrite the inventory script as an inventory plugin. It's more complex, but a lot more flexible, and runs inside the controller, so sidesteps any Python configuration issues once your controller is working properly.

Upvotes: 2

Related Questions