使用參照

定義設定或範本的屬性時,您可以使用對其他資源屬性的參照,而非直接提供值。舉例來說,如要建立使用來自同一項部署作業的執行個體範本的執行個體群組管理員,而不是明確輸入執行個體範本的完整連結,您可以使用語法為 $(ref.instance-template.selfLink) 的參照。

您可以透過參照執行下列操作:

  • 存取直到建立資源時才定義的屬性。例如,在設定中定義虛擬機器時,您還不知道其 IP 位址。但是,您仍然可以使用 IP 位址的參照。部署設定時,會先建立 VM,然後 Deployment Manager 會在位址可用時取得外部 IP 位址。

  • 使設定或範本更容易閱讀和疑難排解。 例如,如果您需要設定多個轉送規則,也必須指定要使用的網路。 您可以使用下列語法建立網路 selfLink 屬性的參照,而不是為每項轉送規則提供網路連結:

    $(ref.network-name.selfLink)
    

    如果您需要疑難排解設定,則透過參照可以更容易地判斷出轉送規則使用的網路。

建立資源的參照時,您也會在資源之間建立相依性。舉例來說,您可以考慮下列程式碼片段,其中 sandbox-vm 會使用 network-a 的參照:

resources:
- name: sandbox-vm
  type: compute.v1.instance
  properties:
    network: $(ref.network-a.selfLink)
    ...
...
- name: network-a
  type: compute.v1.network
  properties:
    ...

當您部署這項設定時,Deployment Manager 會在 sandbox-vm 之前建立 network-a,以便解析參照。如果有任何參照未成功解析,您的部署將失敗。

設定和範本皆可使用參照。

事前準備

在設定檔中使用參照

請使用下列格式,在設定中宣告參照:

$(ref.RESOURCE_NAME.PROPERTY)

下列示例先建立網路,然後建立兩個使用新建立網路參照的執行個體。本示例的參照為:

$(ref.a-new-network.selfLink)
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

resources:
- name: the-first-vm
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/f1-micro
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    # The resource's "network value" has been replaced with a
    # reference to the new network's "selfLink" property. The network 
    # resource has been added to the end of this file.
    - network: $(ref.a-new-network.selfLink)
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
- name: the-second-vm
  type: compute.v1.instance
  properties:
    zone: us-central1-f
    machineType: https://www.googleapis.com/compute/v1/projects/MY_PROJECT/zones/us-central1-f/machineTypes/g1-small
    disks:
    - deviceName: boot
      type: PERSISTENT
      boot: true
      autoDelete: true
      initializeParams:
        sourceImage: https://www.googleapis.com/compute/v1/projects/debian-cloud/global/images/family/debian-11
    networkInterfaces:
    # As in the resource above, the "network" value has been replaced with 
    # the new network's "selfLink" property. 
    - network: $(ref.a-new-network.selfLink)
      accessConfigs:
      - name: External NAT
        type: ONE_TO_ONE_NAT
# The following network is a new resource added to the "two-vms.yaml" file.
- name: a-new-network
  type: compute.v1.network
  properties:
    routingConfig:
      routingMode: REGIONAL
    autoCreateSubnetworks: true

部署這個設定時,將在兩個實例之前建立網路,參照也會解析為網路資源的 selfLink

在範本中使用參照

在範本檔案中,整個參照必須以 $ 做為開頭,並包含在一組括號中:

$(ref.RESOURCE_NAME.PROPERTY)

您可以將參照與範本屬性和環境變數等其他功能結合在一起使用。為確保 Deployment Manager 正確解析參照,請務必將整個參照字串放在括號內,這一點很重要。

以下列舉幾個範本中宣告參照的示例。

Jinja

  • 包含環境變數的參照

    network: $(ref.{{ env["deployment"] }}-network.selfLink)
    
  • 陣列中值的參照

    subnetwork: $(ref.{{ env["deployment"] }}-vm.networkInterfaces[2].subnetwork)
    
  • 包含範本屬性的參照

    network: $(ref.{{ properties["network"] }}.selfLink)
    
  • 使用 Jinja 參數的參照

    network: $(ref.{{ NETWORK_NAME }}.selfLink)
    
  • 輸出中的參照

      outputs:
      - name: UrlToService
        value: http://$(ref.{{ env["deployment"] }}-network.networkInterfaces[0].accessConfigs[0].natIp):8080/
    

Python

  • 包含環境變數的參照

    'network': '$(ref.' + context.env['deployment'] + '-network.selfLink)'
    
  • 陣列中值的參照

    'subnetwork': '$(ref.' + context.env['deployment'] + '-vm.networkInterfaces[2].subnetwork)'
    
  • 包含範本屬性的參照

    'network': '$(ref.' + context.properties['network'] + '.selfLink)'
    
  • 使用 Python 參數的參照

    'value': '$(ref.' + base_name + '.networkInterfaces[0].networkIP)'
    
  • 輸出中的參照

    outputs = [{'name': 'UrlToService',
                'value': '$(ref.' + context.env['deployment'] + '-network.networkInterfaces[0].accessConfigs[0].natIP):8080'}]
    

後續步驟