英文:
How pass JSON array as parameter to another YAML template in AzureDevOps pipeline
问题
我有一个情况,需要将JSON数组作为参数传递到另一个YAML模板中,我已经对JSON数组进行了清理,但仍然出现以下错误。
错误
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:30处的字符338
- … dt-sem-use2" ipSecurityRestrictions=" [{"action":"Allow","descri …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:30处的字符433
- … :"64.136.252.163/32","priority":1},{"action":"Allow","ipAddr …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:30处的字符528
- … ","priority":1,"tag":"Default"},{"action":"Allow","ipAddr …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:30处的字符641
- … priority":1000,"tag":"ServiceTag"},{"action":"Deny","ipAddr …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:38处的字符366
- … dt-sem-use2" ipSecurityRestrictions=" [{"action":"Allow","descri …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:38处的字符461
- … :"64.136.252.163/32","priority":1},{"action":"Allow","ipAddr …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:38处的字符556
- … ","priority":1,"tag":"Default"},{"action":"Allow","ipAddr …
-
~
参数列表中缺少参数。
在D:\a_temp\azureclitaskscript1689449876215_inlinescript.ps1:38处的字符669
- … priority":1000,"tag":"ServiceTag"},{"action":"Deny","ipAddr …
-
~
参数列表中缺少参数。
- CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
- FullyQualifiedErrorId : MissingArgument
有人可以帮忙解决我在这里做错了什么吗?
非常感谢。
以下是我正在尝试的YAML代码
-
task: AzureCLI@2
name: 'AllowedIPs'
displayName: 'GET allowed IPs'
inputs:
azureSubscription: ${{ parameters.ams_service_connection }}
location: $(region)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$jsonResult = az deployment group create -g "<groupname>" --template-file '$(Pipeline.Workspace)/infrastructure/api/ip-restrictions.json' --parameters apiManagementName=<amsname> apiManagementRg=<amsrgname> | ConvertFrom-Json
$ips = @($jsonResult.properties.outputs.ips.value) | ConvertTo-Json -Compress
$val = $ips.replace('"', '"')
Write-Host "##vso[task.setvariable variable=ips;isoutput=true]"$val -
template: deploy/arm-deploy.yml
parameters:
displayName: 'Configure ADT API IP restrictions'
azureSubscription: ${{ parameters.adt_service_connection }}
location: $(region)
resourceGroupName: '<groupname>'
force: true
file: '$(Pipeline.Workspace)/infrastructure/api/adt-api-confog.json'
parameters:
name="<serviceprincipalname>"
appServicePlanName="<serviceplanname>"
apiRgName="<groupname>"
ipSecurityRestrictions="$(AllowedIPs.ips)"
英文:
I have a situation where I need to pass JSON array as parameter into another YAML template, I have already sanitized the json array but still getting below error.
Array
{\"arr\":[{\"priority\":1,\"ipAddress\":\"<IP Address>\",\"description\":\"\",\"name\":\"test\",\"action\":\"Allow\"},{\"priority\":1,\"ipAddress\":\"<IP Address>\",\"description\":\"\",\"name\":\"test1\",\"action\":\"Allow\"}]}
Error
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:338
+ … dt-sem-use2" ipSecurityRestrictions=" [{\"action\":\"Allow\",\"descri …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:433
+ … :\"64.136.252.163/32\",\"priority\":1},{\"action\":\"Allow\",\"ipAddr …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:528
+ … \",\"priority\":1,\"tag\":\"Default\"},{\"action\":\"Allow\",\"ipAddr …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:641
+ … priority\":1000,\"tag\":\"ServiceTag\"},{\"action\":\"Deny\",\"ipAddr …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:366
+ … dt-sem-use2" ipSecurityRestrictions=" [{\"action\":\"Allow\",\"descri …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:461
+ … :\"64.136.252.163/32\",\"priority\":1},{\"action\":\"Allow\",\"ipAddr …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:556
+ … \",\"priority\":1,\"tag\":\"Default\"},{\"action\":\"Allow\",\"ipAddr …
+ ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:669
+ … priority\":1000,\"tag\":\"ServiceTag\"},{\"action\":\"Deny\",\"ipAddr …
+ ~
Missing argument in parameter list.
+ CategoryInfo : ParserError: (:) [], ParentContainsErrorRecordException
+ FullyQualifiedErrorId : MissingArgument
Can anybody help what I am doing wrong here.
Many thanks in advance.
Below is yaml code that I am trying
- task: AzureCLI@2
name: 'AllowedIPs'
displayName: 'GET allowed IPs'
inputs:
azureSubscription: ${{ parameters.ams_service_connection }}
location: $(region)
scriptType: pscore
scriptLocation: inlineScript
inlineScript: |
$jsonResult = az deployment group create -g "<groupname>" --template-file '$(Pipeline.Workspace)/infrastructure/api/ip-restrictions.json' --parameters apiManagementName=<amsname> apiManagementRg=<amsrgname> | ConvertFrom-Json
$ips = @($jsonResult.properties.outputs.ips.value) | ConvertTo-Json -Compress
$val = $ips.replace('"', '\"')
Write-Host "##vso[task.setvariable variable=ips;isoutput=true]"$val
- template: deploy/arm-deploy.yml
parameters:
displayName: 'Configure ADT API IP restrictions'
azureSubscription: ${{ parameters.adt_service_connection }}
location: $(region)
resourceGroupName: '<groupname>'
force: true
file: '$(Pipeline.Workspace)/infrastructure/api/adt-api-confog.json'
parameters:
name="<serviceprincipalname>"
appServicePlanName="<serviceplanname>"
apiRgName="<groupname>"
ipSecurityRestrictions="$(AllowedIPs.ips)"
答案1
得分: 0
发布修复后,我成功解决了这个问题。
$deployment = az deployment group create --resource-group "<resource-group-name>" --template-file '$(Pipeline.Workspace)/infrastructure/api/adt-ip-restrictions.json' --parameters apiManagementName=<app service website name> apiManagementRg=<website-resource-group-name> --query 'properties.outputs.ips.value'
$result = @($deployment) -replace '"','\""' // 我在这一行做了更改
之前我是这样做的
$result = @($deployment) -replace '"','\"'
然后改成了
$result = @($deployment) -replace '"','\""'
Write-Host "##vso[task.setvariable variable=ips]$result"
然后在下一个任务中
az deployment group create --resource-group "<website-resource-group-name>" --template-file '$(Pipeline.Workspace)/infrastructure/api/update-sites.json' --parameters name="<appservice-website-name>" location="<location>" ipSecurityRestrictions="$(ips)"
英文:
Posting the fix I was able to resolve the issue.
$deployment = az deployment group create --resource-group "<resource-group-name>" --template-file '$(Pipeline.Workspace)/infrastructure/api/adt-ip-restrictions.json' --parameters apiManagementName=<app service website name> apiManagementRg=<website-resource-group-name> --query 'properties.outputs.ips.value'
$result = @($deployment) -replace '"','\""' // I had to make a change in this line
earlier I was doing
$result = @($deployment) -replace '"','\"'
Then changed it to
$result = @($deployment) -replace '"','\""'
Write-Host "##vso[task.setvariable variable=ips]$result"
Then in next task
az deployment group create --resource-group "<website-resource-group-name>" --template-file '$(Pipeline.Workspace)/infrastructure/api/update-sites.json' --parameters name="<appservice-website-name>" location="<location>" ipSecurityRestrictions="$(ips)"
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论