Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to import a .py file into an Ansible Module?

Tags:

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}
like image 850
david Avatar asked Feb 10 '16 23:02

david


People also ask

Can I use python in Ansible?

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.

What is Ansible python module location?

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.

How do I make an Ansible module in python?

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.


2 Answers

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

like image 200
taras Avatar answered Sep 22 '22 16:09

taras


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.

like image 34
BobTuckerman Avatar answered Sep 20 '22 16:09

BobTuckerman