Fn::Replace は、文字列内の部分文字列に一致するすべての箇所を新しい文字列に置換します。
関数の宣言
-
JSON
{ "Fn::Replace": [ { "object_oldValue1": "object_newValue1", "object_oldValue2": "object_newValue2" }, "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"
]
]
}
]
}
}
}
}
}