【发布时间】: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