[英]Ansible: How to change Python Version
Trying to use GNS3 to practice ansible script, there is a docker instance called "Network Automation" with built-in ansible.尝试使用 GNS3 练习 ansible 脚本,有一个名为“网络自动化”的内置 ansible 的 docker 实例。 However, it still uses Python 2.7 as the interpreter:
但是,它仍然使用 Python 2.7 作为解释器:
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]
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:我知道我可以使用“ansible-playbook --version -e 'ansible_python_interpreter=/usr/bin/python3'”命令来运行 Python 版本 3 的剧本,或者我可以在剧本中指定 var:
- name: Common package
hosts: all
gather_facts: no
vars:
ansible_python_interpreter: /usr/bin/python3
roles:
- { role: python, tags: [ init, python, common, addusers] }
...
...
However, I would like to have a permanent way to force ansible to use Python3 version.但是,我想有一种永久的方式来强制 ansible 使用 Python3 版本。 How can I achieve this?
我怎样才能做到这一点? Thanks.
谢谢。
Referring from the official ansible docs -从官方ansible文档中引用 -
Individual Linux distribution packages may be packaged for Python2 or Python3.
可以为 Python2 或 Python3 打包单独的 Linux 分发包。 When running from distro packages you'll only be able to use Ansible with the Python version for which it was installed.
当从发行版软件包运行时,您只能将 Ansible 与安装它的 Python 版本一起使用。 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).
有时发行版会提供一种安装多个 Python 版本的方法(通过单独的包或通过安装后运行的一些命令)。 You'll need to check with your distro to see if that applies in your case.
您需要检查您的发行版,看看这是否适用于您的情况。
The easiest way to run /usr/bin/ansible under Python 3 is to install it with the Python3 version of pip.在 Python 3 下运行 /usr/bin/ansible 的最简单方法是使用 Python3 版本的 pip 安装它。 This will make the default /usr/bin/ansible run with Python3:
这将使默认的 /usr/bin/ansible 与 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)]
If you are running Ansible Running From Source and want to use Python 3 with your source checkout, run your command via python3.如果您正在运行 Ansible Running From Source 并希望在源代码检出中使用 Python 3,请通过 python3 运行您的命令。 For example:
例如:
$ source ./hacking/env-setup
$ python3 $(which ansible) localhost -m ping
$ python3 $(which ansible-playbook) sample-playbook.yml
Why not use the var directory in your role...为什么不在您的角色中使用 var 目录...
├── defaults
│ └── main.yml
├── files
├── handlers
│ └── main.yml
├── meta
│ └── main.yml
├── README.md
├── tasks
│ └── main.yml
├── templates
├── tests
│ ├── inventory
│ └── test.yml
└── vars
└── main.yml
in vars/main.yml just add....在 vars/main.yml 中添加....
---
# vars file for XXXX
ansible_python_interpreter: /usr/bin/python3
Per https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html you could simply set it in the inventory for that host, or in your configuration file for ansible (which can also be shipped in the same directory as the playbooks and/or inventory):根据https://docs.ansible.com/ansible/latest/reference_appendices/interpreter_discovery.html,您可以简单地在该主机的清单中设置它,或者在您的 ansible 配置文件中设置它(也可以在与剧本和/或库存):
To control the discovery behavior:
要控制发现行为:
- for individual hosts and groups, use the ansible_python_interpreter inventory variable
对于单个主机和组,使用 ansible_python_interpreter 库存变量
- globally, use the interpreter_python key in the [defaults] section of ansible.cfg
全局使用 ansible.cfg 的 [defaults] 部分中的 interpreter_python 键
Adding some points that you might overlook based on comments above:根据以上评论添加一些您可能会忽略的要点:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.