Azure Ubuntu虚拟机中Ansible动态库存配置故障求助
解决Ansible Azure动态库存解析失败问题
问题核心
从报错日志里的name 'azure_cloud' is not defined可以确定,这是azure.azcollection集合缺失必要Python依赖库导致的,并非凭证本身无效,先优先解决依赖问题。
修复步骤
1. 安装azure.azcollection完整依赖
azure.azcollection需要Azure Python SDK支持,执行以下命令补全依赖:
pip install azure-core azure-mgmt-resource azure-mgmt-compute azure-mgmt-network azure-mgmt-storage
同时强制更新azure.azcollection到最新版本:
ansible-galaxy collection install azure.azcollection --force
2. 修正凭证文件权限
Azure凭证文件需要严格的权限控制,否则Ansible会拒绝读取:
chmod 600 /home/azureuser/.azure/credentials
3. 调整inventory配置
在inventory_azure_rm.yml中显式指定云环境,避免SDK默认值加载失败,修改后内容:
plugin: azure.azcollection.azure_rm auth_source: credential_file include_vm_resource_groups: - devops cloud: AzurePublicCloud
另外,你创建的azure_rm.ini是旧版azure_rm插件的配置,当前azure.azcollection插件不需要该文件,可直接删除。
4. 重新测试验证
执行命令重新检查库存解析情况:
ansible-inventory -vvvv -i /home/azureuser/.ansible/inventory_azure_rm.yml --list
额外排查点
如果问题仍存在,检查以下内容:
- 服务主体是否已在
devops资源组上分配至少ReaderRBAC角色 - 若之前导出过
AZURE_SUBSCRIPTION_ID等环境变量,先执行unset清除,避免和凭证文件配置冲突 - 确认
/home/azureuser/.azure/credentials中的subscription_id、client_id、secret、tenant无多余空格或输入错误
内容的提问来源于stack exchange,提问作者jma
相关产品推荐
相关产品推荐

