全部產品
Search
文件中心

Resource Orchestration Service:Fn::Replace

更新時間:Jun 19, 2024

調用內建函式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"
                ]
              ]
            }
          ]
        }
      }
    }
  }
}

支援的函數