21

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}
david
  • 6,303
  • 16
  • 54
  • 91
  • Can I do something similar to from ... import usefulpythonfunctions ? – david Feb 11 '16 at 00:25
  • Is usefulpythonfunctions installed on your target host(s)? Keep in mind that Ansible copies a module to the target and then invokes the module. So this error is likely originating from your target host and not the host you're running Ansible on. – Bruce P Feb 11 '16 at 21:37

4 Answers4

19

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

taras
  • 3,579
  • 3
  • 26
  • 27
  • 1
    to expand a little, create a folder called `module_utils` in the same location as your playbook file, place your module(s) there, then import as described by @Taras – perlyking Jun 01 '17 at 17:07
  • @perlyking, thank you for the clarification! I have updated the answer. – taras Jun 01 '17 at 20:47
  • 2
    Thanks Taras. I've added this to the Ansible documentation at https://docs.ansible.com/ansible/devel/dev_guide/developing_module_utilities.html – ktdreyer Feb 01 '19 at 21:18
  • 2
    How to make `PyCharm` understand the directory structure? without errors and with autocomplete. – Time Killer Mar 13 '21 at 03:48
3

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...

Until then...

Deploy your dependencies using ansible

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

Download dependencies in your module code

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.

Roll your own package

You could also roll your own package using pip or any other packaging toolchain. This is probably not for the faint of heart.

BobTuckerman
  • 689
  • 9
  • 13
1

According to my understanding is this not possible in Ansible, but there is pending pull request that offers some hope.

slackhacker
  • 523
  • 4
  • 8
0

Try creating a folder called usefulpythonfunctions, create an __init__.py and place the functions you want in there.

Additionally, you should be able to create bin/foo/bar.py and use from foo import bar to call functions or classes out of bar.

djsumdog
  • 2,560
  • 1
  • 29
  • 55
  • 2
    Why would that work? I don't understand how Ansible would recognize an added directory inside the bin directory when it doesn't seem to recognize the bin folder in the first place. – david Feb 10 '16 at 23:52