Fn::Replace remplace toutes les occurrences d'une sous-chaîne dans une chaîne par une nouvelle chaîne.
Déclaration de la fonction
-
JSON
{ "Fn::Replace": [ { "object_key1": "object_value1", "object_key2": "object_value2" }, "object_string" ] } -
YAML
-
Syntaxe complète.
Fn::Replace: - object_oldValue1: object_newValue1 object_oldValue2: object_newValue2 - object_string -
Syntaxe abrégée.
!Replace [{'object_oldValue1': 'object_newValue1', 'object_oldValue2': 'object_newValue2'}, object_string]
-
Paramètres
object_oldValue: sous-chaîne à remplacer.object_newValue: chaîne de remplacement.object_string: chaîne source dans laquelleobject_oldValueest remplacée.
Valeur de retour
La chaîne modifiée.
Exemples
Fn::Replace sert couramment à modifier les scripts UserData et CommandContent. L'exemple suivant remplace print par echo dans un script.
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"
]
]
}
]
}
}
}
}
}