2
votes

Certain variables in ansible.cfg seem to not be taking affect. Ansible 2.2.1.0 Python 2.7.10 Mac OS Version 10.12.5

We have created a custom class that will be used in our custom Ansible module. The class lives here:

/sites/utils/local/ansible/agt_module_utils/ldapData.py /sites/utils/local/ansible/agt_module_utils/init.py

The class inside ldapData.py is named:

class ldapDataClass(object):

In all cases, init.py is a zero byte file. Our Ansible module is located here:

/sites/utils/local/ansible/agt_modules/init.py /sites/utils/local/ansible/agt_modules/agtWeblogic.py

The import statement in atgWeblogic looks as follows:

from ansible.module_utils.ldapData import ldapDataClass

I have also tried:

from ldapData import ldapDataClass

The config file has the following lines:

library = /sites/utils/local/ansible/att_modules module_utils = /sites/utils/local/ansible/att_module_utils

When running our module, the modules directory IS resolved but the module_utils directory is not resolved. When the include is "from ansible.module_utils.ldapData import ldapDataClass" the failure is before ansible even connects to the remote machine. When the include is "from ldapData import ldapDataClass" the failure is on the remote machine. Below I am showing the failure "on the remote machine" (scroll right to see full error):

nverkland@local>ansible ecomtest37 -m agtWeblogic -a "action=stop instances=tst37-shop-main" -vvv

ecomtest37 | FAILED! => {
    "changed": false, 
    "failed": true, 
    "invocation": {
        "module_name": "agtWeblogic"
    }, 
    "module_stderr": "", 
    "module_stdout": "Traceback (most recent call last):\r\n  File \"/tmp/ansible_FwHCmh/ansible_module_attWeblogic.py\", line 63, in <module>\r\n    from ldapData import ldapDataClass\r\nImportError: No module named ldapData\r\n", 
    "msg": "MODULE FAILURE"
}

If I move the ldapData.py file into the "ansible installed" module_utils directory (/Library/Python/2.7/site-packages/ansible/module_utils/ on my Mac) the module runs fine. What have I done incorrectly in my config file that has prevented the use of my "custom" module_utils directory?

Thanks.

2

2 Answers

7
votes

module_utils path is configurable since Ansible 2.3: changelog, PR.

I guess you have to upgrade or refactor the module.

Update: working example

Project tree:

.
├── ansible
│   └── ansible.cfg
├── module_utils
│   └── mycommon.py
└── modules
    └── test_module.py

ansible.cfg:

[defaults]
library = ../modules
module_utils = ../module_utils

mycommon.py:

class MyCommonClass(object):

    @staticmethod
    def hello():
        return 'world'

test_module.py:

#!/usr/bin/python

from ansible.module_utils.basic import *
from ansible.module_utils.mycommon import MyCommonClass

module = AnsibleModule(
    argument_spec = dict()
)
module.exit_json(changed=True, hello=MyCommonClass.hello())

Execution:

$ ansible localhost -m test_module
 [WARNING]: Host file not found: /etc/ansible/hosts

 [WARNING]: provided hosts list is empty, only localhost is available

localhost | SUCCESS => {
    "changed": true,
    "hello": "world"
}

Ansible version:

$ ansible --version
ansible 2.3.1.0
  config file = /<masked>/ansible/ansible.cfg
  configured module search path = [u'../modules']
  python version = 2.7.10 (default, Feb  7 2017, 00:08:15) [GCC 4.2.1 Compatible Apple LLVM 8.0.0 (clang-800.0.34)]
0
votes

Konstantin's notes helped. I managed to solve the riddle. In my config file I had:

module_utils = /sites/utils/local/ansible/agt_module_utils

This causes Ansible to think that my class is found at:

import ansible.agt_module_utils.ldapData (working)

until now I was attempting to find the class at:

import ansible.module_utils.ldapData (broken)