首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >在Azure Devops管道运行的不可用剧本中使用azure_rm_resourcegroup时出错

在Azure Devops管道运行的不可用剧本中使用azure_rm_resourcegroup时出错
EN

Stack Overflow用户
提问于 2022-10-25 15:04:52
回答 1查看 113关注 0票数 1

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

代码语言:javascript
复制
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管道:

代码语言:javascript
复制
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)

我的剧本:

代码语言:javascript
复制
---

- name: config azure environment
  hosts: localhost
  connection: local
  gather_facts: true
  collections:
    - azure.azcollection

  vars_files:
    - group_vars/common.yml

  roles:
    - roles/resourcegroup

以及作用:

代码语言:javascript
复制
---

- 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),一切都应该是好的。所以,我错过了什么?我已经搜索了几个小时了,但我还没有找到一个有效的解决方案:

EN

回答 1

Stack Overflow用户

发布于 2022-11-10 18:49:14

在此期间。我有个可行的解决方案。我从头开始,创建了一个Azure管道模板,没有角色。

例如,我使用我的管道来创建一个码头容器注册中心,但是它适用于您想在Azure中使用管道中的播放本所做的任何事情。就用这个例子来学习我是如何让它工作的。希望它能帮助那些在同样的问题上挣扎的人。

代码语言:javascript
复制
- 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

run_ansible步骤文件:

代码语言:javascript
复制
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)

剧本:

代码语言:javascript
复制
- 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
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74196208

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档