Ansible没有看到已安装的Python模块

ale*_*pov 5 ansible

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

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

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

但是当我使用以下命令运行剧本时:

ansible-playbook pg_local.yml --connection=local
Run Code Online (Sandbox Code Playgroud)

我得到错误:

“ msg”:“需要python psycopg2模块”

该剧本很小:

---
- hosts: my_ws

  tasks:
    - name: create a db
      postgresql_db:
        name: mynewdb
Run Code Online (Sandbox Code Playgroud)

所以是/etc/ansible/hosts

[my_ws]
localhost
Run Code Online (Sandbox Code Playgroud)

我怀疑“远程”计算机(实际上是本地计算机)试图导入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)"
    ]
}
Run Code Online (Sandbox Code Playgroud)

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

<localhost> EXEC /bin/sh -c '/usr/bin/python /Users/lsh783/.ansible/tmp/ansible-tmp-1534957231.272713-121756549613883/postgresql_db.py > && sleep 0'
Run Code Online (Sandbox Code Playgroud)

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

tec*_*raf 5

这是由一个导致公知的Ansible的描述的行为


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

如果在用于执行剧本的Python环境中安装了其他库,而不是在库中安装了其他库,则这又会导致问题/usr/bin/python

该解决方案是指定ansible_python_interpreterlocalhost。在您的情况下:

[my_ws]
localhost ansible_python_interpreter={{ansible_playbook_python}}
Run Code Online (Sandbox Code Playgroud)

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

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