ISSUE TYPE
- Bug Report
COMPONENT NAME
ansible-playbook
ANSIBLE VERSION
ansible-playbook --version
ansible-playbook 2.2.1.0
config file = /automation/ansible/playbooks/ansible.cfg
configured module search path = ['library', '../library']
CONFIGURATION
ansible.cfg
[defaults]
forks = 50
transport = smart
# gathering defaults to 'smart', change to 'explicit'
gathering = explicit
library = library:../library
action_plugins = action_plugins
callback_plugins = callback_plugins
lookup_plugins = lookup_plugins
vars_plugins = vars_plugins
filter_plugins = filter_plugins
roles_path = roles:provisioning/roles
bin_ansible_callbacks = True
force_color = 1
host_key_checking = False
retry_files_enabled = False
[ssh_connection]
ssh_args = -o ControlMaster=auto -o ControlPersist=1h -vvv
pipelining = True
OS / ENVIRONMENT
CenOS 6.x
SUMMARY
We were using Ansible 1.8.2, and with this version we can have a custom filter plugin (named exampleFilter.py for example) with an import line:
import customFilter
where the customFilter.py was located in the same path. This would load the custom filter functions from customFilter.py.
Now, since upgrading to Ansible 2.2, this line fails. I cannot find an alternative way to import the customFilter.py from the exampleFilter.py. These 2 files are located within the ansible/filter_plugins/ path as defined in the ansible.cfg.
STEPS TO REPRODUCE
- create a simple playbook that uses a filter function from the exampleFilter
- create 2 filter plugins (exampleFilter.py and customFilter.py)
- add the following line to the beginning of exampleFilter.py:
import customFilter
- run the playbook
- error is produced in Ansible 2.x but not using Ansible 1.8.2
NOTE: We do not set the PYTHONPATH environment variable. There is no documentation from Ansible that we need to set this environment variable or that something has changed in Ansible 2.x. Can you please explain what exactly has changed and why this behavior is different and what we need to specifically do? This should be documented somewhere.
EXPECTED RESULTS
The filter plugin exampleFilter.py imports the custom filter functions from customFilter.py
ACTUAL RESULTS
ERROR! Unexpected Exception: failed to import customFilter
NOTES
I would actually assume that the “ansible.cfg” tells Ansible the location of the plugins and modules and Ansible uses this information to append these folders to the internal PYTHONPATH so that these modules and plugins can be found and we can import them at any time in our code. It make no sense to set the PYTHONPATH environment variable in addition to the ansible.cfg which also points to the location of our custom Python plugins and modules.
I noticed that there is a difference in the sys.modules that contains the list of all python modules that are loaded. When we use “import customFilter”, it searches in the dictionary of modules (sys.modules). In Ansible 1.x these modules have keys with proper Python namespaces using periods (.), however in Ansible 2.x these same modules or plugins are loaded and the key is now the full path with forward-slashes and not using periods (.). Why the change? It seems these modules or plugins are added to sys.modules incorrectly? Or perhaps I need to specify the package namespaces in Ansible 2.x?