已安装azure.common仍遇ModuleNotFoundError的技术求助
Ansible运行时
ModuleNotFoundError: No module named 'azure.common'问题排查及解决 问题概述
运行Ansible剧本时触发ModuleNotFoundError: No module named 'azure.common'错误,但Python3.6和3.8环境均已安装azure-common 1.1.28及azure-storage-common 1.4.2,需明确程序查找azure.common模块的路径及正确安装位置。
已安装模块验证
sh-4.4# pip3.6 list | grep common azure-common 1.1.28 azure-storage-common 1.4.2 sh-4.4# pip3.8 list | grep common azure-common 1.1.28 azure-storage-common 1.4.2
Ansible运行日志及报错详情
Identity added: /tmp/ansible-runner20220907-326-1noxw49/artifacts/result/ssh_key_data (/tmp/ansible-runner20220907-326-1noxw49/artifacts/result/ssh_key_data) [DEPRECATION WARNING]: Ansible will require Python 3.8 or newer on the controller starting with Ansible 2.12. Current version: 3.6.8 (default, Jan 14 2022, 11:04:20) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)]. This feature will be removed from ansible-core in version 2.12. Deprecation warnings can be disabled by setting deprecation_warnings=False in ansible.cfg. /usr/local/lib/python3.6/site-packages/ansible/parsing/vault/__init__.py:44: CryptographyDeprecationWarning: Python 3.6 is no longer supported by the Python core team. Therefore, support for it is deprecated in cryptography and will be removed in a future release. from cryptography.exceptions import InvalidSignature ansible-playbook [core 2.11.12] config file = /root/.ansible.cfg configured module search path = ['/root/.ansible/plugins/modules', '/usr/share/ansible/plugins/modules'] ansible python module location = /usr/local/lib/python3.6/site-packages/ansible ansible collection location = /root/.ansible/collections:/usr/share/ansible/collections executable location = /usr/local/bin/ansible-playbook python version = 3.6.8 (default, Jan 14 2022, 11:04:20) [GCC 8.5.0 20210514 (Red Hat 8.5.0-7)] jinja version = 3.0.3 libyaml = True Using /root/.ansible.cfg as config file host_list declined parsing /tmp/ansible-runner20220907-326-1noxw49/inventory/hosts as it did not pass its verify_file() method script declined parsing /tmp/ansible-runner20220907-326-1noxw49/inventory/hosts as it did not pass its verify_file() method auto declined parsing /tmp/ansible-runner20220907-326-1noxw49/inventory/hosts as it did not pass its verify_file() method Parsed /tmp/ansible-runner20220907-326-1noxw49/inventory/hosts inventory source with ini plugin [WARNING]: While constructing a mapping from /tmp/ansible-runner- git20220907-328-r4mshj/azure_sg_create.yaml, line 36, column 7, found a duplicate dict key (azure_vm_network_interface). Using last defined value only. redirecting (type: modules) ansible.builtin.azure_rm_networkinterface_info to azure.azcollection.azure_rm_networkinterface_info redirecting (type: modules) ansible.builtin.azure_rm_securitygroup to azure.azcollection.azure_rm_securitygroup redirecting (type: modules) ansible.builtin.azure_rm_securitygroup to azure.azcollection.azure_rm_securitygroup redirecting (type: modules) ansible.builtin.azure_rm_resource_info to azure.azcollection.azure_rm_resource_info redirecting (type: modules) ansible.builtin.azure_rm_networkinterface to azure.azcollection.azure_rm_networkinterface Skipping callback 'awx_display', as we already have a stdout callback. Skipping callback 'default', as we already have a stdout callback. Skipping callback 'minimal', as we already have a stdout callback. Skipping callback 'oneline', as we already have a stdout callback. PLAYBOOK: azure_sg_create.yaml ************************************************* 1 plays in azure_sg_create.yaml PLAY [localhost] *************************************************************** META: ran handlers TASK [Get facts by name] ******************************************************* task path: /tmp/ansible-runner-git20220907-328-r4mshj/azure_sg_create.yaml:18 <localhost> ESTABLISH LOCAL CONNECTION FOR USER: root <localhost> EXEC /bin/sh -c '( umask 77 && mkdir -p "` echo /tmp/.ansible_remote_tmp `"&& mkdir "` echo /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938 `" && echo ansible-tmp-1662559522.4644003-30346-31917676404938="` echo /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938 `" ) && sleep 0' Using module file /root/.ansible/collections/ansible_collections/azure/azcollection/plugins/modules/azure_rm_virtualmachine_info.py <localhost> PUT /tmp/.ansible_local_tmp/ansible-local-30296sqivv1jk/tmp_vbwz1a0 TO /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938/AnsiballZ_azure_rm_virtualmachine_info.py <localhost> EXEC /bin/sh -c 'chmod u+x /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938/ /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938/AnsiballZ_azure_rm_virtualmachine_info.py && sleep 0' <localhost> EXEC /bin/sh -c '/usr/bin/python3.6 /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938/AnsiballZ_azure_rm_virtualmachine_info.py && sleep 0' <localhost> EXEC /bin/sh -c 'rm -f -r /tmp/.ansible_remote_tmp/ansible-tmp-1662559522.4644003-30346-31917676404938/ > /dev/null 2>&1 && sleep 0' The full traceback is: Traceback (most recent call last): File "/tmp/ansible_azure.azcollection.azure_rm_virtualmachine_info_payload_zdtmpkmo/ansible_azure.azcollection.azure_rm_virtualmachine_info_payload.zip/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py", line 234, in <module> from azure.common.credentials import ServicePrincipalCredentials, UserPassCredentials ModuleNotFoundError: No module named 'azure.common' fatal: [localhost -> localhost]: FAILED! => { "changed": false, "invocation": { "module_args": { "ad_user": null, "adfs_authority_url": null, "api_profile": "latest", "auth_source": "auto", "cert_validation_mode": null, "client_id": null, "cloud_environment": "AzureCloud", "log_mode": null, "log_path": null, "name": "Ubuntu973", "password": null, "profile": null, "resource_group": "cloud-shell-storage-centralindia", "secret": null, "subscription_id": null, "tags": null, "tenant": null } }, "msg": "Failed to import the required Python library (ansible[azure] (azure >= 2.0.0)) on a3b2d24ea52b's Python /usr/bin/python3.6. Please read the module documentation and install it in the appropriate location. If the required library is installed, but Ansible is using the wrong Python interpreter, please consult the documentation on ansible_python_interpreter" } PLAY RECAP ********************************************************************* localhost : ok=0 changed=0 unreachable=0 failed=1 skipped=0 rescued=0 ignored=0
问题分析与解决
1. 模块查找路径确认
从日志可以看到,Ansible执行模块时使用的Python解释器是/usr/bin/python3.6,它会按以下顺序查找模块:
- 当前执行脚本的临时目录
- Python内置模块路径
sys.path环境变量中列出的路径,可通过以下命令查看具体路径:
/usr/bin/python3.6 -c "import sys; print('\n'.join(sys.path))"
通常包含/usr/bin/python3.6对应的site-packages目录,这是第三方模块的默认安装位置。
2. 正确安装位置及操作
你需要确保azure-common模块安装在/usr/bin/python3.6对应的Python环境中。可能存在pip3.6指向的Python环境与/usr/bin/python3.6不一致的情况,执行以下命令验证:
pip3.6 --version
如果输出中的Python路径不是/usr/bin/python3.6,则用绝对路径的pip重新安装:
/usr/bin/python3.6 -m pip install azure-common==1.1.28 azure-storage-common==1.4.2
3. 额外排查点
- 检查是否有虚拟环境处于激活状态,导致模块安装到虚拟环境而非全局环境
- 若为远程执行,需确保目标主机对应Python环境中安装了模块(本案例为localhost,无需考虑)
- 可在Ansible剧本中指定
ansible_python_interpreter变量,强制使用正确的解释器:
- hosts: localhost vars: ansible_python_interpreter: /usr/bin/python3.6 tasks: # 你的任务内容
内容的提问来源于stack exchange,提问作者Robert
相关产品推荐
相关产品推荐

