Search code examples
azureazure-devopsazure-akskubeconfig

Accessing kubeconfig file from Azure artifacts


I am new to Azure DevOps and have a requirement where AKS cluster is provisioned through terraform which creates two kubeconfig admin & user files in Azure Devops artifacts. Now I have to call this kubeconfig files to another pipeline to manage post-provisioning activities of AKS cluster through Ansible playbooks.

Is there any way this can be achieved.


Solution

    • One way is to build your terraform with Build yaml pipeline and use the build artifact in a release pipeline, Your build artifact will include the kube config files and then run your ansible task in Release pipeline.

    • Another way is to Publish your artifact and Download your published artifact and use it in your ansible task with the YAML pipeline code below:-

    trigger:
    - main
    
    pr: none
    
    stages:
    - stage: DeployToAKS
      jobs:
      - deployment: Deploy
        pool:
          vmImage: 'ubuntu-latest'
        environment: 'your-environment-name'  # Optional, if you're using environments
        strategy:
          runOnce:
            deploy:
              steps:
              - task: DownloadPipelineArtifact@2
                inputs:
                  artifactName: 'kubeconfig'
                  targetPath: '$(Pipeline.Workspace)/kubeconfig'
    
              - script: |
                  # Use the kubeconfig files in your Ansible playbooks
                  ansible-playbook -i inventory.yml playbook.yml
                displayName: 'Run Ansible Playbooks'
                env:
                  KUBECONFIG_ADMIN: $(Pipeline.Workspace)/kubeconfig/admin_kubeconfig
                  KUBECONFIG_USER: $(Pipeline.Workspace)/kubeconfig/user_kubeconfig
    

    I deployed one AKS with kubeconfig with Azure Devops Release pipeline like below:-

    enter image description here

    enter image description here

    enter image description here

    Publish the Artifact and Download the Published Artifact like below and run your Ansible playbook with Ansible playbook Run task:-

    enter image description here