【问题标题】:Ansible script: python: ModuleNotFoundErrorAnsible 脚本:python:ModuleNotFoundError
【发布时间】:2020-02-11 23:33:10
【问题描述】:

我正在尝试使用 ansible 运行我的 python 主文件。

name: Run project become: yes script: "{{repo}}/main.py --arg1 {{repo}}/configfile.py" args: chdir: "{{repo}}" executable: "{{repo}}/venv/bin/python"

这个文件有多个从我的项目导入。我能够获取虚拟环境并使用 ansible 运行主脚本。但是,我收到此错误

但是,我收到此错误:

fatal: [local_machine]: FAILED! => {"changed": true, "msg": "non-zero return code", "rc": 1, "stderr": "Traceback (most recent call last):\n  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1571132915.6405714-115359809713305/main.py\", line 1, in <module>\n    import utils\nModuleNotFoundError: No module named 'utils'\n", "stderr_lines": ["Traceback (most recent call last):", "  File \"/home/vagrant/.ansible/tmp/ansible-tmp-1571132915.6405714-115359809713305/watch_scraper.py\", line 1, in <module>", "    import utils", "ModuleNotFoundError: No module named 'utils'"], "stdout": "", "stdout_lines": []}

我如何让 ansible 运行脚本并让它知道它的上下文?

【问题讨论】:

    标签: python ansible


    【解决方案1】:

    您尝试执行的脚本可能需要安装 utils 才能执行。

    由于没有对 localhost 的委托或没有使用 local_action。执行发生在目标节点上。

    您需要安装 utils 包。

    在 python 中,pip(包管理器)与特定版本的 python 相关联,即 pip3 用于 python3,pip2 用于 python2。

    由于您使用的是 venv,因此您需要在执行脚本之前安装软件包。

    我建议传递 requirements.txt 并将其用作安装模块的参考。

    【讨论】:

    猜你喜欢
    • 2022-11-18
    • 1970-01-01
    • 2020-01-24
    • 2016-03-29
    • 1970-01-01
    • 1970-01-01
    • 2021-12-12
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多