【问题标题】:Ansible doesn't see an installed Python moduleAnsible 没有看到已安装的 Python 模块
【发布时间】:2019-01-29 00:15:19
【问题描述】:

我正在尝试创建一个 Ansible 剧本,它将设置一个 local Postgres 数据库以用于某个应用程序的本地/开发测试。

postgresql_db 似乎是我需要的 Ansible 模块,psycopg2 是列为依赖项的 Python 模块。

所以在我安装 Ansible 的同一个 virtualenv 中,我还安装了 psycopg2(我在 Mac 上运行 pipenv)。

但是当我用这个命令运行我的剧本时:

ansible-playbook pg_local.yml --connection=local

我得到错误:

"msg": "需要 python psycopg2 模块"

剧本很小:

---
- hosts: my_ws

  tasks:
    - name: create a db
      postgresql_db:
        name: mynewdb

/etc/ansible/hosts也是如此:

[my_ws]
localhost

我怀疑实际上是本地的“远程”机器以某种方式尝试import psycopg2,它在没有模块的 Python 环境中运行。 --connection=local 是罪魁祸首吗?

我已经添加它来解决ssh: connect to host localhost port 22: Connection refused 错误,因为我打算只在本地运行它。我不认为这是错误的 - 但我确实想知道它是否会破坏 Ansible 的环境。


我已在剧本中添加了“测试和报告”任务,但未检测到任何问题:

changed: [localhost] => {
    "changed": true,
    "cmd": [
        "python",
        "-c",
        "import psycopg2; print(psycopg2.__version__)"
    ],
    "delta": "0:00:00.087664",
    "end": "2018-08-22 13:36:17.046624",
    "invocation": {
        "module_args": {
            "_raw_params": "python -c 'import psycopg2; print(psycopg2.__version__)'",
            "_uses_shell": false,
            "argv": null,
            "chdir": null,
            "creates": null,
            "executable": null,
            "removes": null,
            "stdin": null,
            "warn": true
        }
    },
    "rc": 0,
    "start": "2018-08-22 13:36:16.958960",
    "stderr": "/Users/lsh783/.local/share/virtualenvs/docker-ansible-setup--HsJmUMv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.\n  \"\"\")",
    "stderr_lines": [
        "/Users/lsh783/.local/share/virtualenvs/docker-ansible-setup--HsJmUMv/lib/python3.6/site-packages/psycopg2/__init__.py:144: UserWarning: The psycopg2 wheel package will be renamed from release 2.8; in order to keep installing from binary please use \"pip install psycopg2-binary\" instead. For details see: <http://initd.org/psycopg/docs/install.html#binary-install-from-pypi>.",
        "  \"\"\")"
    ],
    "stdout": "2.7.5 (dt dec pq3 ext lo64)",
    "stdout_lines": [
        "2.7.5 (dt dec pq3 ext lo64)"
    ]
}

我确实在-vvv 的输出中看到了这一行:

<localhost> EXEC /bin/sh -c '/usr/bin/python /Users/lsh783/.ansible/tmp/ansible-tmp-1534957231.272713-121756549613883/postgresql_db.py > && sleep 0'

让我感到困扰的是,它不是我所在的虚拟环境中的 Python。

【问题讨论】:

  • 这个变通办法奏效了 - 将此添加到 ansible-playbook 调用中: --extra-vars="ansible_python_interpreter=$(which python)" ,但现在我真的很害怕使用它)
  • 库存显示在问题中; my_ws 是一个组,localhost 是它的成员;该剧本再次运行该组

标签: ansible


【解决方案1】:

这是由well-knowndescribed behaviour of Ansible 引起的。


简而言之,如果您在清单中的任何位置指定localhost,Ansible 将默认使用/usr/bin/python 来运行模块,而不管connection: local 设置如何。

如果在用于执行 playbook 的 Python 环境中安装了其他库,这反过来又会导致问题,但对于 /usr/bin/python 则不会。

解决方案是为localhost 指定ansible_python_interpreter。在你的情况下:

[my_ws]
localhost ansible_python_interpreter={{ansible_playbook_python}}

由于上述原因,验证模块存在的测试应该是:

- command: "{{ ansible_python_interpreter | default('/usr/bin/python') }} -c 'import {{ module }}; print({{ module }}.__version__)'"
  vars:
    module: psycopg2
  register: test
- debug:
    var: test.stdout

【讨论】:

    【解决方案2】:

    当同时使用 virtualenv 和 local_action 设置时,我无法使用 ansible_playbook_python 的值使 ansible_python_interpreter 工作。

    但是,使用 /usr/bin/env 的“非 Ansible 方式”对我有用。

    ansible_python_interpreter: "/usr/bin/env python"
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-12-19
      • 2017-09-04
      • 1970-01-01
      • 1970-01-01
      • 2019-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多