使用引用

定义配置或模板的属性时,您可以使用对其他资源属性的references,而不是直接提供值。例如,如果要创建一个使用同一部署中的实例模板的实例组管理器,您可以通过语法 $(ref.instance-template.selfLink) 使用引用,而无需显式输入该实例模板的完整链接。

通过引用,您可以:

  • 访问在创建资源之后才定义的属性。例如,在配置中定义虚拟机时,您还不知道其 IP 地址。但是,您仍然可以使用对该 IP 地址的引用。部署配置时,首先创建虚拟机,Deployment Manager 会在外部 IP 地址可用时获取该 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 会首先创建 network-a,然后再创建 sandbox-vm,以便可以解析引用。如果有任何引用未成功解析,则部署将失败。

您可以在配置和模板中使用引用。

准备工作

在配置文件中进行引用

使用以下格式在配置中声明引用:

$(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'}]
    

后续步骤