繁体   English   中英

运行 GitActions Pipeline 在 Azure 和 Ansible 中创建资源无法从本地缓存中检索服务主体的凭据

[英]Running GitActions Pipeline to create resources in Azure with Ansible Could not retrieve credential from local cache for service principal

我正在尝试使用 Ansible 在 GitActions 上开发一个管道,在与剧本和环境的兼容性出现一些问题之后,我终于能够将我的 Ubuntu 20.04 本地机器与 Z3A580F140389866 连接起来。 我的目标是使用 GitActions 管道在 Azure 上部署 vm。

我需要使用我已经创建并添加到 git 操作机密的服务主体与 azure 连接,并使用 AZURE_CREDENTIAL json 文件。

on: [push]

name: AzureLoginSample

jobs:
  build-and-deploy:
    runs-on: ubuntu-latest
    steps:    
      - name: Checkout
        uses: actions/checkout@master
        with:
          path: main
      - name: Azure Login
        uses: azure/login@v1
        with:
          enable-AzPSSession: true
          creds: ${{ secrets.AZURE_CREDENTIALS }}
      - name: Azure CLI script
        uses: azure/CLI@v1
        with:
          azcliversion: latest
          inlineScript: |
            set -ex
            ls ~/.azure
            cat ~/.azure/versionCheck.json
            az --version
            az account show
            az group list
      - name: Install Ansible
        run: |
          sudo apt update
          sudo apt install python3-pip
          sudo apt install python3.8-venv
          python3 -m venv ansible-playbook && . ansible-playbook/bin/activate && pip3 install --upgrade pip && pip3 install wheel
          pip3 install ansible==2.10.0
          ansible-galaxy collection install azure.azcollection
          pip3 install -r ~/.ansible/collections/ansible_collections/azure/azcollection/requirements-azure.txt
      - name: Create Azure Resource Group with Ansible Playbook
        run: |
          ./ansible-playbook/bin/ansible-playbook main/ansible/playbook/create_rg_azcollection.yml -vvv

azure/login@v1 和 Azure CLI 脚本正在运行,但最后我的工作安装 ansible 由于身份验证错误而无法运行。

无法从本地缓存中检索服务主体 *** 的凭据。 请为此服务主体运行“az login”。\n"

我很清楚,我的 ansible 步骤使用了不同的 python 解释器,因此之前使用 AZ 的登录不起作用,因为我在 ansible 剧本环境中运行。 你知道如何解决这个问题吗?

我终于意识到如何解决,只需将凭据复制到 GitActions 管道的用户中,名为 runner 在 $HOME/.azure 文件夹中。

我使用的命令是:

cp <path/from/repos/credential> ~/.azure

在凭证文件中,您需要存储来自 azure 服务主体创建的 output

'az ad sp create-for-rbac --name GitActionsPipeline --role Contributor --sdk-auth'

            {
            "clientId": "************************************",
            "clientSecret": "*********************************",
            "subscriptionId": "*********************************",
            "tenantId": "*********************************",
            "activeDirectoryEndpointUrl": "https://login.microsoftonline.com",
            "resourceManagerEndpointUrl": "https://management.azure.com/",
            "activeDirectoryGraphResourceId": "https://graph.windows.net/",
            "sqlManagementEndpointUrl": 
            "https://management.core.windows.net:8443/",
            "galleryEndpointUrl": "https://gallery.azure.com/",
            "managementEndpointUrl": "https://management.core.windows.net/"
             }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM