すべてのプロダクト
Search
ドキュメントセンター

Resource Orchestration Service:Fn::Replace

最終更新日:Jan 16, 2025

組み込み関数 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 で指定された既存の部分文字列を含む文字列。

戻り値

部分文字列が置換された文字列。

Fn::Replace 関数は、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"  // UserData スクリプトと CommandContent スクリプトを変更するためによく使用されます
                ]
              ]
            }
          ]
        }
      }
    }
  }
}

サポートされている関数