问题描述
尝试使用GNS3练习ansible脚本,有一个带有内置ansible的名为"Network Automation"的docker实例.但是,它仍然使用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 -
在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从源运行,并且想在源签出中使用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版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!