Search code examples
pythonimportansibleansible-inventory

import ansible.module_utils in 2.2.1.0 as part of inventory module


Importing UTILS classes into Inventory - can it be done?

I have created a custom LDAP data importer as part of creating my inventory class. The LDAP schema we have wasn't similar enough to the LDAP plugin provided in samples.

My class is called ldapDataModule; the class is in:

/home/agt/ansible/agt_module_utils/ldapDataModule.py

My "$HOME/.ansible.cfg" file has the following:

module_utils = /home/agt/ansible/agt_module_utils

When running my Ansible inventory module, I get the following output:

ansible ecomtest37 -m ping ERROR! Attempted to execute "/sites/utils/local/ansible/hosts" as inventory script: Inventory script (/sites/utils/local/ansible/hosts) had an execution error: Traceback (most recent call last): File "/sites/utils/local/ansible/hosts", line 22, in from ansible.module_utils import ldapDataModule ImportError: No module named module.utils

The include statement inside hosts appears like this:

import copy
import ldap
import re
import sys
import operator
import os
import argparse
import datetime
import os.path
try:
    import json
except:
    import simplejson as json
from ansible.module_utils import ldapDataModule

class agtInventory(object):

RECOMENDATIONS?


Solution

  • I was able to do the following as a "work around". I'd still like to hear from Ansible guru's on proper use of "module_utils" variable from ansible.cfg

    sys.path.insert(0, '/home/agt/ansible/agt_module_utils')
    
    from ldapDataModule import ldapDataModule