使用 Ansible 的 Flask 迁移命令抛出超出最大递归深度错误

Flask migrate command using Ansible throws a maximum recursion depth exceeded error

我的 Flask 应用程序使用 alembic 来管理数据库迁移。在尝试使用 Ansible 部署应用程序时,当 Ansible 剧本遇到命令 python manage.py db upgrade 时,它 运行 陷入 超过最大递归深度的运行时错误 。错误信息:

fatal: [host.ip]: FAILED! => {"changed": true, "cmd": "/home/deploy/venvs/app/bin/python 
/var/www/app/manage.py db upgrade", "delta": "0:00:02.854134",
"end": "2016-04-11 15:59:32.656916", "failed": true, "rc": 1,
"start": "2016-04-11 15:59:29.802782",
"stderr": "INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
INFO  [alembic.runtime.migration] Will assume transactional DDL.
INFO  [alembic.runtime.migration] Context impl PostgresqlImpl.
...
...
File \"/home/deploy/venvs/app/local/lib/python2.7/site-packages/sqlalchemy/
sql/elements.py\", line 3292, in _get_table
return self.__dict__['table']
RuntimeError: maximum recursion depth exceeded in cmp", 
"stdout": "", "stdout_lines": [], "warnings": []}

当我在本地 运行 命令时,我没有收到任何错误,当我实际通过 SSH 连接到远程机器和 运行 相同的命令时,我也没有收到任何错误。我的应用程序结构是:

my_app
|--my_app
|--migrations
   |--versions
   |--alembic.ini
   |--env.py
   |--README
   |--script.py.mako
|--playbooks
|--tests
|--manage.py
|--settings.py
|--wsgi.py

示例 Ansible 剧本 deploy.yml 如下:

- name: install dependencies into a new virtualenv
    pip: requirements={{ app_dir }}/requirements.txt
    virtualenv={{ venv_dir }}
- name: create .env file in base directory of the app from template
    template: src=env.j2
              dest={{ app_dir }}/.env
- name: sync the database tables between Flask and PostgreSQL
    shell: ". {{ app_dir }}/.env; {{ venv_python }} {{ app_dir }}/manage.py db upgrade"

变量赋值的vars文件如下:

wsgi_env_vars: {
                APP_ENV: 'prod',
               }

对应的env.j2模板文件为:

#!/bin/bash
{% for k, v in wsgi_env_vars.iteritems() %}
export {{ k }}="{{ v }}"
{% endfor %}

尝试了不同的方法来解决错误,包括在 ansible 中将默认 shell 从 sh 更改为 bash,但没有任何帮助。谁能帮忙找出错误?

Github 找到了答案。只需将 ansible 命令更改为 cd 进入目录,然后 运行 迁移命令。