Ansible:如何更改Python版本 [英] Ansible: How to change Python Version

查看:684
本文介绍了Ansible:如何更改Python版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试使用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 -

可以为Python2或Python3打包单独的Linux发行包.从发行版软件包运行时,您只能将Ansible与安装了该版本的Python版本一起使用.有时,发行版会提供几种Python版本的安装方式(通过单独的软件包或通过在安装后运行的某些命令).您需要检查发行版,以了解是否适用于您的情况.

Individual Linux distribution packages may be packaged for Python2 or Python3. When running from distro packages you’ll only be able to use Ansible with the Python version for which it was installed. Sometimes distros will provide a means of installing for several Python versions (via a separate package or via some commands that are run after install). You’ll need to check with your distro to see if that applies in your case.

在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版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆