簡體   English   中英

在 ansible 中使用自定義 python 模塊 - 如何導入安裝在 Z594C103F2C6E04C3D8AB059F031E0C 上的 python 模塊

[英]With custom python module in ansible - How to import python modules installed on controller

我花了一整天的時間試圖弄清楚這一點。 但沒有成功:-(

我有一個自定義 python 模塊,我正在與 ansible 一起使用。 該模塊在遠程節點上運行,我可以毫無問題地導入自己的自定義模塊。 一切都很好,直到我想導入我安裝在 controller 上的開源 python 模塊(單調)。 我收到以下錯誤消息:

ModuleNotFoundError:沒有名為“單調”的模塊

只有 1 個 python 解釋器(python3 在使用基本 Ubuntu 映像構建的容器中)。 果然,直接在 python 命令行導入就完美了。

我了解 ansible 收集 controller 上的所有 python 代碼並將其傳輸到遠程節點。 但不知何故,單調(或任何其他已安裝的開源軟件包)不會發生這種情況。 事實上,這甚至可以使用已安裝的軟件包,但我不知何故失去了這種能力。 可能是我很幸運,但是在 ansible 更新后運氣消失了......我認為它與 ansible 和安裝的 python 之間的一些搜索路徑有關。 For what its worth, Ubuntu's pip version apparently installs packages under /usr/local/lib/python3.8/dist-packages/, where I can find the monotonic package (and jinja2, which I presumed was installed as a dependency to ansible. ...)

所以,我的問題很簡單,設置什么是正確的環境變量來訪問 python 的系統安裝包?

Ansible 不會將任何 Python 模塊傳輸到遠程節點。 如果您的自定義模塊具有依賴項,您將需要安排在您的劇本中安裝它們。

例如:

- hosts: all
  tasks:
    - name: Ensure that monotonic is installed on remote host
      pip:
        name: monotonic
        state: present

    - name: Use my custom module
      mycustommodule:

(盡管根據monotonic的文檔,如果您使用的是 Python 3.3 或更高版本,則不需要它;您可以使用time.monotonic代替)。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM