文档

如何对ALIYUN::ECS::InstanceGroup创建的资源输出列表进行遍历拼接?

更新时间:

本文以ALIYUN::ECS::InstanceGroup资源为例,向您介绍如何对资源的输出列表进行遍历拼接。

ALIYUN::ECS::InstanceGroup资源用于创建多个ECS实例,创建成功后返回的参数都是以列表的形式展开。更多信息,请参见ALIYUN::ECS::InstanceGroup

例如,ALIYUN::ECS::InstanceGroup资源的返回参数HostNames为['name1', 'name2'],PrivateIps为['192.168.1.1', 192.168.1.2'],经过遍历拼接后的结果为['name1:192.168.1.1', 'name2:192.168.1.2']。

您可以在模板的Output部分先使用内部函数Fn::GetAtt获取对应资源的属性值,再使用内部函数Fn::MergeMapToList将多个Map合并为一个以Map为元素的列表,最后再使用内部函数Fn::Jq循环拼接成需要的格式。

ROSTemplateFormatVersion: '2015-09-01'
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup # 模板中创建的资源部分已省略,下同。
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps

完整模板示例

ROSTemplateFormatVersion: '2015-09-01'
Parameters:
  VpcId:
    Description:
      en: The VPC id to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 专有网络ID
      en: VpcId
    AssociationProperty: ALIYUN::ECS::VPC::VPCId
    Type: String
  ImageId:
    AssociationPropertyMetadata:
      DefaultFilter:
        OSType: linux
        ImageName: aliyun_3_**
        ImageOwnerAlias: system
      InstanceType: ${InstanceType}
      SupportedImageOwnerAlias:
        - system
        - self
        - others
    Description:
      zh-cn: 用于启动ECS实例的镜像ID。包括公共镜像、自定义镜像和云市场镜像。
      en: Image ID to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 用于启动ECS实例的镜像ID
      en: ImageId
    AssociationProperty: ALIYUN::ECS::Image::ImageId
    Type: String
  VSwitchId:
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
      ZoneId: ${ZoneId}
    Description:
      en: The vSwitch Id to create ecs instance.
    Default: Null
    Required: false
    Label:
      zh-cn: 交换机ID
      en: VSwitchId
    AssociationProperty: ALIYUN::VPC::VSwitch::VSwitchId
    Type: String
  Password:
    Description:
      en: Password of created ecs instance. Must contain at least 3 types of special character, lower character, upper character, number.
    Default: Null
    Required: false
    Label:
      zh-cn: ECS实例登录密码
      en: Password
    AssociationProperty: ALIYUN::ECS::Instance::Password
    Type: String
  InstanceType:
    AssociationPropertyMetadata:
      ZoneId: ${ZoneId}
    Description:
      en: Ecs instance supported instance type, make sure it should be correct.
    Default: ecs.g7.xlarge
    Required: true
    Label:
      zh-cn: ECS实例规格
      en: InstanceType
    AssociationProperty: ALIYUN::ECS::Instance::InstanceType
    Type: String
  MaxAmount:
    Description:
      en: Max number of instances to create, should be bigger than 'MinAmount' and smaller than 1000.
    Default: 2
    Required: true
    MaxValue: 1000
    Label:
      zh-cn: 实例数量
    MinValue: 0
    Type: Number
  SystemDiskCategory:
    AssociationPropertyMetadata:
      InstanceType: ${InstanceType}
      ZoneId: ${ZoneId}
    Description:
      en: Category of system disk. Default is cloud_efficiency. support cloud|cloud_efficiency|cloud_ssd|cloud_essd|ephemeral_ssd|cloud_auto|cloud_essd_entry.Old instances will not be changed.
    Default: cloud_essd
    Required: false
    Label:
      zh-cn: 系统盘类型
      en: SystemDiskCategory
    AssociationProperty: ALIYUN::ECS::Disk::SystemDiskCategory
    AllowedValues:
      - cloud_essd
      - cloud_efficiency
      - cloud_ssd
      - cloud
      - cloud_auto
      - cloud_essd_entry
    Type: String
  ZoneId:
    Description:
      en: |-
        The ID of the zone to which the instance belongs. For more information, 
        call the DescribeZones operation to query the most recent zone list. 
        Default value is empty, which means random selection.
    Default: Null
    Required: false
    Label:
      zh-cn: 可用区ID
      en: ZoneId
    AssociationProperty: ZoneId
    Type: String
  SystemDiskSize:
    Description:
      en: 'Disk size of the system disk, range from 20 to 500 GB. If you specify with your own image, make sure the system disk size bigger than image size. '
    Default: 40
    Required: false
    Label:
      zh-cn: 系统盘大小
      en: SystemDiskSize
    MinValue: 20
    Type: Number
  SystemDiskPerformanceLevel:
    AssociationPropertyMetadata:
      Visible:
        Condition:
          Fn::Equals:
            - ${SystemDiskCategory}
            - cloud_essd
    Description:
      zh-cn: 创建ESSD云盘作为系统盘使用时,设置云盘的性能等级。
      en: 'The performance level of the enhanced SSD used as the system disk.Default value: PL1. Valid values:PL0: A single enhanced SSD delivers up to 10,000 random read/write IOPS.PL1: A single enhanced SSD delivers up to 50,000 random read/write IOPS.PL2: A single enhanced SSD delivers up to 100,000 random read/write IOPS.PL3: A single enhanced SSD delivers up to 1,000,000 random read/write IOPS.'
    Default: PL0
    Required: false
    AllowedValues:
      - PL0
      - PL1
      - PL2
      - PL3
    Label:
      zh-cn: ESSD系统盘性能等级
    Type: String
  SecurityGroupId:
    Type: String
    AssociationProperty: ALIYUN::ECS::SecurityGroup::SecurityGroupId
    AssociationPropertyMetadata:
      VpcId: ${VpcId}
Resources:
  InstanceGroup:
    Type: ALIYUN::ECS::InstanceGroup
    Properties:
      VpcId:
        Ref: VpcId
      ImageId:
        Ref: ImageId
      VSwitchId:
        Ref: VSwitchId
      Password:
        Ref: Password
      InstanceType:
        Ref: InstanceType
      MaxAmount:
        Ref: MaxAmount
      SecurityGroupId:
        Ref: SecurityGroupId
      SystemDiskCategory:
        Ref: SystemDiskCategory
      ZoneId:
        Ref: ZoneId
      SystemDiskSize:
        Ref: SystemDiskSize
      SystemDiskPerformanceLevel:
        Ref: SystemDiskPerformanceLevel
Metadata:
  ALIYUN::ROS::Interface:
    ParameterGroups:
      - Parameters:
          - ZoneId
          - VpcId
          - VSwitchId
          - SecurityGroupId
        Label:
          default:
            zh-cn: 网络配置
            en: Network Configuration
      - Parameters:
          - InstanceType
          - ImageId
          - MaxAmount
        Label:
          default:
            zh-cn: 实例与镜像
            en: Instance and Image
      - Parameters:
          - SystemDiskCategory
          - SystemDiskPerformanceLevel
          - SystemDiskSize
        Label:
          default:
            zh-cn: 系统盘设置
            en: System Disk Configuration
      - Parameters:
          - Password
        DefaultExpand: false
        Label:
          default:
            zh-cn: 管理设置
            en: Manage Configuration
Outputs:
  testMapping:
    Value:
      Fn::Jq:
        - All
        - .[] | "\(.a):\(.b)"
        - Fn::MergeMapToList:
            - a:
                Fn::GetAtt:
                  - InstanceGroup
                  - HostNames
            - b:
                Fn::GetAtt:
                  - InstanceGroup
                  - PrivateIps
  • 本页导读 (1)
文档反馈