Ansible 没有看到已安装的 Python 模块 [英] Ansible doesn't see an installed Python module

查看:29
本文介绍了Ansible 没有看到已安装的 Python 模块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create an Ansible playbook which would setup a local Postgres database to be used for local/dev testing of a certain app.

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

postgresql_db seems to be the Ansible module I need, and psycopg2 is the Python module listed as a dependency.

因此,在安装 Ansible 的同一个 virtualenv 中,我还安装了 psycopg2(我在 Mac 上使用 pipenv 运行).

So in the same virtualenv where I have installed Ansible, I also installed psycopg2 (I'm running on Mac with pipenv).

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

But when I run my playbook with this command:

ansible-playbook pg_local.yml --connection=local

我收到错误:

msg":需要python psycopg2模块"

"msg": "the python psycopg2 module is required"

剧本很小:

---
- hosts: my_ws

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

/etc/ansible/hosts 也是如此:

[my_ws]
localhost

我怀疑不知何故远程"真正本地的机器正在尝试 import psycopg2,它在没有模块的 Python 环境中运行.--connection=local 是罪魁祸首吗?

I suspect that somehow the "remote" machine, which is really local, is trying to import psycopg2, which running in a Python environment which doesn't have the module. Is the --connection=local to blame?

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

I have added it to solve the ssh: connect to host localhost port 22: Connection refused error, and as I do intend to run this only locally. I don't think it's wrong - but I do wonder if it messes up the environment for Ansible.

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

I have added a 'test and report' task to the playbook and no problems were detected:

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 的输出中看到了这一行:


I do see this line in the output with -vvv:

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

让我烦恼的是,它不是我所处的虚拟环境中的 Python.

and it bothers me that it's not the Python inside the virtual environment I'm under.

推荐答案

这是由 众所周知描述 Ansible 的行为.

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

In short, if you specify localhost wherever in your inventory, Ansible will default to using /usr/bin/python for running the modules regardless of the connection: local setting.

如果在用于执行剧本的 Python 环境中安装了额外的库,而不是为 /usr/bin/python 安装了额外的库,这反过来会导致问题.

This in turn will cause problems if additional libraries were installed in a Python environment used to execute a playbook, but not for the /usr/bin/python.

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

The solution is to specify ansible_python_interpreter for the localhost. In your case:

[my_ws]
localhost ansible_python_interpreter={{ansible_playbook_python}}

<小时>

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


Because of the above, the test to verify module presence should be:

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

这篇关于Ansible 没有看到已安装的 Python 模块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆