问题描述
尝试使用GNS3练习ansible脚本,有一个docker实例叫做网络自动化",内置ansible.但是,它仍然使用 Python 2.7 作为解释器:
Trying to use GNS3 to practice ansible script, there is a docker instance called "Network Automation" with built-in ansible. However, it still uses Python 2.7 as the interpreter:
root@Network-Automation:~# ansible --version
ansible 2.7.11
config file = /etc/ansible/ansible.cfg
configured module search path = [u'/root/.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
python version = 2.7.12 (default, Nov 12 2018, 14:36:49) [GCC 5.4.0 20160609]
我知道我可以使用ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'"命令来运行 Python 版本 3 的剧本,或者我可以在剧本中指定 var:
I understand I can use "ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'" command to run a playbook with Python version 3, or I can specifiy var within the playbook:
- name: Common package
hosts: all
gather_facts: no
vars:
ansible_python_interpreter: /usr/bin/python3
roles:
- { role: python, tags: [ init, python, common, addusers] }
...
...
但是,我想有一个永久的方法来强制 ansible 使用 Python3 版本.我怎样才能做到这一点?谢谢.
However, I would like to have a permanent way to force ansible to use Python3 version. How can I achieve this? Thanks.
推荐答案
参考 ansible 官方文档 -
Referring from the official ansible docs -
单独的 Linux 分发包可能被打包用于 Python2 或 Python3.当从发行版包运行时,您只能将 Ansible 与安装它的 Python 版本一起使用.有时发行版会提供一种安装多个 Python 版本的方法(通过单独的包或通过安装后运行的一些命令).您需要咨询您的发行版,看看这是否适用于您的情况.
在 Python 3 下运行/usr/bin/ansible 的最简单方法是使用 Python3 版本的 pip 安装它.这将使默认的/usr/bin/ansible 与 Python3 一起运行:
The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3 version of pip. This will make the default /usr/bin/ansible run with Python3:
$ pip3 install ansible
$ ansible --version | grep "python version"
python version = 3.6.2 (default, Sep 22 2017, 08:28:09) [GCC 7.2.1 20170915 (Red Hat 7.2.1-2)]
如果您正在运行 Ansible Running From Source 并希望在源代码检出中使用 Python 3,请通过 python3 运行您的命令.例如:
If you are running Ansible Running From Source and want to use Python 3 with your source checkout, run your command via python3. For example:
$ source ./hacking/env-setup
$ python3 $(which ansible) localhost -m ping
$ python3 $(which ansible-playbook) sample-playbook.yml
这篇关于Ansible:如何更改 Python 版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!