2017-08-04 5 views
0

と(長期的に)人生を容易にするための試みでは十分に文書properties.templateLinkとは反対に、私は、properties.templateを使用しようとしています。前者は、child.jsonテンプレートファイルの内容をparent.jsonテンプレートにテンプレートのパラメータとして渡すことで、ドキュメントをほとんど作成していません。 Microsoft.Resources/deploymentsのためのMSのドキュメントからAzureのネストされたテンプレートの展開:使用するテンプレート要素(ないtemplateLink)のPowerShell

:私の親テンプレートで

The template content. You use this element when you want to pass the template syntax directly in the request rather than link to an existing template. It can be a JObject or well-formed JSON string. Use either the templateLink property or the template property, but not both.

、私はパラメータchildTemplatesを宣言し、properties.templateでそれを参照しています:

"parameters": { 
    "childTemplates": { 
     "type": "object", 
     "metadata": { 
     "description": "Child template" 
     } 
    } 
} 

other stuff... 


"resources": [ 
    { 
    "name": "[concat('linkedTemplate-VM-Net-',copyIndex(1))]", 
    "type": "Microsoft.Resources/deployments", 
    "apiVersion": "2017-06-01", 
    "dependsOn": [], 
    "copy": { 
     "name": "interate", 
     "count": "[parameters('vmQty')]" 
    }, 
    "properties": { 
     "mode": "Incremental", 
     "template": "[parameters('childTemplates')]", 
     "parameters": { 
     "sharedVariables": { "value": "[variables('sharedVariables')]" }, 
     "sharedTemplate": { "value": "[variables('sharedTemplate')]" }, 
     "artifactsLocationSasToken": { "value": "[parameters('artifactsLocationSasToken')]" }, 
     "adminPassword": { "value": "[parameters('adminPassword')]" }, 
     "copyIndexValue": { "value": "[copyIndex(1)]" } 
     }, 
     "debugSetting": { 
     "detailLevel": "both" 
     } 
    } 
    } 
], 

私はその後、子テンプレートを渡します

$TemplateFileLocation = "C:\Temp\templates\parent.json" 
$JsonChildTemplate = Get-Content -Raw (Join-Path ($TemplateFileLocation | Split-Path -Parent) "nestedtemplates\child.json") | ConvertFrom-Json 

$TemplateParameters = @{ 
    childTemplates = $JsonChildTemplate 
    ...Other parameters... 
} 

New-AzureRmResourceGroupDeployment -TemplateParameterObject $TemplateParameters 
New-AzureRmResourceGroupDeployment -TemplateParameterObjectに親テンプレートを展開します

これは、次のエラーを生成します。私は$JsonChildTemplateを見れば

Code : InvalidTemplate 
Message : The nested deployment 'linkedTemplate-VM-Net-1' failed validation: 'Required property '$schema' not found in JSON. Path 'properties.template'.'. 
Target : 
Details : 

、それは私を与える:$schemaがあるよう

$schema  : https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json# 
contentVersion : 1.0.0.0 
parameters  : @{sharedVariables=; sharedTemplate=; vhdStorageAccountName=; artifactsLocationSasToken=; adminPassword=; copyIndexValue=} 
variables  : @{seqNo=[padleft(add(parameters('copyIndexValue'),3),3,'0')]; nicName=[concat('NIC-',parameters('sharedVariables').role,'-', variables('seqNo'),'-01')]; 
       subnetRef=[parameters('sharedVariables').network.subnetRef]; ipConfigName=[concat('ipconfig-', variables('seqNo'))]} 
resources  : {@{apiVersion=2016-03-30; type=Microsoft.Network/networkInterfaces; name=[variables('nicName')]; location=[resourceGroup().location]; tags=; dependsOn=System.Object[]; 
       properties=}} 
outputs  : @{nicObject=; vmPrivateIp=; vmNameSuffix=; vmPrivateIpArray=} 

私には、それが見えます。

同じエラーで| ConvertFrom-Jsonを削除してみました。上記

、私は最新のAPIのバージョンを示していますが、私はバグがあります念のために、そのような2016年9月1日のように他の人と試してみました。ソリューションのための私の検索で

は、私がGitHubの上this問題を発見しました。返信は$schemacontentVersionを削除することですが、これはエラーが発生したときに飛んでいます。

Function Get-ChildTemplate 
{ 
    $TemplateFileLocation = "C:\Temp\templates\nestedtemplates\child.json" 

    $json = Get-Content -Raw -Path $TemplateFileLocation | ConvertFrom-Json 

    $NewJson = @() 
    $NewJson += $json.parameters 
    $NewJson += $json.variables 
    $NewJson += $json.resources 
    $NewJson += $json.outputs 

    Return $NewJson | ConvertTo-Json 
} 

$JsonChildTemplate = Get-ChildTemplate 
$TemplateParameters = @{ 
    childTemplates = $JsonChildTemplate 
    ...Other parameters... 
} 

$JsonChildTemplateリターン:

[ 
    { 
     "sharedVariables": { 
           "type": "object", 
           "metadata": "@{description=Object of variables from master template}" 
          }... 

私の推測では、私がNew-AzureRmResourceGroupDeploymentにchild.jsonの内容を渡して何か間違ったことを行っているということです私は次のようにこれを試してみました。それは、私がやろうとしていることを実際に行うことはできません。

P.S.

get-command New-AzureRmResourceGroupDeployment 

CommandType  Name            Version Source                            
-----------  ----            ------- ------                            
Cmdlet   New-AzureRmResourceGroupDeployment     4.1.0  AzureRM.Resources 

答えて

0

あなたが言われていることになり0意味ので、これまで何をやっていることをまず第一に、あなたを助けるために試すことができます。

  1. は、スプラッティングを試してみてください。あなたがしていることの代わりにNew-AzureRmResourceGroupDeployment ... @TemplateParametersをしてください。それが直接作業最低限にあなたのネストされたテンプレートを簡素化しようとすると、それは、あなたのネストされたテンプレートは大丈夫ですかどうかを確認しなければ、それが動作するかどうかを確認していない場合
  2. (ないアイデアは、何とかそれは私の経験ではうまく機能しません)。
  3. -Debugスイッチを使用してデプロイメントを作成し、その場所を確認してください。
  4. Azure Cliを使用して同じデプロイメントを試してください(おそらくjsonを適切な方法でオブジェクトを入力するように変換してください)
  5. 項目1〜4をスキップし、適切な方法で行います。 ARMテンプレートのフライト生成では、アドバイスを前処理しないでください。あなたが スマート ハッキーであれば、彼らはすでに何かを達成するのに十分な機能を持っています。私はあなたが達成しようとしているものは考えているが、私はあなたが

小さなテンプレートの例を作成しようとしている怪物必要はありませんそれに私の人生を賭けることができます。

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": {}, 
    "resources": [] 
} 

EDITを:もう少し掘り下げて解決策を見つけました。それを行う1つの方法は、文字列を受け取り、有効なjsonに変換するアームテンプレートの関数json()を使用することです。

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": { 
     "inp": { 
      "type": "string" 
     } 
    }, 
    "resources": [ 
     { 
      "name": "NestedDeployment1", 
      "type": "Microsoft.Resources/deployments", 
      "apiVersion": "2015-01-01", 
      "properties": { 
       "mode": "Incremental", 
       "template": "[json(parameters('inp'))]", 
       "parameters": {} 
      } 
     } 
    ] 
} 

使用にこのような何か展開するには:

New-AzureRmResourceGroupDeployment ... -inp ((get-content path\to.json -raw) -replace '\s','') 
# we minify the string so it gets properly converted to json 

をこれはハックのビットですが、問題は、PowerShellは、それがテンプレートに渡すものに、あなたの入力を変換する方法であり、そしてあなたがすることはできません本当にそれを制御する。

それを行う別の方法:(あなたは出力が必要な場合は、別のパラメータを追加することができます)

{ 
    "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
    "contentVersion": "1.0.0.0", 
    "parameters": { 
     "input-param": { 
      "type": "object" 
     }, 
     "input-resource": { 
      "type": "array" 
     } 
    }, 
    "resources": [ 
     { 
      "name": "NestedDeployment1", 
      "type": "Microsoft.Resources/deployments", 
      "apiVersion": "2015-01-01", 
      "properties": { 
       "mode": "Incremental", 
       "template": { 
        "$schema": "https://schema.management.azure.com/schemas/2015-01-01/deploymentTemplate.json#", 
        "contentVersion": "1.0.0.0", 
        "parameters": "[parameters('input-param')]", 
        "resource": "[parameters('input-resource')]" 
       }, 
       "parameters": {} 
      } 
     } 
    ] 
} 

などのような展開:

New-AzureRmResourceGroupDeployment -ResourceGroupName zzz -TemplateFile path\to.json -input-param @{...} -input-resource @(...) 

PS。ウォルターに気をつけないでください。何かができない、あるいは不可能だと言うたびに、実際には可能です。

+0

ありがとうございました。 'json()'関数は、(私の無意味なアプローチのために)扱います。はい、私はウォルターのコメントに気づきました。彼はそれ以来、完全に異なるパラメータを参照しています。 – woter324

関連する問題