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

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

问题描述

我正在尝试创建一个Ansible剧本,该剧本将设置一个 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(我在使用pipenv的Mac上运行).

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

我怀疑远程"计算机(实际上是本地计算机)试图导入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 refused错误,并且因为我确实打算仅在本地运行它.我认为这没错-但我确实想知道这是否会破坏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,则无论connection: local设置如何,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天全站免登陆