如何将JSON数组作为参数传递给AzureDevOps管道中的另一个YAML模板

huangapple go评论52阅读模式
英文:

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('"', '&quot;')
    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

{\&quot;arr\&quot;:[{\&quot;priority\&quot;:1,\&quot;ipAddress\&quot;:\&quot;&lt;IP Address&gt;\&quot;,\&quot;description\&quot;:\&quot;\&quot;,\&quot;name\&quot;:\&quot;test\&quot;,\&quot;action\&quot;:\&quot;Allow\&quot;},{\&quot;priority\&quot;:1,\&quot;ipAddress\&quot;:\&quot;&lt;IP Address&gt;\&quot;,\&quot;description\&quot;:\&quot;\&quot;,\&quot;name\&quot;:\&quot;test1\&quot;,\&quot;action\&quot;:\&quot;Allow\&quot;}]}

Error

At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:338
+ … dt-sem-use2&quot; ipSecurityRestrictions=&quot; [{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;descri …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:433
+ … :\&quot;64.136.252.163/32\&quot;,\&quot;priority\&quot;:1},{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;ipAddr …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:528
+ … \&quot;,\&quot;priority\&quot;:1,\&quot;tag\&quot;:\&quot;Default\&quot;},{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;ipAddr …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:30 char:641
+ … priority\&quot;:1000,\&quot;tag\&quot;:\&quot;ServiceTag\&quot;},{\&quot;action\&quot;:\&quot;Deny\&quot;,\&quot;ipAddr …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:366
+ … dt-sem-use2&quot; ipSecurityRestrictions=&quot; [{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;descri …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:461
+ … :\&quot;64.136.252.163/32\&quot;,\&quot;priority\&quot;:1},{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;ipAddr …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:556
+ … \&quot;,\&quot;priority\&quot;:1,\&quot;tag\&quot;:\&quot;Default\&quot;},{\&quot;action\&quot;:\&quot;Allow\&quot;,\&quot;ipAddr …
+                                                               ~
Missing argument in parameter list.
At D:\a\_temp\azureclitaskscript1689449876215_inlinescript.ps1:38 char:669
+ … priority\&quot;:1000,\&quot;tag\&quot;:\&quot;ServiceTag\&quot;},{\&quot;action\&quot;:\&quot;Deny\&quot;,\&quot;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: &#39;AllowedIPs&#39;
            displayName: &#39;GET allowed IPs&#39;
            inputs:
              azureSubscription: ${{ parameters.ams_service_connection }}
              location: $(region)
              scriptType: pscore
              scriptLocation: inlineScript
              inlineScript: | 
                $jsonResult = az deployment group create -g &quot;&lt;groupname&gt;&quot; --template-file &#39;$(Pipeline.Workspace)/infrastructure/api/ip-restrictions.json&#39; --parameters apiManagementName=&lt;amsname&gt; apiManagementRg=&lt;amsrgname&gt; | ConvertFrom-Json
                $ips = @($jsonResult.properties.outputs.ips.value) | ConvertTo-Json -Compress
                $val = $ips.replace(&#39;&quot;&#39;, &#39;\&quot;&#39;)
                Write-Host &quot;##vso[task.setvariable variable=ips;isoutput=true]&quot;$val



- template: deploy/arm-deploy.yml
            parameters:
              displayName: &#39;Configure ADT API IP restrictions&#39;
              azureSubscription: ${{ parameters.adt_service_connection }}
              location: $(region)
              resourceGroupName: &#39;&lt;groupname&gt;&#39;
              force: true
              file: &#39;$(Pipeline.Workspace)/infrastructure/api/adt-api-confog.json&#39;
              parameters: 
                name=&quot;&lt;serviceprincipalname&gt;&quot;
                appServicePlanName=&quot;&lt;serviceplanname&gt;&quot;    
                apiRgName=&quot;&lt;groupname&gt;&quot;   
                ipSecurityRestrictions=&quot;$(AllowedIPs.ips)&quot;

答案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 '&quot;','\&quot;&quot;' // 我在这一行做了更改

之前我是这样做的

$result = @($deployment) -replace '&quot;','\&quot;'

然后改成了

$result = @($deployment) -replace '&quot;','\&quot;&quot;'
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 &quot;&lt;resource-group-name&gt;&quot; --template-file &#39;$(Pipeline.Workspace)/infrastructure/api/adt-ip-restrictions.json&#39; --parameters apiManagementName=&lt;app service website name&gt; apiManagementRg=&lt;website-resource-group-name&gt; --query &#39;properties.outputs.ips.value&#39;
$result = @($deployment) -replace &#39;&quot;&#39;,&#39;\&quot;&quot;&#39; // I had to make a change in this line

earlier I was doing

$result = @($deployment) -replace &#39;&quot;&#39;,&#39;\&quot;&#39;

Then changed it to

$result = @($deployment) -replace &#39;&quot;&#39;,&#39;\&quot;&quot;&#39;
Write-Host &quot;##vso[task.setvariable variable=ips]$result&quot;

Then in next task

az deployment group create --resource-group &quot;&lt;website-resource-group-name&gt;&quot; --template-file &#39;$(Pipeline.Workspace)/infrastructure/api/update-sites.json&#39; --parameters name=&quot;&lt;appservice-website-name&gt;&quot; location=&quot;&lt;location&gt;&quot; ipSecurityRestrictions=&quot;$(ips)&quot; 

huangapple
  • 本文由 发表于 2023年7月17日 15:35:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76702349.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定