Title: Running GitActions Pipeline to create resources in Azure with Ansible Could not retrieve credential from local cache for service principal
Tags: azure;github;azure-devops;ansible;devops
Question: I'm trying to develop a pipeline on GitActions with Ansible, after some issue with compatibility with playbook and enviroment I was finally able to connect my Ubuntu 20.04 local machine with Azure. My goal is to use a GitActions pipeline to deploy vm on Azure.
I need to connect with azure with a service principal that I've already created and added to git Action secrets with AZURE_CREDENTIAL json file.
```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 &amp;&amp; . ansible-playbook/bin/activate &amp;&amp; pip3 install --upgrade pip &amp;&amp; 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 and Azure CLI script are working, but at the end my job install ansible is not working due to authentication error.
```
Could not retrieve credential from local cache for service principal ***. Please run 'az login' for this service principal.\n&quot;
```
It's clear to me that my ansible steps use differnt python interpreter so the previous login with AZ is not working because I'm running on ansible playbook environment.
Do you have any idea how to solve this issue?

Comment: Someone has already had this issue, it seems latest version of Az cli is causing the error. https://github.com/Azure/azure-cli/issues/20153 but I can't understand how to solve. Do I need to install Az cli during the ansible steps with pip install and specify the interpreter?


Here is another answer: I finally realized how to solve, just copy the credentials into GitActions pipeline's user, named runner in $HOME/.azure folder.
The command I use is :
cp <path/from/repos/credential&gt; ~/.azure
inside credentials file you need to store the output from azure service principal creation
'az ad sp create-for-rbac --name GitActionsPipeline --role Contributor --sdk-auth'
```            {
            &quot;clientId&quot;: &quot;************************************&quot;,
            &quot;clientSecret&quot;: &quot;*********************************&quot;,
            &quot;subscriptionId&quot;: &quot;*********************************&quot;,
            &quot;tenantId&quot;: &quot;*********************************&quot;,
            &quot;activeDirectoryEndpointUrl&quot;: &quot;https://login.microsoftonline.com&quot;,
            &quot;resourceManagerEndpointUrl&quot;: &quot;https://management.azure.com/&quot;,
            &quot;activeDirectoryGraphResourceId&quot;: &quot;https://graph.windows.net/&quot;,
            &quot;sqlManagementEndpointUrl&quot;: 
            &quot;https://management.core.windows.net:8443/&quot;,
            &quot;galleryEndpointUrl&quot;: &quot;https://gallery.azure.com/&quot;,
            &quot;managementEndpointUrl&quot;: &quot;https://management.core.windows.net/&quot;
             }
```


{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 1, 'CommentCount': 1, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2022:00:01/24/22', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<azure><github><azure-devops><ansible><devops>', 'Title': 'Running GitActions Pipeline to create resources in Azure with Ansible Could not retrieve credential from local cache for service principal', 'ViewCount': 119, 'id': 70835947, 'user_AboutMe': None, 'user_AccountId': 24031403.0, 'user_DisplayName': 'Syd-Crypto-Dev-Ops', 'user_Reputation': 11.0, 'user_Views': 2.0, 'user_id': 18012240.0}}