Ansible在远程服务器上找不到自定义python模块

Ansible在远程服务器上找不到自定义python模块,python,ansible,Python,Ansible,我试图从不同目录中的另一个脚本调用python函数。有一个剧本来执行这个 这在本地主机上运行正常,但在远程服务器上,它因“ModuleNotFoundError:没有名为“script2”的模块”而失败 以下是我的脚本: [root@server测试]#cat python1/script1.py #!/usr/bin/python 导入操作系统 导入系统 sys.path.append(“../python2”) 从script2导入数据库服务器 def main(): cursor=dbSe

我试图从不同目录中的另一个脚本调用python函数。有一个剧本来执行这个

这在本地主机上运行正常,但在远程服务器上,它因“ModuleNotFoundError:没有名为“script2”的模块”而失败

以下是我的脚本:
[root@server测试]#cat python1/script1.py
#!/usr/bin/python
导入操作系统
导入系统
sys.path.append(“../python2”)
从script2导入数据库服务器
def main():
cursor=dbServer()
打印(cursor.count())
如果uuuu name uuuuuu='\uuuuuuu main\uuuuuuu':
main()
[root@server测试]#cat python2/script2.py
#! /usr/bin/python
从pymongo导入MongoClient
def connectToMongoDB():
全局数据库
尝试:
conn=MongoClient(“myserver.com”)
db=conn.CMDB
例外情况除外,如e:
打印(“\n无法从MongoDB获取详细信息..!!!\n%s\n”%e)
sys.exit()
def dbServer():
connectToMongoDB()
collection=db.dbServer
cursor=collection.find()
返回光标
执行:
[root@serverplaybook]#ansible playbook-i../hosts playbook.yml-e“host=myremote”
播放[远程服务器]************************************************************************************************
任务[收集事实]**********************************************************************************************
ok:[myremote]
任务[检查P10系统的ping状态]*****************************************************************************
致命:[myremote]:失败!=>{“changed”:true,“msg”:“非零返回”
代码,“rc”:1,“stderr”:“与myremote的共享连接已关闭。\r\n”,
“stderr_行”:[“与myremote的共享连接已关闭],“stdout”:
“回溯(最近一次调用):\r\n文件
\“/root/.ansible/tmp/ansible-tmp-1602453181.740341-
25196530470896/script1.py\”,第8行,在脚本2中\r\n
导入dbServer\r\nModule NotFoundError:没有名为“script2”的模块\r\n,
“标准输出行”:[“回溯(最近一次调用):”,“文件”
\“/root/.ansible/tmp/ansible-tmp-1602453181.740341-
25196530470896/script1.py\”,第8行,在“来自脚本2”中
导入dbServer”,“ModuleNotFoundError:没有名为“script2”的模块]}
重演**********************************************************************************************************
myremote:确定=1更改=0无法访问=0失败=1跳过=0获救=0忽略=0

请帮助我如何将模块复制到远程服务器。我还需要在AWX中实现这一点。

如模块
脚本
文档中所述:path中的本地脚本将传输到远程节点,然后执行

脚本中的任何导入文件都不会被复制,您必须先使用模块
copy
在远程上复制它们

示例(根据需要调整访问模式和路径):

-hosts:“{{host}”
收集事实:是的
变成:是的
变量:
ansible_python_解释器:/usr/bin/python3
任务:
-名称:创建目录
副本:
路径:/tmp/python1
状态:目录
模式:0755
-名称:复制脚本
副本:
src:../python1
目的地:/tmp/python1
-名称:连接到MongoDB
脚本:../python1/script1.py
args:
chdir:/tmp/python1
可执行文件:python3
但是,通常最好编写Ansible模块,而不是推送脚本


您的脚本在本地运行,因为所有需要导入的文件都已存在

非常感谢!这解决了我的问题。你所说的“通常编写Ansible模块比推送脚本更可取”是什么意思?在我的情况下,我该怎么做?
[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 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