laj*_*ode 5 ubuntu python-3.x ansible
我想将 ansible 更改为在控制器上使用 python3.5。我已经安装了ansible和python3.5,有没有办法将其更改为使用python3?
ansible 文档建议使用 ansible 和python3 /usr/bin/ansible localhost -m ping. 但是,如果这不起作用,请不要提供更多细节。
我的结果是:
Traceback (most recent call last):
File "/usr/bin/ansible", line 32, in <module>
from ansible import context
ImportError: No module named 'ansible'
Run Code Online (Sandbox Code Playgroud)
我也尝试过 pip3 install ansible 没有运气:
File "<string>", line 1, in <module>
File "/tmp/pip-build-eadti4n6/ansible/setup.py", line 12
print "Ansible now needs setuptools in order to build. " \
^
SyntaxError: Missing parentheses in call to 'print'
Run Code Online (Sandbox Code Playgroud)
按照 Ansible Python 3 支持页面上的说明,我pip3在删除以前的 (2.7) 版本后安装了 Ansible :
$ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible
Run Code Online (Sandbox Code Playgroud)
当我收到ImportError: No module named 'ansible'错误时,我验证了 Ansible 可以访问,并发现ansible-playbook它只是一个 Python 脚本:
$ which ansible
$ less /usr/bin/ansible-playbook
Run Code Online (Sandbox Code Playgroud)
我认为可能是安装出了问题,所以我重新安装了:
$ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible
Run Code Online (Sandbox Code Playgroud)
最初的卸载只要求删除两件事,这次提出了一份清单。重新安装没有下载任何内容,显然是从缓存中收集了所有内容。
我尝试重新运行我的剧本,但再次出现错误ImportError: No module named 'ansible'。这导致了一个有趣的发现:
$ ansible --version
Traceback (most recent call last):
File "/usr/bin/ansible", line 34, in <module>
from ansible import context
ImportError: No module named ansible
Run Code Online (Sandbox Code Playgroud)
然后我运行less /usr/bin/ansible并发现调用的脚本中的 shebang,#!/usr/bin/python我怀疑这是一个错误。我编辑了该文件,并更改了 shebang 以调用 python3 解释器。结果非常有说服力:
ansible --version
ansible 2.9.2
config file = /home/MAGICLEAP/fkoschara/.ansible.cfg
configured module search path = ['/home/MAGICLEAP/fkoschara/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
ansible python module location = /usr/local/lib/python3.5/dist-packages/ansible
executable location = /usr/bin/ansible
python version = 3.5.2 (default, Oct 8 2019, 13:06:37) [GCC 5.4.0 20160609]
Run Code Online (Sandbox Code Playgroud)
显然pip3,安装未能为可执行文件设置正确的 shebang ansible。搜索所有文件后,/usr/bin/ansible*我发现ansible-connection还有一个pythonshebang,而不是python3一个,但所有其他文件都是正确的。
修复了不正确的 shebangs 后,我的剧本运行正确。
问:我想更改 ansible 以在控制器(Ubuntu)上使用 python3.5。
A: Ubuntu 18.04 中最新的 ansible 2.8 包使用 Python2
> cat /etc/apt/sources.list.d/ppa_launchpad_net_ansible_ansible_2_8_ubuntu.list
deb http://ppa.launchpad.net/ansible/ansible-2.8/ubuntu bionic main
> ansible --version
ansible 2.8.4
[...]
python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
Run Code Online (Sandbox Code Playgroud)
但如果有的话,也可以在远程主机上使用 Python3。例如
> grep test_01 hosts
test_01 ansible_python_interpreter=/usr/local/bin/python3.6
> ansible test_01 -m ping -vvv
ansible 2.8.4
[...]
python version = 2.7.15+ (default, Nov 27 2018, 23:36:35) [GCC 7.3.0]
[...]
Run Code Online (Sandbox Code Playgroud)
SSH: EXEC ssh -C -o ControlMaster=auto -o ControlPersist=60s -o KbdInteractiveAuthentication=no -o PreferredAuthentications=gssapi-with-mic,gssapi-keyex,hostbased,publickey -o PasswordAuthentication=no -o 'User="admin “' -o ConnectTimeout=30 -o [...] 成功-lwccvmomxxdjjpbipvzertvrtfluaqbt ;/usr/local/bin/python3.6 '"'"'"'"'"'"'"'"' && sleep 0'"'"'' 升级成功 (0, '\n{"ping": " pong", "调用": {"module_args": {"data": "pong"}}}\n', '')
如果您确实需要“掌握 Ansible Python3”,您可能想尝试 pip。请参阅pip install ansible。它适用于 Python2 和 Python3。请注意正确设置路径。为了确定您正在运行什么,请卸载 Ubuntu ansible 软件包。
| 归档时间: |
|
| 查看次数: |
11158 次 |
| 最近记录: |