0

In Ansible, I can run a python script if it contains code in the same script. However, if i try to use

name: Restarting service on different nodes
  hosts: nodes
  connection: ssh
  tasks:
    - name: Restarting tomcat service
      script: main.py 1
      args:
        executable: python3

And main.py has import restart_tomcat (restart_tomcat.py is present in the same folder as main.py) it is not able to import this module , though present in the same directory.

How to make it understand that the other supporting files for main.py is present in same directory. Note : it is failing, when its trying to execute it on remote servers

Edit : It would get too complicated to create custom_module on Ansible for every example we want to run

Viv
  • 1,474
  • 5
  • 28
  • 47
  • 1
    If you really do not wish to use shared code and not wish to convert your scripts to modules, then check this https://stackoverflow.com/questions/35328177/how-to-import-a-py-file-into-an-ansible-module , it may help(not sure) – P.... Nov 30 '22 at 16:25
  • 2
    1) scripts are copied individually to the target then run then removed. If you need other dependent files you'll have to copy them first 2) using the `script` module to restart a service is a very bad practice. There are modules for that like `service`, `systemd`... 3) In fact `script` and its friend `raw` should never be used in ansible tasks except mainly for installing dependencies for ansible itself (e.g. install python). If you are planning to develop playbooks mainly using `script`, `shell`, `command` and running existing python/bash scripts as they are, ansible is not the good tool. – Zeitounator Nov 30 '22 at 19:56

1 Answers1

4

I think you should write a custom module; note that when you run any script, ansible creates a copy of it to a temp location. So any relative path you provided in the import will be messed up. You can run your playbook task with -vvv to confirm this.

Here is an example(high level) of setting up a custom module:

 tree 
.
├── your_playbook.yml
├── library
│   └── your_custom_module.py # write your code logic here         
└── module_utils
    └── restart_tomcat.py     #this file contains common classes/functions

In your module file(your_custom_module.py), you can do the import like this:

#!/usr/bin/python3
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
from ansible.module_utils.basic import AnsibleModule 
from ansible.module_utils.restart_tomcat.py import *  #this line will import the classes or functions present in the other file to the custom module

You can find more details here and an example here and this.

For supporting references, run the ansible-doc script command and navigate to the NOTES section.

P....
  • 17,421
  • 2
  • 32
  • 52
  • Thanks for Answering, but i would not be able to convert all modules to custom ansible module. Is there a way to make ansible understand that the other files are present in the same folder? or will copying the whole set of files on remote server and starting the python script from the relative path work? – Viv Nov 30 '22 at 16:03
  • As far as I know, you will have to use `copy + command` combination to run your scripts. Run `ansible-doc script` command on your ansible controller, and navigate to the `NOTES:` section. – P.... Nov 30 '22 at 16:19