My Ansible directory structure looks something like this.
Ansible-Repo
|
+-- playbooks
| |
| +-- run_custom_module1
|
+-- library
| |
| +-- custom_module1
| +-- custom_module2
|
+-- bin
| |
| +-- usefulpythonfunctions.py
I want to be able to import usefulpythonfunctions.py from the bin inside my Ansible Module. I have an import usefulpythonfunctions.py at the top of my module, but I receive the following error when I run the playbook.
\r\nImportError: No module named usefulpythonfunctions\r\n", "msg": "MODULE FAILURE", "parsed": false}
You can use the Ansible Python API to control nodes, you can extend Ansible to respond to various Python events, you can write plugins, and you can plug in inventory data from external data sources. This document gives a basic overview and examples of the Ansible execution and playbook API.
While you can write Ansible modules in any language, most Ansible modules are written in Python, including the ones central to letting Ansible work. By default, Ansible assumes it can find a /usr/bin/python on your remote system that is either Python2, version 2.6 or higher or Python3, 3.5 or higher.
Create your new module file: $ touch library/my_test.py . Or just open/create it with your editor of choice. Paste the content below into your new module file. It includes the required Ansible format and documentation, a simple argument spec for declaring the module options, and some example code.
Luckily 2.3 release has introduced this feature.
Now you can place your shared code in module_utils
folder where your playbook lives and it will be automatically exported by ansible. Also you might be interested in checking the directory structure documentation
You can access your module in module_utils
as you would access a standard util
from ansible.module_utils.my_shared_code import MySharedCodeClient
UPDATE
Now you can configure a path where your module utils live using ANSIBLE_MODULE_UTILS
env variable. For more details please check documentation
I've been looking into ways to do this and it doesn't appear this is possible in the current version of Ansible.
Honestly, the pull request mentioned by @slackhacker seems to be the cleanest option, but that looks like it may not be available until Ansible 2.1...
The library you want has to be copied to each system you run on. So, let's just have ansible take care of it for us.
---
tasks:
- name: Copy mylibrary
# Only requirement is that the dst path exists in the default system python path
copy: src=path-to-library dst=/usr/local/lib/python2.7/dist-packages
- name:
mymodule:
arg1: foo
arg2: bar
If your module code is accessible to the server you are running on (via HTTP, SSH, etc), you could code the module to go out and grab the dependencies directy in python instead of doing it as a separate task.
This has the added value of not requiring extra steps from the end user, but will probably run slower due to the extra copies having to be made.
You could also roll your own package using pip
or any other packaging toolchain. This is probably not for the faint of heart.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With