Ansible not finding custom python modules on remote server

Viewed 201

I am trying to call a python function from another script in a different directory. Have a playbook to execute this.

This works fine on localhost but on remote_server it is failing with "ModuleNotFoundError: No module named 'script2'"

Here are my scripts:

[root@server Test]# ls
hosts  playbook  python1  python2
[root@server Test]# cat playbook/playbook.yml 

- hosts: "{{ host }}"
  gather_facts: yes
  become: yes

  vars:
    ansible_python_interpreter: /usr/bin/python3

  tasks:

    - name: Connect to MongoDB
      script: ../python1/script1.py
      args:
        executable: python3
[root@server Test]# cat python1/script1.py 
#!/usr/bin/python

import os
import sys

sys.path.append("../python2")

from script2 import dbServer


def main():

    cursor = dbServer()
    print(cursor.count())


if __name__ == '__main__':
    main()
[root@server Test]# cat python2/script2.py 

#! /usr/bin/python

from pymongo import MongoClient

def connectToMongoDB():

    global db

    try:
        conn = MongoClient("myserver.com")
        db = conn.CMDB
    except Exception as e:
        print("\nUnable to fetch details from MongoDB..!!!\n%s\n" % e)
        sys.exit()


def dbServer():

    connectToMongoDB()

    collection = db.dbServer
    cursor = collection.find()

    return cursor

Execution:

[root@server playbook]# ansible-playbook -i ../hosts playbook.yml -e "host=localhost"

PLAY [localhost] ****************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [localhost]

TASK [Connect to MongoDB] *******************************************************************************************
changed: [localhost]

PLAY RECAP **********************************************************************************************************
localhost                  : ok=2    changed=1    unreachable=0    failed=0    skipped=0    rescued=0    ignored=0   
[root@server playbook]# ansible-playbook -i ../hosts playbook.yml -e "host=myremote"

PLAY [remote_server] ************************************************************************************************

TASK [Gathering Facts] **********************************************************************************************
ok: [myremote]

TASK [check ping status of P10 systems] *****************************************************************************
fatal: [myremote]: FAILED! => {"changed": true, "msg": "non-zero return
code", "rc": 1, "stderr": "Shared connection to myremote closed.\r\n",
"stderr_lines": ["Shared connection to myremote closed."], "stdout":
"Traceback (most recent call last):\r\n  File
\"/root/.ansible/tmp/ansible-tmp-1602453181.740341-
25196530470896/script1.py\", line 8, in <module>\r\n    from script2
import dbServer\r\nModuleNotFoundError: No module named 'script2'\r\n",
"stdout_lines": ["Traceback (most recent call last):", "  File
\"/root/.ansible/tmp/ansible-tmp-1602453181.740341-
25196530470896/script1.py\", line 8, in <module>", "    from script2
import dbServer", "ModuleNotFoundError: No module named 'script2'"]}

PLAY RECAP **********************************************************************************************************
myremote : ok=1    changed=0    unreachable=0    failed=1    skipped=0    rescued=0    ignored=0

Please help how can I get my module copied to remote server. I need to get this working in AWX as well.

1 Answers

As explained in module script document : The local script at path will be transferred to the remote node and then executed.

Any imported files in script will not, you must copy them before on remote with module copy.

Example (adapt access modes and path if needed) :

- hosts: "{{ host }}"
  gather_facts: yes
  become: yes

  vars:
    ansible_python_interpreter: /usr/bin/python3

  tasks:
    - name: Create directory
      copy:
        path: /tmp/python1
        state: directory
        mode: 0755
    - name: Copy scripts
      copy:
        src: ../python1
        dest: /tmp/python1

    - name: Connect to MongoDB
      script: ../python1/script1.py
      args:
        chdir: /tmp/python1
        executable: python3

However, it is usually preferable to write Ansible modules rather than pushing scripts.

Your script works locally because all needed imported files already exists

Related