Ansible python 版本不会改变

Zum*_*rio 3 python ansible

我正在执行:

ansible-playbook --version

我得到以下输出:

ansible-playbook 2.5.14
  config file = /etc/ansible/ansible.cfg
  configured module search path = [u'~/.ansible/plugins/modules', u'/usr/share/ansible/plugins/modules']
  ansible python module location = /usr/lib/python2.7/dist-packages/ansible
  executable location = /usr/bin/ansible-playbook
  python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
Run Code Online (Sandbox Code Playgroud)

但是当我执行时:

ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'

我有完全相同的输出。我期待有类似的东西:

python version = 3.5.2
Run Code Online (Sandbox Code Playgroud)

我有什么误解?

Mat*_*t P 6

要更改托管主机上使用的 python 解释器,您可以根据您的问题使用额外或库存变量。这不会更改用于在本地执行 ansible 的 python 版本 - 这在另一个答案中提到。

要确认您在托管主机上使用不同的解释器,您将需要更详细的输出(至少 3 个 'v's, -vvv)。python --version在 shell 命令中运行只会向您展示系统的默认 python 版本是什么。

例子

Python2 解释器:

$ ansible localhost -a 'python --version' -e 'ansible_python_interpreter=/usr/bin/python2' -vvv
ansible 2.8.1
<snip>
  python version = 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)]
<snip>
'/bin/sh -c '"'"'/usr/bin/python2 /home/mattp/.ansible/tmp/ansible-tmp-1564023345.0848873-106174541151316/AnsiballZ_command.py && sleep 0'"'"''
<snip>
localhost | CHANGED | rc=0 >>
Python 2.7.16
Run Code Online (Sandbox Code Playgroud)

Python3 解释器:

$ ansible localhost -a 'python --version' -e 'ansible_python_interpreter=/usr/bin/python3' -vvv
ansible 2.8.1
<snip>
  python version = 3.7.3 (default, May 11 2019, 00:38:04) [GCC 9.1.1 20190503 (Red Hat 9.1.1-1)]
<snip>
'/bin/sh -c '"'"'/usr/bin/python3 /home/mattp/.ansible/tmp/ansible-tmp-1564023350.3869421-223113472194736/AnsiballZ_command.py && sleep 0'"'"''
<snip>
localhost | CHANGED | rc=0 >>
Python 2.7.16
Run Code Online (Sandbox Code Playgroud)

关键要点

  • 用于在本地运行 Ansible 的 python 版本保持不变
  • 系统默认python版本不变
  • 用于远程执行 Ansible 命令模块的 python 版本(即 python 解释器)已从版本 2 更改为 3