passing an ip address as a parameter into a python script template - azure-devops

I have the following python script with an Azure DevOps pipeline template:
# File: templates/clone-docker-volume.yml
parameters:
sourceVolume: ''
targetVolume: ''
pfaEndpoint: ''
steps:
- task: PythonScript#0
inputs:
scriptSource: 'inline'
script: |
#!/usr/bin/env python3
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fa = myfunc(target="${{ parameters.pfaEndpoint }}")
When I hard code the ip address calls to the script in the template work as expected, when I change the template such that the ip address is parameterized, I get an error with the following:
HTTPSConnectionPool(host='$(pfaendpoint)', port=443)
I'm invoking the script in the template as follows:
- template: templates/python-template.yml
parameters:
pfaEndpoint: '$(pfaEndpoint)'
I suspect this is the problem that is causing the ip address used in the script to appear as '$(pfaEndpoint)'. Can someone please advise how I resolve this such that the ip address is correctly passed into the template.

you can only use that syntax ${{ parameters.something }} if its a single "thing", you cannot embed it into a string. for that you have to use format operator:
script: |
${{ format('#!/usr/bin/env python3
import urllib3
urllib3.disable_warnings(urllib3.exceptions.InsecureRequestWarning)
fa = myfunc(target="{0}")', parameters.pfaEndpoint) }}
if you need 2 parameters use this:
${{ format('{0} {1}', parameters.one, parameters.two) }}
https://learn.microsoft.com/en-us/azure/devops/pipelines/process/expressions?view=azure-devops#format

Related

Azure DevOps send parameters from pipeline to powershell

I tried to find some information in internet. But unfortunately I could not found any information.
Im trying to send pipeline parameters from pipeline into powershell script
pipeline below:
parameters:
- name: env
displayName: Select Environment
type: string
default: development
stages:
- stage: test
displayName: test var
jobs:
- job: PostgresSQL
steps:
- task: PowerShell#2
inputs:
filePath: '$(5ystem. DefaultWorkingDirectory)/test.psl'
errorActionPreference: 'continue'
enabled: true
I need to send ${{ parameters.env }} to powershell.
I tried different type of define param like a variable into powershell. but it does not work.
I would be very happy if anybody can help me and share relevant documentation for that.
Thanks all
First approach is to provide arguments using 'arguments' keyword (available in PowerShell by 'param')
filePath: xyz
arguments: -input1 ${{ parameters.env }}
documentation and example - https://learn.microsoft.com/en-us/azure/devops/pipelines/tasks/reference/powershell-v2?view=azure-pipelines#call-powershell-script-with-multiple-arguments
Second approach you can map parameters to environment variables provided to script using 'env' keyword
env:
input1: ${{ parameters.env }}
documentation - https://learn.microsoft.com/en-us/azure/devops/pipelines/process/tasks?view=azure-devops&tabs=yaml#environment-variables

How to use multiple variables for AzureResourceManagerTemplateDeployment#3

I would like to use a yml variables file, rather than an ARM parameters file, so that I can use a single variable file for multiple bicep deployment tasks and to use the variables in other pipeline tasks without duplication, but am having trouble with the syntax. Below is what I have. It seems to not see it as valid syntax. I get the following error:
There was an error while overriding 'tags' parameter because of 'SyntaxError: Unexpected end of JSON input', make sure it follows JavaScript Object Notation (JSON)
What is the correct syntax or is there a better way that meets the criteria?
# vars.yml contents
rsgName: "rsg1"
location: "westus"
tags: |
{
"tag1": "tagA",
"tag2": "tagB"
}
# deploy.yml contents
- task: AzureResourceManagerTemplateDeployment#3
inputs:
deploymentScope: 'Subscription'
azureResourceManagerConnection: ${{ parameters.azureServiceConnection }}
subscriptionId: ${{ variables.subId }}
templateLocation: 'Linked artifact'
csmFile: ./template.bicep
overrideParameters: >
-rsgName ${{ variables.rsgName }}
-location ${{ variables.location }}
-tags ${{ variables.tags }}
deploymentMode: 'Validation'
# template.bicep contents
param rsgName string
param location string
targetScope = 'subscription'
resource resourceGroup 'Microsoft.Resources/resourceGroups#2021-04-01' = {
name: rsgName
location: location
tags: tags
As per the task doc, it supports multiple parameters overwrite on the task.
If you use classic UI editor of the task, click ... , it could be more clear.
The error should be caused by the incorrect format on the tag definition on your vars.yml .
Remove extra , behind "tag2": "tagB" to fix the json format for a check.
Or you can try to use simple tag content for a check.

Azure DevOps: How to pass on a variable to a template read from Library variable group

I have a variable group defined in pipeline > Library > variable group > called 'template-variable-group'
All that I am trying to accomplish here is to pass on the value of the variable my-temp-var (in this case as you can see its value is my-template-value) to a template from the yaml file.
I have a yaml based pipeline as follows.
variables:
- group: template-variable-group
name: $(date:yyyyMMdd)$(rev:.r)
stages:
- stage: Build
jobs:
- job: buildWebApp
displayName: Build Release pipeline for Discount Service on Master branch
steps:
- script: |
echo Here we go
displayName: 'Command Line Script'
- template: template.yaml
parameters:
variableToTemplate: ${{variables['my-temp-var']}}
And the template.yaml file is as follows.
parameters:
variableToTemplate:
steps:
- task: CmdLine#2
inputs:
script: |
echo Hello Hello Hello Hello Hello Hello
echo ${{ parameters.variableToTemplate }}
displayName: 'Run a two-line script'
I am not able to do that.
As you can see, that value is not reaching the template. What am I missing.
I saw this SO Answer but did not seem to be of help.
You can just pass it like a regular variable:
$(my-temp-var)
Passing it this way $(my-temp-var) will only serve for printing the value somewhere. If you try to use it as a parameter to something say for an ssh connection, the passed value will not work. I am still exploring why but it does not wok.

YAML pipeline definition which retrieves variables values from pipeline variables

I have a YAML file which forms an Azure DevOps pipeline. The pipeline itself defines four variables which are needed in the variables section of the YAML...
variables:
environmentIdentifier: "$(environmentIdentifier)"
keyVaultSourceName: "$(keyVaultSourceName)"
location: "$(location)"
locationIdentifier: "$(locationIdentifier)"
The variables are definitely set for each run of the pipeline, but when it runs I encounter errors further down in my script which indicate that these variables were not populated correctly...
ERROR: (InvalidResourceGroup) The provided resource group name 'rg-main-$(locationIdentifier)' has these invalid characters: '$:'. The name can only be a letter, digit, '-', '.', '(', ')' or '_'.
I've also tried...
$env:location
${{variables['location']}}
...but incurred the same error.
How should I correctly declare vars in the variables section of the pipeline definition, where their values are retrieved from the pipeline's variables?
You need to define as:
variables:
- name: location
value: 'Australia Southeast'
If you want them at a later stage as a template expression use:
${{ variables.location }}
and if you want to use them inside a script:
steps:
- bash: echo $(location)
- powershell: echo $(location)
- script: echo $(location)
Check this Link and the below extracted sample for more information.
variables:
- name: one
value: initialValue
steps:
- script: |
echo ${{ variables.one }} # outputs initialValue
echo $(one)
displayName: First variable pass
- bash: echo '##vso[task.setvariable variable=one]secondValue'
displayName: Set new variable value
- script: |
echo ${{ variables.one }} # outputs initialValue
echo $(one) # outputs secondValue
displayName: Second variable pass

How to use VSTS pipeline variable in YAML?

I have defined a variable in the pipeline and set it to false/true. However, when the pipeline is set, I can see that the value for parameters.RunUnitTest is $(RunUnitTest), and this is not the value I set up in the pipeline. So what I am doing wrong here?
trigger: none
extends:
template: ThunderPipeline.yaml
parameters:
MergeBetweenBranches: true
FromBranch: 'master'
ToBranch: 'R_Current_Sprint'
RunUnitTest: '$(RunUnitTest)'
Variables can be defined in one YAML and included in another template. This could be useful if you want to store all of your variables in one file. If you are using a template to include variables in a pipeline, the included template can only be used to define variables. You can use steps and more complex logic when you are extending from a template. Use parameters instead of variables when you want to restrict type.
In this example, the variable favoriteVeggie is included in azure-pipelines.yml.
# File: vars.yml
variables:
favoriteVeggie: 'brussels sprouts'
# File: azure-pipelines.yml
variables:
- template: vars.yml # Template reference
steps:
- script: echo My favorite vegetable is ${{ variables.favoriteVeggie }}.
Or something like:
# File: templates/steps-with-params.yml
parameters:
- name: 'runExtendedTests' # defaults for any parameters that aren't specified
type: boolean
default: false
steps:
- script: npm test
- ${{ if eq(parameters.runExtendedTests, true) }}:
- script: npm test --extended
# File: azure-pipelines.yml
steps:
- script: npm install
- template: templates/steps-with-params.yml # Template reference
parameters:
runExtendedTests: 'true'