[Python] Dynamic Pipeline for deploying Azure resources with Python

Discussão em 'Python' iniciado por Stack, Março 5, 2025.

  1. Stack

    Stack Membro Participativo

    I have 6 different python srcipts. Each script deploys different resource to Azure and accepts different parameters. I would like to achieve this scenario:

    I have a pipeline that will do the following:

    1. Ask which resource to deploy as I'm not always willing to deploy just one resource or all of them. I want to be able to choose.
    2. Ask to provide parameters depending on my choice from step 1.
    3. Deploy only resource selected in step 1.

    So I have already created a pipeline in Azure Devops and it works fine (including integration with Git, it automatically installs python libraries, etc.) The problem is that I have to define parameters for all of my scripts in my yaml file and the list becomes long and messy. It's not very usefull when I know that I won't be deploying all resources. I have an example of my yaml file with just two resources to choose:

    trigger:
    - none

    pool:
    vmImage: ubuntu-latest

    parameters:
    - name: tenant_id
    displayName: Tenant ID
    type: string
    default:

    - name: SP_client_id
    displayName: Service Principal Client ID
    type: string
    default:

    - name: SP_client_secret
    displayName: Service Principal Client secret
    type: string
    default:

    - name: subscription
    displayName: Subscription ID
    type: string
    default:

    - name: dev_environment
    displayName: Do you want to create a DEV environment?
    type: boolean
    default: True

    - name: test_environment
    displayName: Do you want to create a TEST environment?
    type: boolean
    default: True

    - name: prod_environment
    displayName: Do you want to create a PROD environment?
    type: boolean
    default: True

    #################################################

    - name: deploy_storage
    displayName: "Deploy Storage?"
    type: boolean
    default: false

    - name: storage_rg_name
    displayName: Resource Group name.
    type: string
    default: 'abc'

    - name: storage_name
    displayName: Storage name.
    type: string
    default: 'abc'

    - name: storage_location
    displayName: Storage location
    type: string
    values:
    - northeurope
    - westeurope
    default: 'northeurope'

    - name: storage_sku
    displayName: Storage SKU
    type: string
    values:
    - 'Standard_LRS'
    - 'Standard_GRS'
    - 'Standard_RAGRS'
    default: 'Standard_LRS'

    - name: storage_kind
    displayName: Storage kind
    type: string
    values:
    - 'StorageV2'
    - 'BlobStorage'
    - 'FileStorage'
    - 'BlockBlobStorage'
    default: 'StorageV2'

    - name: storage_hns
    displayName: Enable HNS?
    type: boolean
    default: True

    ################################################################


    - name: deploy_adf
    displayName: "Deploy ADF?"
    type: boolean
    default: false

    - name: adf_rg_name
    displayName: Resource Group name. If does not exists, new one will be created.
    type: string
    default: your_resource_group_name

    - name: adf_name
    displayName: ADF name.
    type: string
    default: testowyadf

    - name: adf_location
    displayName: ADF location
    type: string
    values:
    - northeurope
    - westeurope
    default: northeurope

    - name: adf_ir_flag
    displayName: Do you want to create a SHIR and save it's key in KeyVault?
    type: boolean
    default: False

    - name: adf_shir_name
    displayName: SHIR name. Leave blank if not applicable.
    type: string
    default:

    - name: adf_secret_name
    displayName: KV secret name. Leave blank if not applicable.
    type: string
    default:

    - name: adf_keyvault_url
    displayName: KeyVault URL. Leave blank if not applicable.
    type: string
    default:

    stages:
    - stage: SelectPipeline
    displayName: "Select and Run Pipeline"
    jobs:
    - job: RunSelectedPipeline
    steps:
    # Logic to run Pipeline1
    - ${{ if eq(parameters.deploy_storage, true) }}:
    - template: MainPipelineTest_StoragePipeline.yml
    parameters:
    tenant: ${{ parameters.tenant_id }}
    client_id: ${{ parameters.SP_client_id }}
    client_secret: ${{ parameters.SP_client_secret }}
    subscription: ${{ parameters.subscription }}
    storage_rg_name: ${{ parameters.storage_rg_name }}
    storage_name: ${{ parameters.storage_name }}
    storage_location: ${{ parameters.storage_location }}
    storage_sku: ${{ parameters.storage_sku }}
    storage_kind: ${{ parameters.storage_kind }}
    storage_hns: ${{ parameters.storage_hns }}
    dev_environment: ${{ parameters.dev_environment }}
    test_environment: ${{ parameters.test_environment }}
    prod_environment: ${{ parameters.prod_environment }}

    - ${{ if eq(parameters.deploy_adf, true) }}:
    - template: MainPipelineTest_ADFPipeline.yml
    parameters:
    tenant: ${{ parameters.tenant_id }}
    client_id: ${{ parameters.SP_client_id }}
    client_secret: ${{ parameters.SP_client_secret }}
    subscription: ${{ parameters.subscription }}
    adf_rg_name: ${{ parameters.adf_rg_name }}
    adf_name: ${{ parameters.adf_name }}
    adf_location: ${{ parameters.adf_location }}
    adf_ir_flag: ${{ parameters.adf_ir_flag }}
    adf_shir_name: ${{ parameters.adf_shir_name }}
    adf_secret_name: ${{ parameters.adf_secret_name }}
    adf_keyvault_url: ${{ parameters.adf_keyvault_url }}
    dev_environment: ${{ parameters.dev_environment }}
    test_environment: ${{ parameters.test_environment }}
    prod_environment: ${{ parameters.prod_environment }}


    Is there a way to make it more dynamic? So it will ask for parameters only for selected resources? Maybe there is different approach than Azure Devops for that?

    Thanks in advance.

    Continue reading...

Compartilhe esta Página