升级 ansible 以在控制器上使用 python3

新手上路,请多包涵

我想更改 ansible 以在控制器上使用 python3.5。我已经安装了 ansible 和 python3.5,有没有办法将其更改为使用 python3?

ansible 文档建议使用 python3 /usr/bin/ansible localhost -m ping 对 python3 进行测试。但如果这不起作用,请不要提供更多细节。

我的结果是:

 Traceback (most recent call last):
    File "/usr/bin/ansible", line 32, in <module>
        from ansible import context
ImportError: No module named 'ansible'

我也试过 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'

原文由 lajmode 发布,翻译遵循 CC BY-SA 4.0 许可协议

阅读 574
1 个回答

按照 Ansible Python 3 支持 页面上的说明,我使用 pip3 在删除之前的 (2.7) 版本后安装了 Ansible:

 $ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible

当我收到 ImportError: No module named 'ansible' 错误时,我验证了 Ansible 是可访问的,并发现 ansible-playbook 只是一个 Python 脚本:

 $ which ansible
$ less /usr/bin/ansible-playbook

我想可能是安装出了问题,所以我重做了:

 $ sudo -H pip uninstall ansible
$ sudo -H pip3 install ansible

原来的卸载只询问删除两个东西,这次提出了一个洗衣清单。重新安装没有下载任何东西,显然是从缓存中收集了所有内容。

我尝试重新运行我的剧本并再次收到 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

然后我运行 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]

显然 pip3 安装未能为 ansible 可执行文件设置正确的 shebang。 Searching all of /usr/bin/ansible* I discovered ansible-connection also had a python shebang, rather than a python3 one, but all of the other files were正确的。

修复不正确的 shebang 后,我的 playbook 运行正确。

原文由 FKEinternet 发布,翻译遵循 CC BY-SA 4.0 许可协议

撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题