Ansible:剧本不是 运行 python3

Ansible: playbook not running with python3

我已经按照官方 ansible 文档克隆了 ansible 存储库和设置。

我通过为 python3 创建 venv 直接从源代码进行设置。

运行 这个:. venv/bin/activate && . hacking/env-setup 确实设置了 ansible 环境。如果我写这个(当 venv 被激活时):

ansible-playbook --version

它输出这个:

ansible-playbook 2.8.5.post0
  config file = /etc/ansible/ansible.cfg
  configured module search path = ['/home/oerp/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /home/oerp/src/ansible/lib/ansible
  executable location = /home/oerp/src/ansible/bin/ansible-playbook
  python version = 3.6.7 (default, Mar 29 2019, 10:38:28) [GCC 5.4.0 20160609]

所以它确实表明它使用了 Python 版本 3.6.7。如果我 运行 对具有 python 3.6 语法的自定义模块进行单元测试,测试 运行 很好(测试是 运行 和 tox 记录由 ansible 本身)。

但是它无法通过 playbook 运行 我的模块。看起来它正在使用 python2.

执行它

当我运行这个命令时:

ansible-playbook --extra-vars "target=local_stage" --connection=local /home/oerp/src/ansible-playbooks/buildout_test.yml -vvv

它抛出这个回溯:

The full traceback is:
Traceback (most recent call last):
  File "/home/oerp/.ansible/tmp/ansible-tmp-1570781848.1110492-2720137190388/AnsiballZ_focusate_buildout.py", line 114, in <module>
    _ansiballz_main()
  File "/home/oerp/.ansible/tmp/ansible-tmp-1570781848.1110492-2720137190388/AnsiballZ_focusate_buildout.py", line 106, in _ansiballz_main
    invoke_module(zipped_mod, temp_path, ANSIBALLZ_PARAMS)
  File "/home/oerp/.ansible/tmp/ansible-tmp-1570781848.1110492-2720137190388/AnsiballZ_focusate_buildout.py", line 49, in invoke_module
    imp.load_module('__main__', mod, module, MOD_DESC)
  File "/tmp/ansible_focusate_buildout_payload_JDfOK3/__main__.py", line 52
    path: str,
        ^
SyntaxError: invalid syntax

显然它不识别输入语法。查看调试,我可以看到正在执行这样的二进制文件:

<stage-my-domain.com> EXEC /bin/sh -c '/usr/bin/python /home/oerp/.ansible/tmp/ansible-tmp-1570781848.1110492-2720137190388/AnsiballZ_my_buildout.py && sleep 0'

在我的环境 /usr/bin/python --version 中,是 Python 2.7.12

调试也显示此信息:

<stage-my-domain.com> EXEC /bin/sh -c 'echo PLATFORM; uname; echo FOUND; command -v '"'"'/usr/bin/python'"'"'; command -v '"'"'python3.7'"'"'; command -v '"'"'python3.6'"'"'; command -v '"'"'python3.5'"'"'; command -v '"'"'python2.7'"'"'; command -v '"'"'python2.6'"'"'; command -v '"'"'/usr/libexec/platform-python'"'"'; command -v '"'"'/usr/bin/python3'"'"'; command -v '"'"'python'"'"'; echo ENDFOUND && sleep 0'

有谁知道为什么 ansible 忽略激活的 venv 而只使用全局 python 即 python2?

P.S。我还通过 apt-get install ansible.

在全球范围内安装了 ansible

venv 使用起来有点棘手。它与您的会话相关。 Ansible 在执行时使用的会话与您作为登录用户使用的会话不同。您需要为 Ansible 在执行时使用的特定会话激活 venv。

您可以尝试两件事:

1:设置环境变量:

environment:
  PATH: "{{ ansible_env.PATH }}:/yourpython/bin"
  SOME: value

2:设置 Ansible python 解释器

https://docs.ansible.com/ansible/latest/reference_appendices/faq.html#how-do-i-handle-python-not-having-a-python-interpreter-at-usr-bin-python-on-a-remote-machine

Setting the inventory variable ansible_python_interpreter on any host will tell Ansible to auto-replace the Python interpreter with that value instead. Thus, you can point to any Python you want on the system if /usr/bin/python on your system does not point to a compatible Python interpreter.

Some platforms may only have Python 3 installed by default. If it is not installed as /usr/bin/python, you will need to configure the path to the interpreter via ansible_python_interpreter. Although most core modules will work with Python 3, there may be some special purpose ones which do not or you may encounter a bug in an edge case. As a temporary workaround you can install Python 2 on the managed host and configure Ansible to use that Python via ansible_python_interpreter.