Ansible ec2.py dynamic inventory errors: ImportError: cannot import name 'ec2'

Ansible ec2.py dynamic inventory errors: ImportError: cannot import name 'ec2'

我当前的机器崩溃了,我必须使用相同的方法在新机器上再次设置 ansible,但似乎我遗漏了一些东西。出于一致性原因,我需要使用版本 2.X,因为我们尚未升级。

我经常使用 ec2.py 和 ec2.ini 所以当我 运行

~ python /Users/joseph/infrastructure_as_code/ansible_projects/inventory/ec2.py
Traceback (most recent call last):
File  "/Users/joseph/infrastructure_as_code/ansible_projects/inventory/ec2.py", line 172, in <module>
from ansible.module_utils import ec2 as ec2_utils
ImportError: cannot import name 'ec2' from 'ansible.module_utils'  (/Users/joseph/.pyenv/versions/pypy3.8-7.3.7/lib/pypy3.8/site-  packages/ansible/module_utils/__init__.py)

这是我的设置。全新的 MacBook Pro M1。

#Mac, no prior ansible installation either brew or pip
#Check Python version
➜  ~ python --version
Python 3.8.12 (9ef55f6fc369, Oct 25 2021, 05:10:01)
[PyPy 7.3.7 with GCC Apple LLVM 13.0.0 (clang-1300.0.29.3)]

#Check version used by shebang
➜  ~ /usr/bin/env python --version
Python 3.8.12 (9ef55f6fc369, Oct 25 2021, 05:10:01)
[PyPy 7.3.7 with GCC Apple LLVM 13.0.0 (clang-1300.0.29.3)]

## Check ansible version. Had to use version 2 for now. Installed with pip over python installed by peen
➜  ~ ansible --version
ansible 2.10.17
  config file = /Users/joseph/.ansible.cfg
  configured module search path = ['/Users/joseph/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules']
  ansible python module location = /Users/joseph/.pyenv/versions/pypy3.8-7.3.7/lib/pypy3.8/site-packages/ansible
  executable location = /Users/joseph/.pyenv/versions/pypy3.8-7.3.7/bin/ansible
  python version = 3.8.12 (9ef55f6fc369, Oct 25 2021, 05:10:01)[PyPy 7.3.7 with GCC  Apple LLVM 13.0.0 (clang-1300.0.29.3)]

#Run ec2.py inventory script
➜  ~ python   /Users/joseph/infrastructure_as_code/ansible_projects/inventory/ec2.py
Traceback (most recent call last):
File   "/Users/joseph/infrastructure_as_code/ansible_projects/inventory/ec2.py ", line 172, in <module>
from ansible.module_utils import ec2 as ec2_utils
ImportError: cannot import name 'ec2' from 'ansible.module_utils' (/Users/joseph/.pyenv/versions/pypy3.8-7.3.7/lib/pypy3.8/site-   packages/ansible/module_utils/__init__.py)

#$PATH
             /Users/joseph/.sdkman/candidates/java/current/bin:/Users/joseph/.pyenv/plugins/pyenv- virtualenv/shims:/Users/joseph/.pyenv/shims:/Users/joseph/.pyenv/bin:/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin

## Content of ~/.ansible.cfg
[defaults]
interpreter_python=/Users/joseph/.pyenv/shims/python

## Which python
/Users/joseph/.pyenv/shims/python

## other python
➜  ~ pyenv versions
   system
   3.10.2
   3.8.12
   ansible-2x
   * pypy3.8-7.3.7 (set by /Users/joseph/.pyenv/version)
   pypy3.8-7.3.7/envs/ansible-2x

我已经阅读了一些关于 PATH 的解决方案,并对配置文件中的 interpreter_python 设置寄予厚望,但 none 确实有效。所以我不确定出了什么问题,我也切换到不同版本的 python 但它什么也没做。不确定缺少什么,我将不胜感激帮助解决这个问题。提前致谢

这是因为您有 ansible v2.10.17 而该文件仅存在于 v2.9 系列中:https://github.com/ansible/ansible/blob/v2.9.27/lib/ansible/module_utils/ec2.py

将来,pip freeze 将发出一个 requirements.txt 文件,其中的版本固定到当前使用的版本,这可以帮助这种漂移