我试图从不同目录中的另一个脚本调用 python 函数。有一个剧本来执行此操作。
这在本地主机上工作正常,但在远程服务器上失败,并显示“ModuleNotFoundError:没有名为“script2”的模块”
这是我的脚本:
[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
青春有我
相关分类