Revisit ansible module with virtualenv - library module is using wrong python

Hi Folks,

I’m having a problem getting my mdule ti run with my python virtual environment.

I’m running ansible with a python 3 virtual environment with other libraries I want to use in my modules.

(py363venvAns) [netutil@ncc657 ncans]$ ansible --version
ansible 2.5.3
config file = /opt/apps/netutil/ncans/ansible.cfg
configured module search path = [‘/home/netutil/.ansible/plugins/modules’, ‘/usr/share/ansible/plugins/modules’]
ansible python module location = /opt/apps/netutil/py363venvAns/lib/python3.6/site-packages/ansible
executable location = /opt/apps/netutil/py363venvAns/bin/ansible
python version = 3.6.3 (default, Jan 9 2018, 10:19:07) [GCC 4.8.5 20150623 (Red Hat 4.8.5-11)]
(py363venvAns) [netutil@ncc657 ncans]$

I’m trying to run a custom module that I’ve placed in the “library” directory from a playbook.

(py363venvAns) [netutil@ncc657 ncans]$ pwd
/opt/apps/netutil/ncans
(py363venvAns) [netutil@ncc657 ncans]$ tree -d
.
├── backup
├── data
├── group_vars
├── library
├── log
├── pycache
└── roles
└── gatherSFSwitchInfo.demo
├── defaults
├── files
├── handlers
├── meta
├── tasks
├── templates
├── tests
└── vars

16 directories
(py363venvAns) [netutil@ncc657 ncans]$

(py363venvAns) [netutil@ncc657 ncans]$ cd library
(py363venvAns) [netutil@ncc657 library]$ ll
total 16K
3938337 -rwxrwx— 1 netutil netauto 2.6K May 7 17:20 naam_nexusPopDb.py
3938341 -rwxrwx— 1 netutil netauto 2.3K May 16 10:45 naam_getNetautoTask.py
3939011 -rwxrwx— 1 netutil netauto 6.9K May 23 11:48 naam_updateNaTaskResults.py
(py363venvAns) [netutil@ncc657 library]$

When my module named naam_updateNaTaskResults.py gets called, it doesn’t seem to use the python environment I want. Below is some log output:

(py363venvAns) [netutil@ncc657 ncans]$ tail -f naam_updateNaTaskResults.log
Wed, 23 May 2018 11:46:41 DEBUG naam_updateNaTaskResults_v2 naTaskUpdateDevResults pushing task results for device 10.4.200.240
Wed, 23 May 2018 11:46:41 DEBUG naam_updateNaTaskResults_v2 naTaskUpdateDevResults - natResults = {‘failed’: False, ‘changed’: False, ‘ping’: ‘pong’}
Wed, 23 May 2018 11:48:42 INFO naam_updateNaTaskResults_v2 started ----------------------------------------------------------------------
Wed, 23 May 2018 11:48:42 DEBUG naam_updateNaTaskResults_v2 sys path is: [‘/opt/apps/netutil/ncans/’, ‘/tmp/ansible_kqNOOF’, ‘/tmp/ansible_kqNOOF/ansible_modlib.zip’, ‘/tmp/ansible_kqNOOF/ansible_modlib.zip’, ‘/usr/lib64/python27.zip’, ‘/usr/lib64/python2.7’, ‘/usr/lib64/python2.7/plat-linux2’, ‘/usr/lib64/python2.7/lib-tk’, ‘/usr/lib64/python2.7/lib-old’, ‘/usr/lib64/python2.7/lib-dynload’, ‘/usr/lib64/python2.7/site-packages’, ‘/usr/lib64/python2.7/site-packages/gtk-2.0’, ‘/usr/lib/python2.7/site-packages’]
Wed, 23 May 2018 11:48:42 INFO naam_updateNaTaskResults_v2 Input nataskid type: <type ‘str’>, value: 5b058d4e7c557204e05714c5, deviceIp: 10.4.200.240
Wed, 23 May 2018 11:48:42 DEBUG naam_updateNaTaskResults_v2 naTaskUpdateDevResults pushing task results for device 10.4.200.240
Wed, 23 May 2018 11:48:42 DEBUG naam_updateNaTaskResults_v2 naTaskUpdateDevResults - natResults = {‘failed’: False, ‘changed’: False, ‘ping’: ‘pong’}
Wed, 23 May 2018 11:48:42 DEBUG naam_updateNaTaskResults_v2 mdb update returned: not called, commented
Wed, 23 May 2018 11:48:42 INFO naam_updateNaTaskResults_v2 natask 5b058d4e7c557204e05714c5 updated for 10.4.200.240
Wed, 23 May 2018 11:48:42 INFO naam_updateNaTaskResults_v2 completed in 0.008 seconds

I was expecting to see something like this (as I get from my dynamic inventory script called via “-i” on ansible-playbook command:

Wed, 23 May 2018 12:01:37 DEBUG na_getNetautoTaskInventory_v3 sys path is: [‘/opt/apps/netutil/ncans/’, ‘/opt/apps/netutil/ncans’, ‘/opt/apps/netutil/py363venvAns/lib64/python36.zip’, ‘/opt/apps/netutil/py363venvAns/lib64/python3.6’, ‘/opt/apps/netutil/py363venvAns/lib64/python3.6/lib-dynload’, ‘/opt/rh/rh-python36/root/usr/lib64/python3.6’, ‘/opt/rh/rh-python36/root/usr/lib/python3.6’, ‘/opt/apps/netutil/py363venvAns/lib/python3.6/site-packages’]

Can anyone tell me how to make sure the modules from the library directory get called with my python virtual environment?

By the way, this is for network automation and using a dynamic inventory with vars like:
“ansible_connection”: “network_cli”,
“ansible_network_os”: “nxos”,
“ansible_become_method”: “enable”,
“ansible_become”: “no”

I tried setting:

#!/opt/apps/netutil/py363venvAns/bin/python

and

#!/usr/bin/env python

on my module. Neither worked for me.

I tried reading through past comments, but I couldn’t seem to find a proper solution that I could understand.

Thanks for any pointers.

Regards,

Rick

Hi,

Replying to my own post. It looks like setting and inventory var “ansible_python_interpreter”: “/opt/apps/netutil/py363venvAns/bin/python3” will tell the module what to do.

me.