在由 Azure DevOps 管道运行的 Ansible 行动手册中使用azure_rm_resourcegroup时出错"No module named 'azure.storage.cloud



据我所知,我在我的Ansible剧本中安装了所有依赖项来使用azure模块,但我仍然收到这个错误。

The full traceback is:
Traceback (most recent call last):
File "/tmp/ansible_azure_rm_resourcegroup_payload_7l31ymh4/ansible_azure_rm_resourcegroup_payload.zip/ansible_collections/azure/azcollection/plugins/module_utils/azure_rm_common.py", line 250, in <module>
from azure.storage.cloudstorageaccount import CloudStorageAccount
ModuleNotFoundError: No module named 'azure.storage.cloudstorageaccount'

我的Azure Devops管道:

pool:
vmImage: 'ubuntu-latest'
steps:
- task: UsePythonVersion@0
displayName: 'Install Python'
inputs:
versionSpec: '3.x'
addToPath: true
architecture: 'x64'
- task: AzureCLI@2
inputs:
azureSubscription: '$(AZURE_SUBSCRIPTION_NAME)'
addSpnToEnvironment: true
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "##vso[task.setvariable variable=ARM_SUBSCRIPTION_ID]$(az account show --query='id' -o tsv)"
echo "##vso[task.setvariable variable=ARM_CLIENT_ID]${servicePrincipalId}"
echo "##vso[task.setvariable variable=ARM_CLIENT_SECRET]${servicePrincipalKey}"
echo "##vso[task.setvariable variable=ARM_TENANT_ID]${tenantId}"
- script: pip install ansible[azure]
displayName: 'Install Ansible'
- script: ansible-galaxy collection install azure.azcollection
displayName: 'Install Ansible Azure Collection' 
- script: pip install -r https://raw.githubusercontent.com/ansible-collections/azure/dev/requirements-azure.txt
displayName: 'Install Azure modules needed'
- script: pip install azure-storage-blob azure-storage-file-share azure-storage-file-datalake azure-storage-queue
displayName: 'Install missing modules (to be sure to have the azure storage modules)'
- script: ansible-playbook -vvv -i inv site.yml
displayName: 'Run Ansible Playbook'
env:
AZURE_CLIENT_ID: $(ARM_CLIENT_ID)
AZURE_SECRET: $(ARM_CLIENT_SECRET)
AZURE_TENANT: $(ARM_TENANT_ID)
AZURE_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)

我的战术手册:

---
- name: config azure environment
hosts: localhost
connection: local
gather_facts: true
collections:
- azure.azcollection
vars_files:
- group_vars/common.yml
roles:
- roles/resourcegroup

以及角色:

---
- name: create a resource group
azure_rm_resourcegroup:
name: "{{ app.name }}-{{ dict.resource_group }}"
location: "{{ azure.location }}"
state: present

根据文件(https://docs.ansible.com/ansible/latest/collections/azure/azcollection/azure_rm_resourcegroup_module.html)一切都应该好起来。那么,我错过了什么??我已经在谷歌上搜索了几个小时,但我还没有找到一个有效的解决方案:-(

与此同时。我有一个可行的解决方案。我从零开始,创建了一个没有角色的Azure管道模板。

举个例子,我用我的管道创建了一个docker容器注册表,但它适用于你想在Azure中使用管道中的剧本做的所有事情。只要用这个例子来学习我是如何使它工作的。希望它能帮助其他正在与同样问题作斗争的人。

- task: Bash@3
displayName: 'create vars file for docker registry playbook'
inputs:
targetType: 'inline'
workingDirectory: './playbooks'
script: |
touch vars.yml
echo 'azure:' > vars.yml
echo '  location: "${{ parameters.azure_location }}"' >> vars.yml
echo '  resourcegroup: "${{ parameters.resourcegroup_name }}"' >> vars.yml
echo '  containerregistry: "${{ parameters.containerregistry_name }}"' >> vars.yml
cat vars.yml
- template: steps/run_ansible.yml
parameters:
playbook: playbooks/setup_dockerhub.yml
varsfile: playbooks/vars.yml

可运行的步骤文件:

parameters:
- name: playbook
type: string
- name: varsfile
type: string
steps:
- task: AzureCLI@2
displayName: 'install Azure CLI'
inputs:
#azureSubscription: '$(AZURE_SUBSCRIPTION_NAME)'
connectedServiceNameARM: 'ARM Outstanding24'
addSpnToEnvironment: true
scriptType: 'bash'
scriptLocation: 'inlineScript'
inlineScript: |
echo "##vso[task.setvariable variable=ARM_SUBSCRIPTION_ID]$(az account show --query='id' -o tsv)"
echo "##vso[task.setvariable variable=ARM_CLIENT_ID]${servicePrincipalId}"
echo "##vso[task.setvariable variable=ARM_CLIENT_SECRET]${servicePrincipalKey}"
echo "##vso[task.setvariable variable=ARM_TENANT_ID]${tenantId}"
- script: sudo apt install -y python3-pip
displayName: 'install pip'
- script: sudo pip3 install --upgrade pip
displayName: 'ensure we have the latest version of pip3'
- script: pip3 install "ansible==2.9.17"
displayName: 'install ansible 2.9'
- script: pip3 install ansible[azure]
displayName: 'install ansible "azure modules"'
- script: 'ansible-playbook -v ${{ parameters.playbook }} --extra-vars @${{ parameters.varsfile }}'
displayName: 'run azure playbook'
env:
AZURE_CLIENT_ID: $(ARM_CLIENT_ID)
AZURE_SECRET: $(ARM_CLIENT_SECRET)
AZURE_TENANT: $(ARM_TENANT_ID)
AZURE_SUBSCRIPTION_ID: $(ARM_SUBSCRIPTION_ID)

战术手册:

- name: setup docker registry in Azure
hosts: localhost
connection: local
gather_facts: false
collections:
- azure.azcollection
vars_files:
- vars.yml
tasks:
- name: ensure the resourcegroup exists
azure_rm_resourcegroup:
name: "{{ azure.resourcegroup }}"
location: "{{ azure.location }}"
state: present
- name: ensure docker registry exists
azure_rm_containerregistry:
name: "{{ azure.containerregistry }}"
location: "{{ azure.location }}"
resource_group: "{{ azure.resourcegroup }}"
sku: Basic
state: present

最新更新