Fn::Replace

调用内部函数Fn::Replace,用新字符串替换原字符串中的子字符串。

函数声明

  • JSON

    {
      "Fn::Replace": [
        {
          "object_key1": "object_value1",
          "object_key2": "object_value2"
        },
        "object_string"
      ]
    }
  • YAML

    • 完整函数的语法。

      Fn::Replace:
       - object_oldValue1: object_newValue1
         object_oldValue2: object_newValue2
       - object_string
    • 缩写形式。

      !Replace [{'object_oldValue1': 'object_newValue1', 'object_oldValue2': 'object_newValue2'}, object_string]

参数信息

  • object_oldValue:将要被替换的字符串。

  • object_newValue:将要替换成的最终字符串。

  • object_string:包含要被替换的object_oldValue的原字符串。

返回值

被替换后的字符串。

使用示例

该函数常用于UserData、CommandContent脚本的编辑中,例如以下示例将指定脚本中的print替换成echo。

ROSTemplateFormatVersion: '2015-09-01'
Resources:
  WebServer:
    Type: ALIYUN::ECS::Instance
    Properties:
      ImageId: centos_7_2_64_40G_base_20170222****
      InstanceType: ecs.n1.medium
      SecurityGroupId: sg-94q49****
      Password: MytestPassword****
      IoOptimized: optimized
      VSwitchId: vsw-94vdv****
      VpcId: vpc-949uz****
      SystemDiskCategory: cloud_ssd
      UserData:
        !Replace
          - print: echo
          - !Join
              - ''
              - - |
                  #!/bin/sh
                - |
                  mkdir ~/test_ros
                - |
                  print hello > ~/1.txt
{
  "ROSTemplateFormatVersion": "2015-09-01",
  "Resources": {
    "WebServer": {
      "Type": "ALIYUN::ECS::Instance",
      "Properties": {
        "ImageId": "centos_7_2_64_40G_base_20170222****",
        "InstanceType": "ecs.n1.medium",
        "SecurityGroupId": "sg-94q49****",
        "Password": "MytestPassword****",
        "IoOptimized": "optimized",
        "VSwitchId": "vsw-94vdv****",
        "VpcId": "vpc-949uz****",
        "SystemDiskCategory": "cloud_ssd",
        "UserData": {
          "Fn::Replace": [
            {
              "print": "echo"
            },
            {
              "Fn::Join": [
                "",
                [
                  "#!/bin/sh\n",
                  "mkdir ~/test_ros\n",
                  "print hello > ~/1.txt\n"
                ]
              ]
            }
          ]
        }
      }
    }
  }
}

支持的函数